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

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