PluginProbe
The Innovative Form Builder – IvyForms / 0.3
The Innovative Form Builder – IvyForms v0.3
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 / Entry / EntryField.php

EntryField.php in The Innovative Form Builder – IvyForms 0.3, at backend/src/ValueObjects/Entry/EntryField.php

154 lines 3.2 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\Entry;
4
5 // phpcs:disable PSR1.Files.SideEffects
6 use IvyForms\Common\Exceptions\ValidationException;
7
8 if (!defined('ABSPATH')) {
9 exit; // Exit if accessed directly
10 }
11
12 /**
13 * Class EntryField
14 *
15 * Represents a single field value in a form entry.
16 */
17 final class EntryField
18 {
19 /**
20 * @var int|null
21 */
22 public ?int $id;
23 /**
24 * @var int
25 */
26 public int $fieldId;
27 /**
28 * @var string
29 */
30 public string $fieldValue;
31 /**
32 * @var int
33 */
34 public int $entryId;
35
36 /**
37 * EntryField constructor.
38 *
39 * @param int|null $id
40 * @param int $entryId
41 * @param int $fieldId
42 * @param string $fieldValue
43 * @throws ValidationException
44 */
45 public function __construct(
46 ?int $id,
47 int $entryId,
48 int $fieldId,
49 string $fieldValue
50 ) {
51 $this->id = $id ? $this->validateId($id) : null;
52 $this->entryId = $this->validateId($entryId);
53 $this->fieldId = $this->validateId($fieldId);
54 $this->fieldValue = $this->validateString($fieldValue, 65535, 'fieldValue');
55 }
56
57 /**
58 * Get the ID of the entry field.
59 *
60 * @return int|null The entry field ID.
61 */
62 public function getId(): ?int
63 {
64 return $this->id;
65 }
66
67 /**
68 * Get the field ID.
69 *
70 * @return int The field ID.
71 */
72 public function getFieldId(): int
73 {
74 return $this->fieldId;
75 }
76
77 /**
78 * Get the field value.
79 *
80 * @return string The field value.
81 */
82 public function getFieldValue(): string
83 {
84 return $this->fieldValue;
85 }
86
87 /**
88 * Get the entry ID.
89 *
90 * @return int The entry ID or null if not set.
91 */
92 public function getEntryId(): int
93 {
94 return $this->entryId;
95 }
96
97 /**
98 * Validates the ID.
99 *
100 * @param int $id
101 *
102 * @return int
103 * @throws ValidationException
104 */
105 private function validateId(int $id): int
106 {
107 if ($id < 0) {
108 throw new ValidationException("Id must be a positive integer.");
109 }
110 return $id;
111 }
112
113 /**
114 * Validates a string value.
115 *
116 * @param string $value
117 * @param int $maxLength
118 * @param string $fieldName
119 *
120 * @return string
121 *
122 * @throws ValidationException
123 */
124 private function validateString(string $value, int $maxLength, string $fieldName): string
125 {
126 if (strlen($value) > $maxLength) {
127 throw new ValidationException(
128 sprintf(
129 /* translators: 1: String value, 2: String max length. */
130 esc_html__('%1$s must be at most %2$d characters.', 'ivyforms'),
131 esc_html($fieldName),
132 $maxLength
133 )
134 );
135 }
136 return $value;
137 }
138
139 /**
140 * Convert the EntryField object to an array.
141 *
142 * @return array<mixed> The array representation of the EntryField.
143 */
144 public function toArray(): array
145 {
146 return [
147 'id' => $this->getId(),
148 'entryId' => $this->getEntryId(),
149 'fieldId' => $this->getFieldId(),
150 'fieldValue' => $this->getFieldValue(),
151 ];
152 }
153 }
154