| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
namespace MyParcelNL\Sdk\src\Model\Consignment; |
| 4 |
|
| 5 |
use MyParcelNL\Sdk\src\Exception\InvalidConsignmentException; |
| 6 |
|
| 7 |
class DPDConsignment extends AbstractConsignment |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @var int |
| 11 |
*/ |
| 12 |
public const CARRIER_ID = 4; |
| 13 |
|
| 14 |
/** |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
public const CARRIER_NAME = 'dpd'; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var int |
| 21 |
*/ |
| 22 |
public const DEFAULT_WEIGHT = 3000; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
public const INSURANCE_POSSIBILITIES_LOCAL = [0]; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var array |
| 31 |
*/ |
| 32 |
private const VALID_PACKAGE_TYPES = [ |
| 33 |
self::PACKAGE_TYPE_PACKAGE |
| 34 |
]; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
public const ADDITIONAL_COUNTRY_COSTS = [ |
| 40 |
'BA', |
| 41 |
'IS', |
| 42 |
'HR', |
| 43 |
'MC', |
| 44 |
'NO', |
| 45 |
'UA', |
| 46 |
'RS', |
| 47 |
'CH' |
| 48 |
]; |
| 49 |
|
| 50 |
/** |
| 51 |
* @internal |
| 52 |
* |
| 53 |
* @var int |
| 54 |
*/ |
| 55 |
public $physical_properties = ['weight' => self::DEFAULT_WEIGHT]; |
| 56 |
|
| 57 |
/** |
| 58 |
* @var string |
| 59 |
*/ |
| 60 |
protected $local_cc = self::CC_BE; |
| 61 |
|
| 62 |
/** |
| 63 |
* @param array $consignmentEncoded |
| 64 |
* |
| 65 |
* @return array |
| 66 |
* @throws \MyParcelNL\Sdk\src\Exception\MissingFieldException |
| 67 |
*/ |
| 68 |
public function encodeStreet(array $consignmentEncoded): array |
| 69 |
{ |
| 70 |
if ($this->getCountry() == $this->local_cc) { |
| 71 |
$consignmentEncoded = array_merge_recursive( |
| 72 |
$consignmentEncoded, |
| 73 |
[ |
| 74 |
'recipient' => [ |
| 75 |
'street' => $this->getStreet(true), |
| 76 |
'street_additional_info' => $this->getStreetAdditionalInfo(), |
| 77 |
'number' => $this->getNumber(), |
| 78 |
'box_number' => (string) $this->getBoxNumber(), |
| 79 |
], |
| 80 |
] |
| 81 |
); |
| 82 |
|
| 83 |
return $consignmentEncoded; |
| 84 |
} |
| 85 |
|
| 86 |
if ($this->getCountry() == self::CC_NL) { |
| 87 |
$consignmentEncoded = array_merge_recursive( |
| 88 |
$consignmentEncoded, |
| 89 |
[ |
| 90 |
'recipient' => [ |
| 91 |
'street' => $this->getStreet(true), |
| 92 |
'street_additional_info' => $this->getStreetAdditionalInfo(), |
| 93 |
'number' => $this->getNumber(), |
| 94 |
'number_suffix' => (string) $this->getNumberSuffix(), |
| 95 |
], |
| 96 |
] |
| 97 |
); |
| 98 |
|
| 99 |
return $consignmentEncoded; |
| 100 |
} |
| 101 |
|
| 102 |
return parent::encodeStreet($consignmentEncoded); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* The package type |
| 107 |
* |
| 108 |
* For international shipment only package type 1 is allowed |
| 109 |
* Pattern: [1 – 3]<br> |
| 110 |
* Example: |
| 111 |
* 1. package |
| 112 |
* 2. mailbox package |
| 113 |
* 3. letter |
| 114 |
* Required: Yes |
| 115 |
* |
| 116 |
* @param int $packageType |
| 117 |
* |
| 118 |
* @return $this |
| 119 |
* @throws \Exception |
| 120 |
*/ |
| 121 |
public function setPackageType(int $packageType): AbstractConsignment |
| 122 |
{ |
| 123 |
if (! in_array($packageType, self::VALID_PACKAGE_TYPES)) { |
| 124 |
throw new \Exception('Use the correct package type for shipment:' . self::INSURANCE_POSSIBILITIES_LOCAL); |
| 125 |
} |
| 126 |
|
| 127 |
return parent::setPackageType($packageType); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* The delivery date time for this shipment |
| 132 |
* Pattern: YYYY-MM-DD | YYYY-MM-DD HH:MM:SS |
| 133 |
* Example: 2017-01-01 | 2017-01-01 00:00:00 |
| 134 |
* Required: Yes if delivery type has been specified |
| 135 |
* |
| 136 |
* @param string $delivery_date |
| 137 |
* |
| 138 |
* @return \MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment |
| 139 |
* @throws \Exception |
| 140 |
*/ |
| 141 |
public function setDeliveryDate(?string $delivery_date): AbstractConsignment |
| 142 |
{ |
| 143 |
return parent::setDeliveryDate(null); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Insurance price for the package. |
| 148 |
* |
| 149 |
* Composite type containing integer and currency. The amount is without decimal separators. |
| 150 |
* Required: No |
| 151 |
* |
| 152 |
* @param int|null $insurance |
| 153 |
* |
| 154 |
* @return \MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment |
| 155 |
* @throws \Exception |
| 156 |
*/ |
| 157 |
public function setInsurance(?int $insurance): AbstractConsignment |
| 158 |
{ |
| 159 |
if (null === $insurance) { |
| 160 |
throw new \BadMethodCallException('Insurance must be one of ' . implode(', ', self::INSURANCE_POSSIBILITIES_LOCAL)); |
| 161 |
} |
| 162 |
|
| 163 |
return parent::setInsurance($insurance); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* @return string |
| 168 |
* @deprecated Use setRetailNetworkId instead |
| 169 |
* |
| 170 |
*/ |
| 171 |
public function getPickupNetworkId(): string |
| 172 |
{ |
| 173 |
return $this->getRetailNetworkId(); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* @return string |
| 178 |
*/ |
| 179 |
public function getRetailNetworkId(): string |
| 180 |
{ |
| 181 |
return ""; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
/** |
| 186 |
* Pattern: [0-9A-Za-z] |
| 187 |
* Example: Albert Heijn |
| 188 |
* Required: Yes for pickup location |
| 189 |
* |
| 190 |
* @param string $retailNetworkId |
| 191 |
* |
| 192 |
* @return \MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment |
| 193 |
* @deprecated Use setRetailNetworkId instead |
| 194 |
* |
| 195 |
*/ |
| 196 |
public function setPickupNetworkId($retailNetworkId): AbstractConsignment |
| 197 |
{ |
| 198 |
return $this->setRetailNetworkId((string) $retailNetworkId); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Pattern: [0-9A-Za-z] |
| 203 |
* Example: Albert Heijn |
| 204 |
* Required: Yes for pickup location |
| 205 |
* |
| 206 |
* @param string $retailNetworkId |
| 207 |
* |
| 208 |
* @return \MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment |
| 209 |
*/ |
| 210 |
public function setRetailNetworkId(string $retailNetworkId): AbstractConsignment |
| 211 |
{ |
| 212 |
$this->retail_network_id = $retailNetworkId; |
| 213 |
|
| 214 |
return $this; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* @return bool |
| 219 |
* @throws \MyParcelNL\Sdk\src\Exception\InvalidConsignmentException |
| 220 |
*/ |
| 221 |
public function validate(): bool |
| 222 |
{ |
| 223 |
if ($this->getTotalWeight() < 1) { |
| 224 |
throw new InvalidConsignmentException('It is necessary to at a minimum weight of 1 grams'); |
| 225 |
} |
| 226 |
|
| 227 |
return parent::validate(); |
| 228 |
} |
| 229 |
} |
| 230 |
|