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;
// BasicSpecTrait: name, title, description
use BasicSpecTrait;
+ // DataTypeSpecTrait: dataType, serialize, fkEntity
+ use DataTypeSpecTrait;
+
// GuiSpecTrait: label, inputType, inputAttrs, helpPre, helpPost
use GuiSpecTrait;
*/
private $optionsCallback;
- /**
- * @var string
- */
- public $dataType;
-
- /**
- * @var string
- */
- public $fkEntity;
-
- /**
- * @var int
- */
- public $serialize;
-
/**
* @var array
*/
*/
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
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
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
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
*
--- /dev/null
+<?php
+
+/*
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC. All rights reserved. |
+ | |
+ | This work is published under the GNU AGPLv3 license with some |
+ | permitted exceptions and without any warranty. For full license |
+ | and copyright information, see https://civicrm.org/licensing |
+ +--------------------------------------------------------------------+
+ */
+
+namespace Civi\Schema\Traits;
+
+/**
+ * Describe what values are allowed to be stored in this field.
+ *
+ * @package CRM
+ * @copyright CiviCRM LLC https://civicrm.org/licensing
+ */
+trait DataTypeSpecTrait {
+
+ /**
+ * The type of data stored in this field.
+ *
+ * Ex: 'Integer', 'Boolean', 'Float', 'String', 'Text', 'Blob'
+ *
+ * @var string
+ */
+ public $dataType;
+
+ /**
+ * @var int
+ * @see CRM_Core_DAO::SERIALIZE_SEPARATOR_BOOKEND,
+ * CRM_Core_DAO::SERIALIZE_JSON, etc
+ */
+ public $serialize;
+
+ /**
+ * @var string
+ */
+ public $fkEntity;
+
+ /**
+ * Aliases for the valid data types
+ *
+ * @var array
+ */
+ public static $typeAliases = [
+ 'Int' => '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);
+ }
+
+}