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

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