685a7c7df64975bc3a5f47a746819d9540585cb8
3 namespace Civi\Api4\Service\Spec
;
5 use Civi\Api4\Utils\CoreUtil
;
11 protected $defaultValue;
31 protected $description;
36 protected $required = FALSE;
41 protected $requiredIf;
61 protected $inputAttrs = [];
74 * Aliases for the valid data types
78 public static $typeAliases = [
86 * @param string $entity
87 * @param string $dataType
89 public function __construct($name, $entity, $dataType = 'String') {
90 $this->entity
= $entity;
91 $this->setName($name);
92 $this->setDataType($dataType);
98 public function getDefaultValue() {
99 return $this->defaultValue
;
103 * @param mixed $defaultValue
107 public function setDefaultValue($defaultValue) {
108 $this->defaultValue
= $defaultValue;
116 public function getName() {
121 * @param string $name
125 public function setName($name) {
134 public function getTitle() {
139 * @param string $title
143 public function setTitle($title) {
144 $this->title
= $title;
152 public function getEntity() {
153 return $this->entity
;
159 public function getDescription() {
160 return $this->description
;
164 * @param string $description
168 public function setDescription($description) {
169 $this->description
= $description;
177 public function isRequired() {
178 return $this->required
;
182 * @param bool $required
186 public function setRequired($required) {
187 $this->required
= $required;
195 public function getRequiredIf() {
196 return $this->requiredIf
;
200 * @param bool $requiredIf
204 public function setRequiredIf($requiredIf) {
205 $this->requiredIf
= $requiredIf;
213 public function getDataType() {
214 return $this->dataType
;
223 public function setDataType($dataType) {
224 if (array_key_exists($dataType, self
::$typeAliases)) {
225 $dataType = self
::$typeAliases[$dataType];
228 if (!in_array($dataType, $this->getValidDataTypes())) {
229 throw new \
Exception(sprintf('Invalid data type "%s', $dataType));
232 $this->dataType
= $dataType;
240 public function getSerialize() {
241 return $this->serialize
;
245 * @param int|null $serialize
248 public function setSerialize($serialize) {
249 $this->serialize
= $serialize;
257 public function getInputType() {
258 return $this->inputType
;
262 * @param string $inputType
265 public function setInputType($inputType) {
266 $this->inputType
= $inputType;
274 public function getInputAttrs() {
275 return $this->inputAttrs
;
279 * @param array $inputAttrs
282 public function setInputAttrs($inputAttrs) {
283 $this->inputAttrs
= $inputAttrs;
289 * Add valid types that are not not part of \CRM_Utils_Type::dataTypes
293 private function getValidDataTypes() {
294 $extraTypes = ['Boolean', 'Text', 'Float', 'Url', 'Array'];
295 $extraTypes = array_combine($extraTypes, $extraTypes);
297 return array_merge(\CRM_Utils_Type
::dataTypes(), $extraTypes);
303 public function getOptions() {
304 if (!isset($this->options
) ||
$this->options
=== TRUE) {
305 $fieldName = $this->getName();
307 if ($this instanceof CustomFieldSpec
) {
308 // buildOptions relies on the custom_* type of field names
309 $fieldName = sprintf('custom_%d', $this->getCustomFieldId());
312 $bao = CoreUtil
::getBAOFromApiName($this->getEntity());
313 $options = $bao::buildOptions($fieldName);
315 if (!is_array($options) ||
!$options) {
319 $this->setOptions($options);
321 return $this->options
;
325 * @param array|bool $options
329 public function setOptions($options) {
330 $this->options
= $options;
337 public function getFkEntity() {
338 return $this->fkEntity
;
342 * @param string $fkEntity
346 public function setFkEntity($fkEntity) {
347 $this->fkEntity
= $fkEntity;
353 * @param array $values
356 public function toArray($values = []) {
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)) {