PluginProbe
The Innovative Form Builder – IvyForms / 0.5
The Innovative Form Builder – IvyForms v0.5
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 / Form / Form.php

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

330 lines 7.8 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\Form;
4
5 // phpcs:disable PSR1.Files.SideEffects
6 if (!defined('ABSPATH')) {
7 exit; // Exit if accessed directly
8 }
9
10 use IvyForms\Common\Exceptions\ValidationException;
11 use IvyForms\Services\Translations\BackendStrings;
12 use IvyForms\ValueObjects\Form\IntegrationSettings;
13
14 /**
15 * Class Form
16 *
17 * @package IvyForms\ValueObjects\Form
18 */
19 final class Form
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 $author;
33 /**
34 * @var bool
35 */
36 public bool $published;
37 /**
38 * @var bool
39 */
40 public bool $starred;
41 /**
42 * @var string
43 */
44 public string $dateCreated;
45 /**
46 * @var string
47 */
48 public string $dateEdited;
49 /**
50 * @var array<int, array<string, mixed>>|null
51 */
52 public ?array $fields;
53 /**
54 * @var string
55 */
56 public string $description;
57 /**
58 * @var bool
59 */
60 public bool $showTitle;
61 /**
62 * @var bool
63 */
64 public bool $showDescription;
65 /**
66 * @var bool
67 */
68 public bool $storeEntries;
69 /**
70 * @var IntegrationSettings|null
71 */
72 public ?IntegrationSettings $integrationSettings;
73
74 /**
75 * Form constructor.
76 *
77 * @param int $id
78 * @param string $name
79 * @param string $author
80 * @param bool $starred
81 * @param bool $published
82 * @param string $description
83 * @param bool $showTitle
84 * @param bool $showDescription
85 * @param bool $storeEntries
86 * @param array<int, array<string, mixed>> $fields
87 * @param IntegrationSettings|null $integrationSettings
88 * @throws ValidationException
89 * @SuppressWarnings("ExcessiveParameterList")
90 */
91 public function __construct(
92 int $id,
93 string $name,
94 string $author,
95 bool $starred,
96 bool $published,
97 string $description,
98 bool $showTitle,
99 bool $showDescription,
100 bool $storeEntries,
101 array $fields = [],
102 IntegrationSettings $integrationSettings = null
103 ) {
104 $this->id = $this->validateId($id);
105 $this->name = $this->validateString($name, 255, 'name');
106 $this->author = $this->validateString($author, 1000, 'author');
107 $this->starred = $this->validateBool($starred);
108 $this->published = $this->validateBool($published);
109 $this->dateCreated = gmdate('Y-m-d H:i:s');
110 $this->dateEdited = gmdate('Y-m-d H:i:s');
111 $this->fields = $fields;
112 $this->description = $this->validateString($description, 1000, 'description');
113 $this->showTitle = $this->validateBool($showTitle);
114 $this->showDescription = $this->validateBool($showDescription);
115 $this->storeEntries = $this->validateBool($storeEntries);
116 $this->integrationSettings = $integrationSettings;
117 }
118
119 /**
120 * Validates the ID.
121 *
122 * @param int $id
123 *
124 * @return int
125 * @throws ValidationException
126 */
127 private function validateId(int $id): int
128 {
129 if ($id < 0) {
130 throw new ValidationException(BackendStrings::getExceptionStrings()['id_positive_integer']);
131 }
132 return $id;
133 }
134
135 /**
136 * Validates a string value.
137 *
138 * @param string $value
139 * @param int $maxLength
140 * @param string $fieldName
141 *
142 * @return string
143 *
144 * @throws ValidationException
145 */
146 private function validateString(string $value, int $maxLength, string $fieldName): string
147 {
148 if (strlen($value) > $maxLength) {
149 throw new ValidationException(
150 sprintf(
151 /* translators: 1: String value, 2: String max length. */
152 esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'),
153 esc_html($fieldName),
154 $maxLength
155 )
156 );
157 }
158 return $value;
159 }
160
161 /**
162 * Validates a boolean field.
163 *
164 * @param bool $value
165 *
166 * @return bool
167 */
168 private function validateBool(bool $value): bool
169 {
170 return $value;
171 }
172
173 /**
174 * Get the form ID.
175 *
176 * @return int
177 */
178 public function getId(): int
179 {
180 return $this->id;
181 }
182
183 /**
184 * Get the form name.
185 *
186 * @return string
187 */
188 public function getName(): string
189 {
190 return $this->name;
191 }
192
193 /**
194 * Get the form author.
195 *
196 * @return string
197 */
198 public function getAuthor(): string
199 {
200 return $this->author;
201 }
202
203 /**
204 * Get the form starred status.
205 *
206 * @return bool
207 */
208 public function isStarred(): bool
209 {
210 return $this->starred;
211 }
212 /**
213 * Get the form published status.
214 *
215 * @return bool
216 */
217 public function isPublished(): bool
218 {
219 return $this->published;
220 }
221 /**
222 * Get the date of form creation.
223 *
224 * @return string
225 */
226 public function getDateCreated(): string
227 {
228 return $this->dateCreated;
229 }
230 /**
231 * Get the form date edited.
232 *
233 * @return string
234 */
235 public function getDateEdited(): string
236 {
237 return $this->dateEdited;
238 }
239 /**
240 * Get the form fields.
241 *
242 * @return array<int, array<string, mixed>>|null
243 */
244 public function getFields(): ?array
245 {
246 return $this->fields;
247 }
248 /**
249 * Get the form description.
250 *
251 * @return string
252 */
253 public function getDescription(): string
254 {
255 return $this->description;
256 }
257 /**
258 * Get the show description of the form.
259 *
260 * @return bool The form show description status.
261 */
262 public function isDescriptionVisible(): bool
263 {
264 return $this->showDescription;
265 }
266 /**
267 * Get the form store entries' status.
268 * @return bool
269 */
270 public function isStoreEntries(): bool
271 {
272 return $this->storeEntries;
273 }
274
275 /**
276 * Get integration settings.
277 * @return IntegrationSettings|null
278 */
279 public function getIntegrationSettings(): ?IntegrationSettings
280 {
281 return $this->integrationSettings;
282 }
283
284 /**
285 * Set integration settings.
286 * @param IntegrationSettings $settings
287 */
288 public function setIntegrationSettings(IntegrationSettings $settings): void
289 {
290 $this->integrationSettings = $settings;
291 }
292
293 /**
294 * Get the show title of the form.
295 *
296 * @return bool The form show title status.
297 */
298 public function isTitleVisible(): bool
299 {
300 return $this->showTitle;
301 }
302
303 /**
304 * Convert the form to an array.
305 *
306 * @return array<string, mixed>
307 */
308 public function toArray(): array
309 {
310 $data = array_merge(
311 [
312 'id' => $this->getId(),
313 'name' => $this->getName(),
314 'author' => $this->getAuthor(),
315 'starred' => $this->isStarred(),
316 'published' => $this->isPublished(),
317 'dateCreated' => $this->getDateCreated(),
318 'dateEdited' => $this->getDateEdited(),
319 'fields' => $this->getFields(),
320 'description' => $this->getDescription(),
321 'showTitle' => $this->isTitleVisible(),
322 'showDescription' => $this->isDescriptionVisible(),
323 'storeEntries' => $this->isStoreEntries(),
324 ],
325 $this->getIntegrationSettings()->toArray()
326 );
327 return $data;
328 }
329 }
330