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

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