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 / Confirmation / Confirmation.php

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

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