Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
3 | namespace Civi\Api4\Service\Spec; | |
4 | ||
5 | use Civi\Api4\Utils\CoreUtil; | |
6 | ||
7 | class FieldSpec { | |
8 | /** | |
9 | * @var mixed | |
10 | */ | |
11 | protected $defaultValue; | |
12 | ||
13 | /** | |
14 | * @var string | |
15 | */ | |
16 | protected $name; | |
17 | ||
18 | /** | |
19 | * @var string | |
20 | */ | |
21 | protected $title; | |
22 | ||
23 | /** | |
24 | * @var string | |
25 | */ | |
26 | protected $entity; | |
27 | ||
28 | /** | |
29 | * @var string | |
30 | */ | |
31 | protected $description; | |
32 | ||
33 | /** | |
34 | * @var bool | |
35 | */ | |
36 | protected $required = FALSE; | |
37 | ||
38 | /** | |
39 | * @var bool | |
40 | */ | |
41 | protected $requiredIf; | |
42 | ||
43 | /** | |
44 | * @var array|boolean | |
45 | */ | |
46 | protected $options; | |
47 | ||
48 | /** | |
49 | * @var string | |
50 | */ | |
51 | protected $dataType; | |
52 | ||
53 | /** | |
54 | * @var string | |
55 | */ | |
56 | protected $inputType; | |
57 | ||
58 | /** | |
59 | * @var array | |
60 | */ | |
61 | protected $inputAttrs = []; | |
62 | ||
63 | /** | |
64 | * @var string | |
65 | */ | |
66 | protected $fkEntity; | |
67 | ||
68 | /** | |
69 | * @var int | |
70 | */ | |
71 | protected $serialize; | |
72 | ||
73 | /** | |
74 | * Aliases for the valid data types | |
75 | * | |
76 | * @var array | |
77 | */ | |
78 | public static $typeAliases = [ | |
79 | 'Int' => 'Integer', | |
80 | 'Link' => 'Url', | |
81 | 'Memo' => 'Text', | |
82 | ]; | |
83 | ||
84 | /** | |
85 | * @param string $name | |
86 | * @param string $entity | |
87 | * @param string $dataType | |
88 | */ | |
89 | public function __construct($name, $entity, $dataType = 'String') { | |
90 | $this->entity = $entity; | |
91 | $this->setName($name); | |
92 | $this->setDataType($dataType); | |
93 | } | |
94 | ||
95 | /** | |
96 | * @return mixed | |
97 | */ | |
98 | public function getDefaultValue() { | |
99 | return $this->defaultValue; | |
100 | } | |
101 | ||
102 | /** | |
103 | * @param mixed $defaultValue | |
104 | * | |
105 | * @return $this | |
106 | */ | |
107 | public function setDefaultValue($defaultValue) { | |
108 | $this->defaultValue = $defaultValue; | |
109 | ||
110 | return $this; | |
111 | } | |
112 | ||
113 | /** | |
114 | * @return string | |
115 | */ | |
116 | public function getName() { | |
117 | return $this->name; | |
118 | } | |
119 | ||
120 | /** | |
121 | * @param string $name | |
122 | * | |
123 | * @return $this | |
124 | */ | |
125 | public function setName($name) { | |
126 | $this->name = $name; | |
127 | ||
128 | return $this; | |
129 | } | |
130 | ||
131 | /** | |
132 | * @return string | |
133 | */ | |
134 | public function getTitle() { | |
135 | return $this->title; | |
136 | } | |
137 | ||
138 | /** | |
139 | * @param string $title | |
140 | * | |
141 | * @return $this | |
142 | */ | |
143 | public function setTitle($title) { | |
144 | $this->title = $title; | |
145 | ||
146 | return $this; | |
147 | } | |
148 | ||
149 | /** | |
150 | * @return string | |
151 | */ | |
152 | public function getEntity() { | |
153 | return $this->entity; | |
154 | } | |
155 | ||
156 | /** | |
157 | * @return string | |
158 | */ | |
159 | public function getDescription() { | |
160 | return $this->description; | |
161 | } | |
162 | ||
163 | /** | |
164 | * @param string $description | |
165 | * | |
166 | * @return $this | |
167 | */ | |
168 | public function setDescription($description) { | |
169 | $this->description = $description; | |
170 | ||
171 | return $this; | |
172 | } | |
173 | ||
174 | /** | |
175 | * @return bool | |
176 | */ | |
177 | public function isRequired() { | |
178 | return $this->required; | |
179 | } | |
180 | ||
181 | /** | |
182 | * @param bool $required | |
183 | * | |
184 | * @return $this | |
185 | */ | |
186 | public function setRequired($required) { | |
187 | $this->required = $required; | |
188 | ||
189 | return $this; | |
190 | } | |
191 | ||
192 | /** | |
193 | * @return bool | |
194 | */ | |
195 | public function getRequiredIf() { | |
196 | return $this->requiredIf; | |
197 | } | |
198 | ||
199 | /** | |
200 | * @param bool $requiredIf | |
201 | * | |
202 | * @return $this | |
203 | */ | |
204 | public function setRequiredIf($requiredIf) { | |
205 | $this->requiredIf = $requiredIf; | |
206 | ||
207 | return $this; | |
208 | } | |
209 | ||
210 | /** | |
211 | * @return string | |
212 | */ | |
213 | public function getDataType() { | |
214 | return $this->dataType; | |
215 | } | |
216 | ||
217 | /** | |
218 | * @param $dataType | |
219 | * | |
220 | * @return $this | |
221 | * @throws \Exception | |
222 | */ | |
223 | public function setDataType($dataType) { | |
224 | if (array_key_exists($dataType, self::$typeAliases)) { | |
225 | $dataType = self::$typeAliases[$dataType]; | |
226 | } | |
227 | ||
228 | if (!in_array($dataType, $this->getValidDataTypes())) { | |
229 | throw new \Exception(sprintf('Invalid data type "%s', $dataType)); | |
230 | } | |
231 | ||
232 | $this->dataType = $dataType; | |
233 | ||
234 | return $this; | |
235 | } | |
236 | ||
237 | /** | |
238 | * @return int | |
239 | */ | |
240 | public function getSerialize() { | |
241 | return $this->serialize; | |
242 | } | |
243 | ||
244 | /** | |
245 | * @param int|null $serialize | |
246 | * @return $this | |
247 | */ | |
248 | public function setSerialize($serialize) { | |
249 | $this->serialize = $serialize; | |
250 | ||
251 | return $this; | |
252 | } | |
253 | ||
254 | /** | |
255 | * @return string | |
256 | */ | |
257 | public function getInputType() { | |
258 | return $this->inputType; | |
259 | } | |
260 | ||
261 | /** | |
262 | * @param string $inputType | |
263 | * @return $this | |
264 | */ | |
265 | public function setInputType($inputType) { | |
266 | $this->inputType = $inputType; | |
267 | ||
268 | return $this; | |
269 | } | |
270 | ||
271 | /** | |
272 | * @return array | |
273 | */ | |
274 | public function getInputAttrs() { | |
275 | return $this->inputAttrs; | |
276 | } | |
277 | ||
278 | /** | |
279 | * @param array $inputAttrs | |
280 | * @return $this | |
281 | */ | |
282 | public function setInputAttrs($inputAttrs) { | |
283 | $this->inputAttrs = $inputAttrs; | |
284 | ||
285 | return $this; | |
286 | } | |
287 | ||
288 | /** | |
289 | * Add valid types that are not not part of \CRM_Utils_Type::dataTypes | |
290 | * | |
291 | * @return array | |
292 | */ | |
293 | private function getValidDataTypes() { | |
294 | $extraTypes = ['Boolean', 'Text', 'Float', 'Url', 'Array']; | |
295 | $extraTypes = array_combine($extraTypes, $extraTypes); | |
296 | ||
297 | return array_merge(\CRM_Utils_Type::dataTypes(), $extraTypes); | |
298 | } | |
299 | ||
300 | /** | |
301 | * @return array | |
302 | */ | |
303 | public function getOptions() { | |
304 | if (!isset($this->options) || $this->options === TRUE) { | |
305 | $fieldName = $this->getName(); | |
306 | ||
307 | if ($this instanceof CustomFieldSpec) { | |
308 | // buildOptions relies on the custom_* type of field names | |
309 | $fieldName = sprintf('custom_%d', $this->getCustomFieldId()); | |
310 | } | |
311 | ||
312 | $bao = CoreUtil::getBAOFromApiName($this->getEntity()); | |
313 | $options = $bao::buildOptions($fieldName); | |
314 | ||
315 | if (!is_array($options) || !$options) { | |
316 | $options = FALSE; | |
317 | } | |
318 | ||
319 | $this->setOptions($options); | |
320 | } | |
321 | return $this->options; | |
322 | } | |
323 | ||
324 | /** | |
325 | * @param array|bool $options | |
326 | * | |
327 | * @return $this | |
328 | */ | |
329 | public function setOptions($options) { | |
330 | $this->options = $options; | |
331 | return $this; | |
332 | } | |
333 | ||
334 | /** | |
335 | * @return string | |
336 | */ | |
337 | public function getFkEntity() { | |
338 | return $this->fkEntity; | |
339 | } | |
340 | ||
341 | /** | |
342 | * @param string $fkEntity | |
343 | * | |
344 | * @return $this | |
345 | */ | |
346 | public function setFkEntity($fkEntity) { | |
347 | $this->fkEntity = $fkEntity; | |
348 | ||
349 | return $this; | |
350 | } | |
351 | ||
352 | /** | |
353 | * @param array $values | |
354 | * @return array | |
355 | */ | |
356 | public function toArray($values = []) { | |
357 | $ret = []; | |
358 | foreach (get_object_vars($this) as $key => $val) { | |
359 | $key = strtolower(preg_replace('/(?=[A-Z])/', '_$0', $key)); | |
360 | if (!$values || in_array($key, $values)) { | |
361 | $ret[$key] = $val; | |
362 | } | |
363 | } | |
364 | return $ret; | |
365 | } | |
366 | ||
367 | } |