zangra-dev /
bpost-api-library
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace Bpost\BpostApiClient\Bpost\Order\Box; |
||
| 5 | |||
| 6 | use Bpost\BpostApiClient\Bpost\Order\Box\OpeningHour\Day; |
||
| 7 | use Bpost\BpostApiClient\Bpost\Order\Box\Option\Messaging; |
||
| 8 | use Bpost\BpostApiClient\Bpost\Order\Box\Option\Option; |
||
| 9 | use Bpost\BpostApiClient\BpostException; |
||
| 10 | use Bpost\BpostApiClient\Common\ComplexAttribute; |
||
| 11 | use Bpost\BpostApiClient\Common\XmlHelper; |
||
| 12 | use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException; |
||
| 13 | use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException; |
||
| 14 | use Bpost\BpostApiClient\Exception\BpostNotImplementedException; |
||
| 15 | use Bpost\BpostApiClient\Exception\XmlException\BpostXmlInvalidItemException; |
||
| 16 | use DOMDocument; |
||
| 17 | use DOMElement; |
||
| 18 | use SimpleXMLElement; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * bPost National class |
||
| 22 | * |
||
| 23 | * @author Tijs Verkoyen <[email protected]> |
||
| 24 | * |
||
| 25 | * @version 3.0.0 |
||
| 26 | * |
||
| 27 | * @copyright Copyright (c), Tijs Verkoyen. All rights reserved. |
||
| 28 | * @license BSD License |
||
| 29 | */ |
||
| 30 | abstract class National extends ComplexAttribute implements IBox |
||
| 31 | { |
||
| 32 | protected ?string $product = null; |
||
| 33 | |||
| 34 | /** @var Option[] */ |
||
| 35 | protected array $options = []; |
||
| 36 | |||
| 37 | protected ?int $weight = null; |
||
| 38 | |||
| 39 | /** @var Day[] */ |
||
| 40 | private array $openingHours = []; |
||
| 41 | |||
| 42 | private ?string $desiredDeliveryPlace = null; |
||
| 43 | |||
| 44 | public function setOptions(array $options): void |
||
| 45 | { |
||
| 46 | $this->options = $options; |
||
| 47 | } |
||
| 48 | |||
| 49 | public function getOptions(): array |
||
| 50 | { |
||
| 51 | return $this->options; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function addOption(Option $option): void |
||
| 55 | { |
||
| 56 | $this->options[] = $option; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function setProduct(string $product): void |
||
| 60 | { |
||
| 61 | $this->product = $product; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function getProduct(): ?string |
||
| 65 | { |
||
| 66 | return $this->product; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @remark should be implemented by the child class |
||
| 71 | */ |
||
| 72 | public static function getPossibleProductValues(): array |
||
| 73 | { |
||
| 74 | return []; |
||
| 75 | } |
||
| 76 | |||
| 77 | public function setWeight(int $weight): void |
||
| 78 | { |
||
| 79 | $this->weight = $weight; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function getWeight(): ?int |
||
| 83 | { |
||
| 84 | return $this->weight; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function setOpeningHours(array $openingHours): void |
||
| 88 | { |
||
| 89 | $this->openingHours = $openingHours; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function addOpeningHour(Day $day): void |
||
| 93 | { |
||
| 94 | $this->openingHours[] = $day; |
||
| 95 | } |
||
| 96 | |||
| 97 | public function getOpeningHours(): array |
||
| 98 | { |
||
| 99 | return $this->openingHours; |
||
| 100 | } |
||
| 101 | |||
| 102 | public function setDesiredDeliveryPlace(?string $desiredDeliveryPlace): void |
||
| 103 | { |
||
| 104 | $this->desiredDeliveryPlace = $desiredDeliveryPlace; |
||
| 105 | } |
||
| 106 | |||
| 107 | public function getDesiredDeliveryPlace(): ?string |
||
| 108 | { |
||
| 109 | return $this->desiredDeliveryPlace; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @throws \DOMException |
||
| 114 | */ |
||
| 115 | public function toXML(DOMDocument $document, ?string $prefix = null, ?string $type = null): DOMElement |
||
| 116 | { |
||
| 117 | $typeElement = $document->createElement((string)$type); |
||
| 118 | |||
| 119 | if ($this->product !== null) { |
||
| 120 | $typeElement->appendChild( |
||
| 121 | $document->createElement(XmlHelper::getPrefixedTagName('product', $prefix), $this->product) |
||
| 122 | ); |
||
| 123 | } |
||
| 124 | |||
| 125 | if (!empty($this->options)) { |
||
| 126 | $optionsElement = $document->createElement('options'); |
||
| 127 | foreach ($this->options as $option) { |
||
| 128 | $optionsElement->appendChild($option->toXML($document)); |
||
| 129 | } |
||
| 130 | $typeElement->appendChild($optionsElement); |
||
| 131 | } |
||
| 132 | |||
| 133 | if ($this->weight !== null) { |
||
| 134 | $typeElement->appendChild( |
||
| 135 | $document->createElement(XmlHelper::getPrefixedTagName('weight', $prefix), (string)$this->weight) |
||
| 136 | ); |
||
| 137 | } |
||
| 138 | |||
| 139 | if (!empty($this->openingHours)) { |
||
| 140 | $openingHoursElement = $document->createElement('openingHours'); |
||
| 141 | foreach ($this->openingHours as $day) { |
||
| 142 | $openingHoursElement->appendChild($day->toXML($document)); |
||
| 143 | } |
||
| 144 | $typeElement->appendChild($openingHoursElement); |
||
| 145 | } |
||
| 146 | |||
| 147 | if ($this->desiredDeliveryPlace !== null) { |
||
| 148 | $typeElement->appendChild( |
||
| 149 | $document->createElement( |
||
| 150 | XmlHelper::getPrefixedTagName('desiredDeliveryPlace', $prefix), |
||
| 151 | $this->desiredDeliveryPlace |
||
| 152 | ) |
||
| 153 | ); |
||
| 154 | } |
||
| 155 | |||
| 156 | return $typeElement; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @throws BpostInvalidLengthException |
||
| 161 | * @throws BpostNotImplementedException |
||
| 162 | * @throws BpostInvalidValueException |
||
| 163 | * @throws BpostException |
||
| 164 | */ |
||
| 165 | public static function createFromXML(SimpleXMLElement $xml, National $self = null): National |
||
| 166 | { |
||
| 167 | if ($self === null) { |
||
| 168 | throw new BpostException('Set an instance of National'); |
||
| 169 | } |
||
| 170 | |||
| 171 | if (isset($nationalXml->product) && (string)$nationalXml->product !== '') { |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 172 | $self->setProduct((string)$nationalXml->product); |
||
| 173 | } |
||
| 174 | |||
| 175 | if (!empty($nationalXml->options)) { |
||
| 176 | foreach ($nationalXml->options as $optionData) { |
||
| 177 | $optionData = $optionData->children('http://schema.post.be/shm/deepintegration/v3/common'); |
||
| 178 | |||
| 179 | if (in_array( |
||
| 180 | $optionData->getName(), |
||
| 181 | [ |
||
| 182 | Messaging::MESSAGING_TYPE_INFO_DISTRIBUTED, |
||
| 183 | Messaging::MESSAGING_TYPE_INFO_NEXT_DAY, |
||
| 184 | Messaging::MESSAGING_TYPE_INFO_REMINDER, |
||
| 185 | Messaging::MESSAGING_TYPE_KEEP_ME_INFORMED, |
||
| 186 | ], |
||
| 187 | true |
||
| 188 | )) { |
||
| 189 | $option = Messaging::createFromXML($optionData); |
||
| 190 | } else { |
||
| 191 | $option = self::getOptionFromOptionData($optionData); |
||
| 192 | } |
||
| 193 | |||
| 194 | $self->addOption($option); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | if (isset($nationalXml->weight) && (string)$nationalXml->weight !== '') { |
||
| 199 | $self->setWeight((int)$nationalXml->weight); |
||
| 200 | } |
||
| 201 | |||
| 202 | if (isset($nationalXml->openingHours) && (string)$nationalXml->openingHours !== '') { |
||
| 203 | foreach ($nationalXml->openingHours->children() as $day => $value) { |
||
| 204 | $self->addOpeningHour(new Day((string)$day, (string)$value)); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | if (isset($nationalXml->desiredDeliveryPlace) && (string)$nationalXml->desiredDeliveryPlace !== '') { |
||
| 209 | $self->setDesiredDeliveryPlace((string)$nationalXml->desiredDeliveryPlace); |
||
| 210 | } |
||
| 211 | |||
| 212 | return $self; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @throws BpostNotImplementedException |
||
| 217 | */ |
||
| 218 | protected static function getOptionFromOptionData(SimpleXMLElement $optionData): Option |
||
| 219 | { |
||
| 220 | $className = '\\Bpost\\BpostApiClient\\Bpost\\Order\\Box\\Option\\' . ucfirst($optionData->getName()); |
||
| 221 | XmlHelper::assertMethodCreateFromXmlExists($className); |
||
| 222 | |||
| 223 | /** @var callable $factory */ |
||
| 224 | $factory = [$className, 'createFromXML']; |
||
| 225 | return $factory($optionData); |
||
| 226 | } |
||
| 227 | } |