From: Tim Otten Date: Fri, 16 Jul 2021 06:23:39 +0000 (-0700) Subject: (REF) APIv4 FieldSpec - Extract DataTypeSpecTrait (dataType, serialize, fkEntity) X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=17bb4afc266841af48d62778717a61610fe440c0;p=civicrm-core.git (REF) APIv4 FieldSpec - Extract DataTypeSpecTrait (dataType, serialize, fkEntity) Also: Update `setSerialize()` to accept types a string. This is useful if the info comes from an annotation. --- diff --git a/Civi/Api4/Service/Spec/FieldSpec.php b/Civi/Api4/Service/Spec/FieldSpec.php index 4f5006d931..ceefef3a4c 100644 --- a/Civi/Api4/Service/Spec/FieldSpec.php +++ b/Civi/Api4/Service/Spec/FieldSpec.php @@ -13,6 +13,7 @@ namespace Civi\Api4\Service\Spec; use Civi\Schema\Traits\BasicSpecTrait; +use Civi\Schema\Traits\DataTypeSpecTrait; use Civi\Schema\Traits\GuiSpecTrait; use Civi\Schema\Traits\SqlSpecTrait; @@ -21,6 +22,9 @@ class FieldSpec { // BasicSpecTrait: name, title, description use BasicSpecTrait; + // DataTypeSpecTrait: dataType, serialize, fkEntity + use DataTypeSpecTrait; + // GuiSpecTrait: label, inputType, inputAttrs, helpPre, helpPost use GuiSpecTrait; @@ -62,21 +66,6 @@ class FieldSpec { */ private $optionsCallback; - /** - * @var string - */ - public $dataType; - - /** - * @var string - */ - public $fkEntity; - - /** - * @var int - */ - public $serialize; - /** * @var array */ @@ -92,17 +81,6 @@ class FieldSpec { */ public $outputFormatters; - /** - * Aliases for the valid data types - * - * @var array - */ - public static $typeAliases = [ - 'Int' => 'Integer', - 'Link' => 'Url', - 'Memo' => 'Text', - ]; - /** * @param string $name * @param string $entity @@ -186,50 +164,6 @@ class FieldSpec { return $this; } - /** - * @return string - */ - public function getDataType() { - return $this->dataType; - } - - /** - * @param $dataType - * - * @return $this - * @throws \Exception - */ - public function setDataType($dataType) { - if (array_key_exists($dataType, self::$typeAliases)) { - $dataType = self::$typeAliases[$dataType]; - } - - if (!in_array($dataType, $this->getValidDataTypes())) { - throw new \Exception(sprintf('Invalid data type "%s', $dataType)); - } - - $this->dataType = $dataType; - - return $this; - } - - /** - * @return int - */ - public function getSerialize() { - return $this->serialize; - } - - /** - * @param int|null $serialize - * @return $this - */ - public function setSerialize($serialize) { - $this->serialize = $serialize; - - return $this; - } - /** * @param array $permission * @return $this @@ -289,18 +223,6 @@ class FieldSpec { return $this; } - /** - * Add valid types that are not not part of \CRM_Utils_Type::dataTypes - * - * @return array - */ - private function getValidDataTypes() { - $extraTypes = ['Boolean', 'Text', 'Float', 'Url', 'Array', 'Blob', 'Mediumblob']; - $extraTypes = array_combine($extraTypes, $extraTypes); - - return array_merge(\CRM_Utils_Type::dataTypes(), $extraTypes); - } - /** * @param array $values * @param array|bool $return @@ -339,24 +261,6 @@ class FieldSpec { return $this; } - /** - * @return string - */ - public function getFkEntity() { - return $this->fkEntity; - } - - /** - * @param string $fkEntity - * - * @return $this - */ - public function setFkEntity($fkEntity) { - $this->fkEntity = $fkEntity; - - return $this; - } - /** * Gets all public variables, converted to snake_case * diff --git a/Civi/Schema/Traits/DataTypeSpecTrait.php b/Civi/Schema/Traits/DataTypeSpecTrait.php new file mode 100644 index 0000000000..6b0a00af9a --- /dev/null +++ b/Civi/Schema/Traits/DataTypeSpecTrait.php @@ -0,0 +1,142 @@ + 'Integer', + 'Link' => 'Url', + 'Memo' => 'Text', + ]; + + /** + * @return string + */ + public function getDataType() { + return $this->dataType; + } + + /** + * @param $dataType + * + * @return $this + * @throws \Exception + */ + public function setDataType($dataType) { + if (array_key_exists($dataType, self::$typeAliases)) { + $dataType = self::$typeAliases[$dataType]; + } + + if (!in_array($dataType, $this->getValidDataTypes())) { + throw new \Exception(sprintf('Invalid data type "%s', $dataType)); + } + + $this->dataType = $dataType; + + return $this; + } + + /** + * @return string + */ + public function getFkEntity() { + return $this->fkEntity; + } + + /** + * @param string $fkEntity + * + * @return $this + */ + public function setFkEntity($fkEntity) { + $this->fkEntity = $fkEntity; + + return $this; + } + + /** + * @return int + */ + public function getSerialize() { + return $this->serialize; + } + + /** + * @param int|string|null $serialize + * @return $this + */ + public function setSerialize($serialize) { + if (is_string($serialize)) { + $const = 'CRM_Core_DAO::SERIALIZE_' . $serialize; + if (defined($const)) { + $serialize = constant($const); + } + } + $this->serialize = $serialize; + return $this; + } + + /** + * Add valid types that are not not part of \CRM_Utils_Type::dataTypes + * + * @return array + */ + protected function getValidDataTypes() { + $extraTypes = [ + 'Boolean', + 'Text', + 'Float', + 'Url', + 'Array', + 'Blob', + 'Mediumblob', + ]; + $extraTypes = array_combine($extraTypes, $extraTypes); + + return array_merge(\CRM_Utils_Type::dataTypes(), $extraTypes); + } + +}