PluginProbe
The Innovative Form Builder – IvyForms / 1.1
The Innovative Form Builder – IvyForms v1.1
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 / Confirmation / Confirmation.php

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

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