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