PluginProbe
The Innovative Form Builder – IvyForms / 0.8
The Innovative Form Builder – IvyForms v0.8
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 1.2 1.3
ivyforms / backend / src / ValueObjects / FieldOptions / FieldOptions.php

FieldOptions.php in The Innovative Form Builder – IvyForms 0.8, at backend/src/ValueObjects/FieldOptions/FieldOptions.php

187 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * @copyright © Melograno Venture Studio. All rights.
5 * @licence See LICENCE.md for license details.
6 */
7
8 namespace IvyForms\ValueObjects\FieldOptions;
9
10 use IvyForms\Common\Exceptions\ValidationException;
11 use IvyForms\Services\Translations\BackendStrings;
12
13 // phpcs:disable PSR1.Files.SideEffects
14 if (!defined('ABSPATH')) {
15 exit; // Exit if accessed directly
16 }
17
18 final class FieldOptions
19 {
20 /**
21 * @var int
22 */
23 public int $id;
24
25 /**
26 * @var int
27 */
28 public int $fieldId;
29
30 /**
31 * @var string
32 */
33 public string $label;
34
35 /**
36 * @var string
37 */
38 public string $value;
39
40 /**
41 * @var bool
42 */
43 public bool $isDefault;
44
45 /**
46 * @var int
47 */
48 public int $position;
49
50 /**
51 * @throws ValidationException
52 */
53 public function __construct(
54 int $id,
55 int $fieldId,
56 string $label,
57 string $value,
58 bool $isDefault,
59 int $position
60 ) {
61 $this->id = $this->validatePositiveInteger($id);
62 $this->fieldId = $this->validatePositiveInteger($fieldId);
63 $this->label = $this->validateString($label, 255, 'label');
64 $this->value = $this->validateString($value, 255, 'value');
65 $this->isDefault = $this->validateBool($isDefault);
66 $this->position = $this->validatePositiveInteger($position);
67 }
68
69 /**
70 * Validates the ID.
71 *
72 * @param int $id
73 *
74 * @return int
75 * @throws ValidationException
76 */
77 private function validatePositiveInteger(int $id): int
78 {
79 if ($id < 0) {
80 throw new ValidationException(BackendStrings::getExceptionStrings()['property_positive_integer']);
81 }
82 return $id;
83 }
84
85 /**
86 * Validates a string value.
87 *
88 * @param string $value
89 * @param int $maxLength
90 * @param string $fieldName
91 *
92 * @return string
93 *
94 * @throws ValidationException
95 */
96 private function validateString(string $value, int $maxLength, string $fieldName): string
97 {
98 if (strlen($value) > $maxLength) {
99 throw new ValidationException(
100 sprintf(
101 /* translators: 1: String value, 2: String max length. */
102 esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'),
103 esc_html($fieldName),
104 $maxLength
105 )
106 );
107 }
108 return $value;
109 }
110
111 /**
112 * Validates a boolean field.
113 *
114 * @param bool $value
115 *
116 * @return bool
117 */
118 private function validateBool(bool $value): bool
119 {
120 return $value;
121 }
122
123 /**
124 * @return int
125 */
126 public function getId(): int
127 {
128 return $this->id;
129 }
130
131 /**
132 * @return int
133 */
134 public function getFieldId(): int
135 {
136 return $this->fieldId;
137 }
138
139 /**
140 * @return string
141 */
142 public function getLabel(): string
143 {
144 return $this->label;
145 }
146
147 /**
148 * @return string
149 */
150 public function getValue(): string
151 {
152 return $this->value;
153 }
154
155 /**
156 * @return bool
157 */
158 public function isDefault(): bool
159 {
160 return $this->isDefault;
161 }
162
163 /**
164 * @return int
165 */
166 public function getPosition(): int
167 {
168 return $this->position;
169 }
170
171
172 /**
173 * @return array<string, int|string|bool>
174 */
175 public function toArray(): array
176 {
177 return [
178 'id' => $this->getId(),
179 'fieldId' => $this->getFieldId(),
180 'label' => $this->getLabel(),
181 'value' => $this->getValue(),
182 'isDefault' => $this->isDefault(),
183 'position' => $this->getPosition(),
184 ];
185 }
186 }
187