1 | <?php |
||
14 | |||
15 | use function array_search; |
||
16 | |||
17 | /** |
||
18 | * Represents an SMS. |
||
19 | */ |
||
20 | class Sms |
||
21 | { |
||
22 | private ?string $sender = null; |
||
|
|||
23 | |||
24 | /** @var string[] */ |
||
25 | private array $recipients = []; |
||
26 | |||
27 | /** @var string[][] */ |
||
28 | private array $recipientVariables = []; |
||
29 | |||
30 | private string $text = ''; |
||
31 | private ?string $userReference = null; |
||
32 | private ?DateTimeInterface $deliveryStart = null; |
||
33 | private ?DateInterval $validityPeriod = null; |
||
34 | private ClockInterface $clock; |
||
35 | |||
36 | final public function __construct(?ClockInterface $clock = null) |
||
37 | { |
||
38 | $this->clock = $clock ?? new SystemClock(); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Creates a new instance of SMS. |
||
43 | * |
||
44 | * @return static |
||
45 | */ |
||
46 | public static function create(): self |
||
47 | { |
||
48 | return new static(); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Gets the sender. |
||
53 | */ |
||
54 | 14 | public function getSender(): ?string |
|
55 | { |
||
56 | 14 | return $this->sender; |
|
57 | 14 | } |
|
58 | 14 | ||
59 | /** |
||
60 | * Sets the sender. |
||
61 | */ |
||
62 | public function setSender(string $sender): self |
||
63 | { |
||
64 | $this->sender = $sender; |
||
65 | 9 | ||
66 | return $this; |
||
67 | 9 | } |
|
68 | |||
69 | /** |
||
70 | * Gets the recipients. |
||
71 | * |
||
72 | * @return string[] |
||
73 | */ |
||
74 | public function getRecipients(): array |
||
75 | 8 | { |
|
76 | return $this->recipients; |
||
77 | 8 | } |
|
78 | |||
79 | /** |
||
80 | * Sets the recipients. |
||
81 | * |
||
82 | * @param string[] $recipients |
||
83 | */ |
||
84 | public function setRecipients(array $recipients): self |
||
85 | { |
||
86 | $this->recipients = $recipients; |
||
87 | 1 | ||
88 | return $this; |
||
89 | 1 | } |
|
90 | |||
91 | 1 | /** |
|
92 | * Adds a single recipient. |
||
93 | */ |
||
94 | public function addRecipient(string $recipient): self |
||
95 | { |
||
96 | $this->recipients[] = $recipient; |
||
97 | |||
98 | return $this; |
||
99 | 9 | } |
|
100 | |||
101 | 9 | /** |
|
102 | * Removes a single recipient. |
||
103 | */ |
||
104 | public function removeRecipient(string $recipient): self |
||
105 | { |
||
106 | $itemPosition = array_search($recipient, $this->recipients, true); |
||
107 | if ($itemPosition !== false) { |
||
108 | unset($this->recipients[$itemPosition]); |
||
109 | } |
||
110 | |||
111 | 8 | unset($this->recipientVariables[$recipient]); |
|
112 | |||
113 | 8 | return $this; |
|
114 | } |
||
115 | 8 | ||
116 | /** |
||
117 | * Whether the current sms has or not recipients. |
||
118 | */ |
||
119 | public function hasRecipients(): bool |
||
120 | { |
||
121 | return ! empty($this->recipients); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | 6 | * Gets the recipient variables. |
|
126 | * |
||
127 | 6 | * @return string[][] |
|
128 | */ |
||
129 | 6 | public function getRecipientVariables(): array |
|
130 | { |
||
131 | return $this->recipientVariables; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Sets the recipient variables for the recipient specified. |
||
136 | * |
||
137 | * @param string[] $recipientVariables |
||
138 | */ |
||
139 | 1 | public function setRecipientVariables(string $recipient, array $recipientVariables): self |
|
140 | { |
||
141 | 1 | $this->recipientVariables[$recipient] = $recipientVariables; |
|
142 | |||
143 | 1 | return $this; |
|
144 | 1 | } |
|
145 | |||
146 | /** |
||
147 | 1 | * Adds a single recipient variable for the specified recipient. |
|
148 | */ |
||
149 | 1 | public function addRecipientVariable( |
|
150 | string $recipient, |
||
151 | string $recipientVariable, |
||
152 | string $recipientVariableValue |
||
153 | ): self { |
||
154 | if (! isset($this->recipientVariables[$recipient])) { |
||
155 | $this->recipientVariables[$recipient] = []; |
||
156 | } |
||
157 | 9 | ||
158 | $this->recipientVariables[$recipient][$recipientVariable] = $recipientVariableValue; |
||
159 | 9 | ||
160 | return $this; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Removes the recipient variable for the recipient specified. |
||
165 | */ |
||
166 | public function removeRecipientVariable(string $recipient, string $recipientVariable): self |
||
167 | 10 | { |
|
168 | unset($this->recipientVariables[$recipient][$recipientVariable]); |
||
169 | 10 | ||
170 | return $this; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Whether the current sms has or not recipient variables. |
||
175 | */ |
||
176 | public function hasRecipientVariables(): bool |
||
177 | { |
||
178 | return ! empty($this->recipientVariables); |
||
179 | } |
||
180 | 1 | ||
181 | /** |
||
182 | 1 | * Clears the recipient variables. |
|
183 | */ |
||
184 | 1 | public function clearRecipientVariables(): self |
|
185 | { |
||
186 | $this->recipientVariables = []; |
||
187 | |||
188 | return $this; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Gets the text. |
||
193 | */ |
||
194 | public function getText(): string |
||
195 | { |
||
196 | 5 | return $this->text; |
|
197 | } |
||
198 | |||
199 | /** |
||
200 | * Sets the text. |
||
201 | 5 | */ |
|
202 | 5 | public function setText(string $text): self |
|
203 | { |
||
204 | $this->text = $text; |
||
205 | 5 | ||
206 | return $this; |
||
207 | 5 | } |
|
208 | |||
209 | /** |
||
210 | * Gets the user reference. |
||
211 | */ |
||
212 | public function getUserReference(): ?string |
||
213 | { |
||
214 | return $this->userReference; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | 1 | * Sets the user reference. |
|
219 | */ |
||
220 | 1 | public function setUserReference(string $userReference): self |
|
221 | { |
||
222 | 1 | $this->userReference = $userReference; |
|
223 | |||
224 | return $this; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Gets the delivery start. |
||
229 | */ |
||
230 | 8 | public function getDeliveryStart(): ?DateTimeInterface |
|
231 | { |
||
232 | 8 | return $this->deliveryStart; |
|
233 | } |
||
234 | |||
235 | /** |
||
236 | * @throws InvalidDeliveryStartException |
||
237 | */ |
||
238 | public function setDeliveryStart(?DateTimeInterface $deliveryStart = null): self |
||
239 | { |
||
240 | 8 | if ($deliveryStart !== null && $deliveryStart < $this->clock->now()) { |
|
241 | throw new InvalidDeliveryStartException(); |
||
242 | 8 | } |
|
243 | |||
244 | 8 | $this->deliveryStart = $deliveryStart; |
|
245 | |||
246 | return $this; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Gets the validity period. |
||
251 | */ |
||
252 | 8 | public function getValidityPeriod(): ?DateInterval |
|
253 | { |
||
254 | 8 | return $this->validityPeriod; |
|
255 | } |
||
256 | |||
257 | /** |
||
258 | * Sets the validity period. |
||
259 | * |
||
260 | * @throws InvalidValidityPeriodException |
||
261 | */ |
||
262 | public function setValidityPeriod(?DateInterval $validityPeriod = null): self |
||
263 | { |
||
264 | 9 | if ( |
|
265 | $validityPeriod !== null && |
||
266 | 9 | ($validityPeriod->i < ValidityPeriods::MIN || $validityPeriod->i > ValidityPeriods::MAX) |
|
267 | ) { |
||
268 | 9 | throw new InvalidValidityPeriodException(); |
|
269 | } |
||
270 | |||
271 | $this->validityPeriod = $validityPeriod; |
||
272 | |||
273 | return $this; |
||
274 | } |
||
275 | } |
||
276 |