PluginProbe
The Innovative Form Builder – IvyForms / 0.2.1
The Innovative Form Builder – IvyForms v0.2.1
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / ValueObjects / Notification / Notification.php

Notification.php in The Innovative Form Builder – IvyForms 0.2.1, at backend/src/ValueObjects/Notification/Notification.php

257 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\ValueObjects\Notification;
4
5 // phpcs:disable PSR1.Files.SideEffects
6 if (!defined('ABSPATH')) {
7 exit; // Exit if accessed directly
8 }
9
10 use IvyForms\Common\Exceptions\InvalidArgumentException;
11 use IvyForms\Common\Exceptions\ValidationException;
12
13 /**
14 * Class Notification
15 *
16 * @package IvyForms\ValueObjects\Notification
17 */
18 final class Notification
19 {
20 /**
21 * @var int
22 */
23 public int $id;
24 /**
25 * @var string
26 */
27 public string $name;
28 /**
29 * @var string
30 */
31 public string $sender;
32 /**
33 * @var string
34 */
35 public string $receiver;
36 /**
37 * @var bool
38 */
39 public bool $enabled;
40 /**
41 * @var string
42 */
43 public string $subject;
44 /**
45 * @var string
46 */
47 public string $message;
48 /**
49 * @var bool
50 */
51 public bool $smartLogic;
52 /**
53 * @var int
54 */
55 public int $formId;
56
57 /**
58 * Form constructor.
59 *
60 * @param int $id
61 * @param string $name
62 * @param string $sender
63 * @param string $receiver
64 * @param bool $enabled
65 * @param string $subject
66 * @param string $message
67 * @param bool $smartLogic
68 * @param int $formId
69 * @throws InvalidArgumentException|ValidationException
70 */
71 public function __construct(
72 int $id,
73 string $name,
74 string $sender,
75 string $receiver,
76 bool $enabled,
77 string $subject,
78 string $message,
79 bool $smartLogic,
80 int $formId
81 ) {
82 $this->id = $this->validateId($id);
83 $this->name = $this->validateString($name, 255, 'name');
84 $this->sender = $this->validateString($sender, 255, 'sender');
85 $this->receiver = $this->validateString($receiver, 255, 'receiver');
86 $this->enabled = $this->validateBool($enabled);
87 $this->subject = $this->validateString($subject, 255, 'subject');
88 $this->message = $this->validateString($message, 1000, 'message');
89 $this->smartLogic = $this->validateBool($smartLogic);
90 $this->formId = $this->validateId($formId);
91 }
92
93 /**
94 * Validates the ID.
95 *
96 * @param int $id
97 *
98 * @return int
99 * @throws InvalidArgumentException
100 */
101 private function validateId(int $id): int
102 {
103 if ($id < 0) {
104 throw new InvalidArgumentException("Id must be a positive integer.");
105 }
106 return $id;
107 }
108
109 /**
110 * Validates a string value.
111 *
112 * @param string $value
113 * @param int $maxLength
114 * @param string $fieldName
115 *
116 * @return string
117 *
118 * @throws ValidationException
119 */
120 private function validateString(string $value, int $maxLength, string $fieldName): string
121 {
122 if (strlen($value) > $maxLength) {
123 throw new ValidationException(
124 sprintf(
125 /* translators: 1: String value, 2: String max length. */
126 esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'),
127 esc_html($fieldName),
128 $maxLength
129 )
130 );
131 }
132 return $value;
133 }
134
135 /**
136 * Validates a boolean field.
137 *
138 * @param bool $value
139 *
140 * @return bool
141 */
142 private function validateBool(bool $value): bool
143 {
144 return $value;
145 }
146
147 /**
148 * Get the notification ID.
149 *
150 * @return int
151 */
152 public function getId(): int
153 {
154 return $this->id;
155 }
156
157 /**
158 * Get the notification name.
159 *
160 * @return string
161 */
162 public function getName(): string
163 {
164 return $this->name;
165 }
166
167 /**
168 * Get the notification sender.
169 *
170 * @return string
171 */
172 public function getSender(): string
173 {
174 return $this->sender;
175 }
176
177 /**
178 * Get the notification receiver.
179 *
180 * @return string
181 */
182 public function getReceiver(): string
183 {
184 return $this->receiver;
185 }
186
187 /**
188 * Get the notification status.
189 *
190 * @return bool
191 */
192 public function isEnabled(): bool
193 {
194 return $this->enabled;
195 }
196
197 /**
198 * Get the notification subject.
199 *
200 * @return string
201 */
202 public function getSubject(): string
203 {
204 return $this->subject;
205 }
206
207 /**
208 * Get the notification message.
209 *
210 * @return string
211 */
212 public function getMessage(): string
213 {
214 return $this->message;
215 }
216
217 /**
218 * Get the smart logic status.
219 *
220 * @return bool
221 */
222 public function isSmartLogic(): bool
223 {
224 return $this->smartLogic;
225 }
226
227 /**
228 * Get the form ID associated with the notification.
229 *
230 * @return int
231 */
232 public function getFormId(): int
233 {
234 return $this->formId;
235 }
236
237 /**
238 * Convert the notification to an array.
239 *
240 * @return array<string, mixed>
241 */
242 public function toArray(): array
243 {
244 return [
245 'id' => $this->getId(),
246 'name' => $this->getName(),
247 'sender' => $this->getSender(),
248 'receiver' => $this->getReceiver(),
249 'enabled' => $this->isEnabled(),
250 'subject' => $this->getSubject(),
251 'message' => $this->getMessage(),
252 'smartLogic' => $this->isSmartLogic(),
253 'formId' => $this->getFormId(),
254 ];
255 }
256 }
257