Debian Codegraph @

vendor/ipl/web/src/FormElement/TermInput/RegisteredTerm.php

raw
1 <?php
2
3 namespace ipl\Web\FormElement\TermInput;
4
5 class RegisteredTerm implements Term
6 {
7 /** @var string The search value */
8 protected $value;
9
10 /** @var ?string The label */
11 protected $label;
12
13 /** @var ?string The CSS class */
14 protected $class;
15
16 /** @var string The failure message */
17 protected $message;
18
19 /** @var string The validation constraint */
20 protected $pattern;
21
22 /**
23 * Create a new RegisteredTerm
24 *
25 * @param string $value The search value
26 */
27 public function __construct(string $value)
28 {
29 $this->setSearchValue($value);
30 }
31
32 public function setSearchValue(string $value): self
33 {
34 $this->value = $value;
35
36 return $this;
37 }
38
39 public function getSearchValue(): string
40 {
41 return $this->value;
42 }
43
44 public function setLabel(string $label): self
45 {
46 $this->label = $label;
47
48 return $this;
49 }
50
51 public function getLabel(): ?string
52 {
53 return $this->label;
54 }
55
56 public function setClass(string $class): self
57 {
58 $this->class = $class;
59
60 return $this;
61 }
62
63 public function getClass(): ?string
64 {
65 return $this->class;
66 }
67
68 public function setMessage(string $message): self
69 {
70 $this->message = $message;
71
72 return $this;
73 }
74
75 public function getMessage(): ?string
76 {
77 return $this->message;
78 }
79
80 public function setPattern(string $pattern): self
81 {
82 $this->pattern = $pattern;
83
84 return $this;
85 }
86
87 public function getPattern(): ?string
88 {
89 if ($this->message === null) {
90 return null;
91 }
92
93 return $this->pattern ?? sprintf(Term::DEFAULT_CONSTRAINT, $this->getLabel() ?? $this->getSearchValue());
94 }
95
96 /**
97 * Render this term as a string
98 *
99 * Pass the separator being used to separate multiple terms. If the term's value contains it,
100 * the result will be automatically quoted.
101 *
102 * @param string $separator
103 *
104 * @return string
105 */
106 public function render(string $separator): string
107 {
108 if (strpos($this->value, $separator) !== false) {
109 return '"' . $this->value . '"';
110 }
111
112 return $this->value;
113 }
114
115 /**
116 * Apply the given term data to this term
117 *
118 * @param array $termData
119 *
120 * @return void
121 */
122 public function applyTermData(array $termData): void
123 {
124 if (isset($termData['search'])) {
125 $this->value = $termData['search'];
126 }
127
128 if (isset($termData['label'])) {
129 $this->setLabel($termData['label']);
130 }
131
132 if (isset($termData['class'])) {
133 $this->setClass($termData['class']);
134 }
135
136 if (isset($termData['invalidMsg'])) {
137 $this->setMessage($termData['invalidMsg']);
138 }
139
140 if (isset($termData['pattern'])) {
141 $this->setPattern($termData['pattern']);
142 }
143 }
144 }