markosirec / gls-italy-sdk

An unofficial PHP SDK for the GLS Italy webservice
13 stars 11 forks source link

Empty PdfLabel #7

Open zazy opened 1 year ago

zazy commented 1 year ago

In ParcelAdapter::parseAddResponse, the $responseObject->PdfLabel member is an array of SimpleXMLElement (even for a single label) but it's casted to a string resulting in an empty Pdf. I removed the cast and changed the AddParcelResponse::setPdfLabel method signature so that $pdfLabel parameter is an Array:

public function setPdfLabel(array $pdfLabel): void

furthermore I added a method to add a pdfLabel:

 public function addPdfLabel(string $pdfLabel) : void {
    $this->pdfLabel[] = $pdfLabel;
}

Added also a method to retrieve the number of labels in AddParcelResponse:

public function getNumLabels() : int {
    if(empty($this->pdfLabel)) {
        return 0;
    } else if(is_array($this->pdfLabel)) {
        return count($this->pdfLabel);
    }
}

and modified the getPdfLabel() method:

public function getPdfLabel(int $idx): ?string {
    return $this->pdfLabel[$idx] ?? null;
}

Finally in the ParcelAdapter::parseAddResponse method i changed this line:

$response->setPdfLabel((string)$parcel->PdfLabel);

with this:

foreach($parcel->PdfLabel as $label) {
    $response->addPdfLabel($label);
}

I'm not an expert about GLS API so I don't know if these changes are correct. For now this is for me the only way to get the Labels.