Debian Codegraph @

vendor/ipl/web/src/Control/SearchBar/ValidatedTerm.php

raw
1 <?php
2
3 namespace ipl\Web\Control\SearchBar;
4
5 use ipl\Stdlib\Data;
6
7 abstract class ValidatedTerm
8 {
9 /**
10 * The default validation constraint
11 *
12 * Forbids what's inside an input unless non-whitespace chars are prepended or appended.
13 *
14 * @var string
15 */
16 public const DEFAULT_PATTERN = '^(?!\s*%s\s*$).*$';
17
18 /** @var string The search value */
19 protected $searchValue;
20
21 /** @var ?string The label */
22 protected $label;
23
24 /** @var ?string The validation message */
25 protected $message;
26
27 /** @var ?string The validation constraint */
28 protected $pattern;
29
30 /** @var bool Whether the term has been adjusted */
31 protected $changed = false;
32
33 /**
34 * Create a new ValidatedTerm
35 *
36 * @param string $searchValue The search value
37 * @param ?string $label The label
38 */
39 public function __construct(string $searchValue, ?string $label = null)
40 {
41 $this->searchValue = $searchValue;
42 $this->label = $label;
43 }
44
45 /**
46 * Create a new ValidatedTerm from the given data
47 *
48 * @param array $data
49 *
50 * @return static
51 */
52 public static function fromTermData(array $data)
53 {
54 return new static($data['search'], isset($data['label']) ? $data['label'] : null);
55 }
56
57 /**
58 * Check whether the term is valid
59 *
60 * @return bool
61 */
62 public function isValid()
63 {
64 return $this->message === null;
65 }
66
67 /**
68 * Check whether the term has been adjusted
69 *
70 * @return bool
71 */
72 public function hasBeenChanged()
73 {
74 return $this->changed;
75 }
76
77 /**
78 * Get the search value
79 *
80 * @return string
81 */
82 public function getSearchValue(): string
83 {
84 return $this->searchValue;
85 }
86
87 /**
88 * Set the search value
89 *
90 * @param string $searchValue
91 *
92 * @return $this
93 */
94 public function setSearchValue(string $searchValue): self
95 {
96 $this->searchValue = $searchValue;
97 $this->changed = true;
98
99 return $this;
100 }
101
102 /**
103 * Get the label
104 *
105 * @return string
106 */
107 public function getLabel(): ?string
108 {
109 return $this->label;
110 }
111
112 /**
113 * Set the label
114 *
115 * @param ?string $label
116 *
117 * @return $this
118 */
119 public function setLabel(?string $label): self
120 {
121 $this->label = $label;
122 $this->changed = true;
123
124 return $this;
125 }
126
127 /**
128 * Get the validation message
129 *
130 * @return ?string
131 */
132 public function getMessage(): ?string
133 {
134 return $this->message;
135 }
136
137 /**
138 * Set the validation message
139 *
140 * @param string $message
141 *
142 * @return $this
143 */
144 public function setMessage(string $message): self
145 {
146 $this->message = $message;
147
148 return $this;
149 }
150
151 /**
152 * Get the validation constraint
153 *
154 * Returns the default constraint if none is set.
155 *
156 * @return string
157 */
158 public function getPattern(): ?string
159 {
160 if ($this->message === null) {
161 return null;
162 }
163
164 return $this->pattern ?? sprintf(
165 static::DEFAULT_PATTERN,
166 static::escapeForHTMLPattern($this->getLabel() ?: $this->getSearchValue())
167 );
168 }
169
170 /**
171 * Set the validation constraint
172 *
173 * @param string $pattern
174 *
175 * @return $this
176 */
177 public function setPattern(string $pattern): self
178 {
179 $this->pattern = $pattern;
180
181 return $this;
182 }
183
184 /**
185 * Escape the given subject for usage inside an HTML pattern attribute
186 *
187 * @param string $subject
188 *
189 * @return string
190 */
191 public static function escapeForHTMLPattern(string $subject): string
192 {
193 return preg_quote($subject);
194 }
195
196 /**
197 * Get this term's data
198 *
199 * @return array
200 */
201 public function toTermData()
202 {
203 return [
204 'search' => $this->getSearchValue(),
205 'label' => $this->getLabel() ?: $this->getSearchValue(),
206 'invalidMsg' => $this->getMessage(),
207 'pattern' => $this->getPattern()
208 ];
209 }
210
211 /**
212 * Get this term's metadata
213 *
214 * @return Data
215 */
216 abstract public function toMetaData();
217 }