public function __construct($data = [], $connection = null) { $this->connection = $connection; foreach (['id', 'shipment_reference', 'tracking_url'] as $prop) { if (isset($data[$prop])) { $this->{$prop} = $data[$prop]; } } foreach (['sender', 'recipient'] as $prop) { $this->{$prop} = isset($data[$prop]) ? Address::import($data[$prop]) : null; } $this->product = isset($data['product']) ? Product::import($data['product']) : null; if (isset($data['parcels'])) { $this->parcels = []; foreach ($data['parcels'] as $parcel_data) { $this->parcels[] = Parcel::import($parcel_data); } } if (isset($data['labels'])) { $this->labels = []; foreach ($data['labels'] as $url) { $this->labels[] = Label::import($url, $connection); } } if (isset($data['order'])) { $this->order = Order::import($data['order'], $connection); } if (isset($data['allow'])) { $this->allow = $data['allow']; } }
public function fetch($mixed) { if ($mixed instanceof LabelRef) { $path = $mixed->url; } else { $path = "labels/{$mixed}"; } $response = $this->client()->get($path); if ($response->getStatusCode() == 200) { $content_disposition = $response->getHeader('Content-Disposition'); $filename = null; if (preg_match('/filename="([^"]+)"/', $content_disposition[0], $mm)) { $filename = $mm[1]; } if ($filename) { return Label::import(['filename' => $filename, 'content' => $response->getBody()], $this->connection); } } }