Messaging::getLanguage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Bpost\BpostApiClient\Bpost\Order\Box\Option;
5
6
use Bpost\BpostApiClient\Common\XmlHelper;
7
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException;
8
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException;
9
use DOMDocument;
10
use DOMElement;
11
use SimpleXMLElement;
12
13
/**
14
 * bPost Messaging class
15
 *
16
 * @author    Tijs Verkoyen <[email protected]>
17
 *
18
 * @version   3.0.0
19
 *
20
 * @copyright Copyright (c), Tijs Verkoyen. All rights reserved.
21
 * @license   BSD License
22
 */
23
class Messaging extends Option
24
{
25
    public const MESSAGING_LANGUAGE_EN = 'EN';
26
    public const MESSAGING_LANGUAGE_NL = 'NL';
27
    public const MESSAGING_LANGUAGE_FR = 'FR';
28
    public const MESSAGING_LANGUAGE_DE = 'DE';
29
30
    public const MESSAGING_TYPE_INFO_DISTRIBUTED  = 'infoDistributed';
31
    public const MESSAGING_TYPE_INFO_NEXT_DAY     = 'infoNextDay';
32
    public const MESSAGING_TYPE_INFO_REMINDER     = 'infoReminder';
33
    public const MESSAGING_TYPE_KEEP_ME_INFORMED  = 'keepMeInformed';
34
35
    private string $type;
36
    private string $language;
37
    private ?string $emailAddress = null;
38
    private ?string $mobilePhone  = null;
39
40
    /**
41
     * @throws BpostInvalidLengthException
42
     * @throws BpostInvalidValueException
43
     */
44
    public function __construct(string $type, string $language, ?string $emailAddress = null, ?string $mobilePhone = null)
45
    {
46
        $this->setType($type);
47
        $this->setLanguage($language);
48
49
        if ($emailAddress !== null) {
50
            $this->setEmailAddress($emailAddress);
51
        }
52
        if ($mobilePhone !== null) {
53
            $this->setMobilePhone($mobilePhone);
54
        }
55
    }
56
57
    public static function getPossibleLanguageValues(): array
58
    {
59
        return [
60
            self::MESSAGING_LANGUAGE_EN,
61
            self::MESSAGING_LANGUAGE_NL,
62
            self::MESSAGING_LANGUAGE_FR,
63
            self::MESSAGING_LANGUAGE_DE,
64
        ];
65
    }
66
67
    public static function getPossibleTypeValues(): array
68
    {
69
        return [
70
            self::MESSAGING_TYPE_INFO_DISTRIBUTED,
71
            self::MESSAGING_TYPE_INFO_NEXT_DAY,
72
            self::MESSAGING_TYPE_INFO_REMINDER,
73
            self::MESSAGING_TYPE_KEEP_ME_INFORMED,
74
        ];
75
    }
76
77
    /**
78
     * @throws BpostInvalidValueException
79
     */
80
    public function setType(string $type): void
81
    {
82
        if (!in_array($type, self::getPossibleTypeValues(), true)) {
83
            throw new BpostInvalidValueException('type', $type, self::getPossibleTypeValues());
84
        }
85
        $this->type = $type;
86
    }
87
88
    public function getType(): string
89
    {
90
        return $this->type;
91
    }
92
93
    /**
94
     * @throws BpostInvalidValueException
95
     */
96
    public function setLanguage(string $language): void
97
    {
98
        $language = strtoupper($language);
99
        if (!in_array($language, self::getPossibleLanguageValues(), true)) {
100
            throw new BpostInvalidValueException('language', $language, self::getPossibleLanguageValues());
101
        }
102
        $this->language = $language;
103
    }
104
105
    public function getLanguage(): string
106
    {
107
        return $this->language;
108
    }
109
110
    /**
111
     * @throws BpostInvalidLengthException
112
     */
113
    public function setEmailAddress(string $emailAddress): void
114
    {
115
        $length = 50;
116
        if (mb_strlen($emailAddress) > $length) {
117
            throw new BpostInvalidLengthException('emailAddress', mb_strlen($emailAddress), $length);
118
        }
119
        $this->emailAddress = $emailAddress;
120
    }
121
122
    public function getEmailAddress(): ?string
123
    {
124
        return $this->emailAddress;
125
    }
126
127
    /**
128
     * @throws BpostInvalidLengthException
129
     */
130
    public function setMobilePhone(string $mobilePhone): void
131
    {
132
        $length = 20;
133
        if (mb_strlen($mobilePhone) > $length) {
134
            throw new BpostInvalidLengthException('mobilePhone', mb_strlen($mobilePhone), $length);
135
        }
136
        $this->mobilePhone = $mobilePhone;
137
    }
138
139
    public function getMobilePhone(): ?string
140
    {
141
        return $this->mobilePhone;
142
    }
143
144
    /**
145
     * @throws \DOMException
146
     */
147
    public function toXML(DOMDocument $document, ?string $prefix = 'common'): DOMElement
148
    {
149
        $messaging = $document->createElement(XmlHelper::getPrefixedTagName($this->getType(), $prefix));
150
        $messaging->setAttribute('language', $this->getLanguage());
151
152
        if ($this->emailAddress !== null) {
153
            $messaging->appendChild(
154
                $document->createElement(XmlHelper::getPrefixedTagName('emailAddress', $prefix), $this->emailAddress)
155
            );
156
        }
157
        if ($this->mobilePhone !== null) {
158
            $messaging->appendChild(
159
                $document->createElement(XmlHelper::getPrefixedTagName('mobilePhone', $prefix), $this->mobilePhone)
160
            );
161
        }
162
163
        return $messaging;
164
    }
165
166
    /**
167
     * @throws BpostInvalidLengthException
168
     * @throws BpostInvalidValueException
169
     */
170
    public static function createFromXML(SimpleXMLElement $xml): static
171
    {
172
        $messaging = new static($xml->getName(), (string)$xml->attributes()->language);
173
174
        if ((string)$xml->emailAddress !== '') {
175
            $messaging->setEmailAddress((string)$xml->emailAddress);
176
        }
177
        if ((string)$xml->mobilePhone !== '') {
178
            $messaging->setMobilePhone((string)$xml->mobilePhone);
179
        }
180
181
        return $messaging;
182
    }
183
}