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

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