From b1b92ea217a2d592c2b2f6737707722f581241a8 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 15 Jul 2021 23:34:32 -0700 Subject: [PATCH] (REF) APIv4 FieldSpec - Extract ArrayFormatTrait. Add loadArray(). --- Civi/Api4/Service/Spec/FieldSpec.php | 32 ++--------- Civi/Schema/Traits/ArrayFormatTrait.php | 76 +++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 28 deletions(-) create mode 100644 Civi/Schema/Traits/ArrayFormatTrait.php diff --git a/Civi/Api4/Service/Spec/FieldSpec.php b/Civi/Api4/Service/Spec/FieldSpec.php index 9839029416..f971b16de8 100644 --- a/Civi/Api4/Service/Spec/FieldSpec.php +++ b/Civi/Api4/Service/Spec/FieldSpec.php @@ -12,6 +12,7 @@ namespace Civi\Api4\Service\Spec; +use Civi\Schema\Traits\ArrayFormatTrait; use Civi\Schema\Traits\BasicSpecTrait; use Civi\Schema\Traits\DataTypeSpecTrait; use Civi\Schema\Traits\GuiSpecTrait; @@ -35,6 +36,9 @@ class FieldSpec { // SqlSpecTrait tableName, columnName, operators, sqlFilters use SqlSpecTrait; + // ArrayFormatTrait: toArray():array, loadArray($array) + use ArrayFormatTrait; + /** * @var mixed */ @@ -217,32 +221,4 @@ class FieldSpec { return $this; } - /** - * Gets all public variables, converted to snake_case - * - * @return array - */ - public function toArray() { - // Anonymous class will only have access to public vars - $getter = new class { - - function getPublicVars($object) { - return get_object_vars($object); - } - - }; - - // If getOptions was never called, make options a boolean - if (!isset($this->options)) { - $this->options = isset($this->optionsCallback); - } - - $ret = []; - foreach ($getter->getPublicVars($this) as $key => $val) { - $key = strtolower(preg_replace('/(?=[A-Z])/', '_$0', $key)); - $ret[$key] = $val; - } - return $ret; - } - } diff --git a/Civi/Schema/Traits/ArrayFormatTrait.php b/Civi/Schema/Traits/ArrayFormatTrait.php new file mode 100644 index 0000000000..520568dcdb --- /dev/null +++ b/Civi/Schema/Traits/ArrayFormatTrait.php @@ -0,0 +1,76 @@ +options)) { + $this->options = isset($this->optionsCallback); + } + + $ret = []; + foreach ($getter->getPublicVars($this) as $key => $val) { + $key = strtolower(preg_replace('/(?=[A-Z])/', '_$0', $key)); + $ret[$key] = $val; + } + return $ret; + } + + /** + * Populate this field-spec using values from an array. + * + * @param iterable $values + * List of public variables, expressed in snake_case. + * Ex: ['title' => 'Color', 'default_value' => '#f00'] + * @param bool $strict + * In strict mode, properties are only accepted if they are formally defined on the current class. + * @return $this + */ + public function loadArray(iterable $values, bool $strict = FALSE) { + foreach ($values as $key => $value) { + $field = \CRM_Utils_String::convertStringToCamel($key, FALSE); + $setter = 'set' . ucfirst($field); + if ($strict && !property_exists($this, $field) && !method_exists($this, $setter) && $value !== NULL) { + throw new \CRM_Core_Exception(sprintf('Cannot assign field (%s::%s aka %s)', get_class($this), $field, $key)); + } + if (is_callable([$this, $setter])) { + $this->{$setter}($value); + } + else { + $this->{$field} = $value; + } + } + return $this; + } + +} -- 2.25.1