PluginProbe
The Innovative Form Builder – IvyForms / 0.6.1-backup
The Innovative Form Builder – IvyForms v0.6.1-backup
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.6.1-backup, at backend/src/ValueObjects/Notification/Notification.php

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