AtHome::setRequestedDeliveryDate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Bpost\BpostApiClient\Bpost\Order\Box;
5
6
use Bpost\BpostApiClient\Bpost\Order\Receiver;
7
use Bpost\BpostApiClient\Bpost\ProductConfiguration\Product;
8
use Bpost\BpostApiClient\Common\XmlHelper;
9
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
10
use Bpost\BpostApiClient\Exception\XmlException\BpostXmlInvalidItemException;
11
use DOMDocument;
12
use DOMElement;
13
use SimpleXMLElement;
14
15
/**
16
 * bPost AtHome class
17
 *
18
 * @author    Tijs Verkoyen <[email protected]>
19
 *
20
 * @version   3.0.0
21
 *
22
 * @copyright Copyright (c), Tijs Verkoyen. All rights reserved.
23
 * @license   BSD License
24
 */
25
class AtHome extends National
26
{
27
    private ?Receiver $receiver = null;
28
    protected ?string $requestedDeliveryDate = null;
29
30
    /**
31
     * @throws BpostInvalidValueException
32
     */
33
    public function setProduct(string $product): void
34
    {
35
        if (!in_array($product, self::getPossibleProductValues(), true)) {
36
            throw new BpostInvalidValueException('product', $product, self::getPossibleProductValues());
37
        }
38
        parent::setProduct($product);
39
    }
40
41
    public static function getPossibleProductValues(): array
42
    {
43
        return [
44
            Product::PRODUCT_NAME_BPACK_24H_PRO,
45
            Product::PRODUCT_NAME_BPACK_24H_BUSINESS,
46
            Product::PRODUCT_NAME_BPACK_BUSINESS,
47
            Product::PRODUCT_NAME_BPACK_PALLET,
48
            Product::PRODUCT_NAME_BPACK_EASY_RETOUR,
49
        ];
50
    }
51
52
    public function setReceiver(?Receiver $receiver): void
53
    {
54
        $this->receiver = $receiver;
55
    }
56
57
    public function getReceiver(): ?Receiver
58
    {
59
        return $this->receiver;
60
    }
61
62
    public function getRequestedDeliveryDate(): ?string
63
    {
64
        return $this->requestedDeliveryDate;
65
    }
66
67
    public function setRequestedDeliveryDate(?string $requestedDeliveryDate): void
68
    {
69
        $this->requestedDeliveryDate = $requestedDeliveryDate;
70
    }
71
72
    public function toXML(DOMDocument $document, ?string $prefix = null, ?string $type = null): DOMElement
73
    {
74
        $nationalElement = $document->createElement(XmlHelper::getPrefixedTagName('nationalBox', $prefix));
75
        $boxElement = parent::toXML($document, null, 'atHome');
76
        $nationalElement->appendChild($boxElement);
77
78
        if ($this->receiver !== null) {
79
            $boxElement->appendChild($this->receiver->toXML($document));
80
        }
81
82
        $this->addToXmlRequestedDeliveryDate($document, $boxElement);
83
84
        return $nationalElement;
85
    }
86
87
    /**
88
     * @throws \DOMException
89
     */
90
    protected function addToXmlRequestedDeliveryDate(DOMDocument $document, DOMElement $typeElement): void
91
    {
92
        if ($this->requestedDeliveryDate !== null) {
93
            $typeElement->appendChild(
94
                $document->createElement('requestedDeliveryDate', $this->requestedDeliveryDate)
95
            );
96
        }
97
    }
98
99
    public static function createFromXML(SimpleXMLElement $xml, National $self = null): AtHome
100
    {
101
        if ($self === null) {
102
            $self = new self();
103
        }
104
105
        if (!isset($xml->atHome)) {
106
            throw new BpostXmlInvalidItemException();
107
        }
108
109
        $atHomeXml = $xml->atHome[0];
110
111
        /** @var AtHome $self */
112
        $self = parent::createFromXML($atHomeXml, $self);
113
114
        if (isset($atHomeXml->receiver)) {
115
            $self->setReceiver(
116
                Receiver::createFromXML(
117
                    $atHomeXml->receiver->children('http://schema.post.be/shm/deepintegration/v3/common')
118
                )
119
            );
120
        }
121
122
        if (isset($atHomeXml->requestedDeliveryDate) && (string)$atHomeXml->requestedDeliveryDate !== '') {
123
            $self->setRequestedDeliveryDate((string)$atHomeXml->requestedDeliveryDate);
124
        }
125
126
        return $self;
127
    }
128
}
129