From aa0aca463e9245398dfa19e6083c29b32f7f88d5 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 31 Jul 2022 10:51:45 -0400 Subject: [PATCH] APIv4 - smarter parsing of Entity docblocks --- Civi/Api4/Generic/AbstractEntity.php | 27 +++++++++++++++------ Civi/Api4/Generic/Traits/DAOActionTrait.php | 1 - Civi/Api4/Utils/ReflectionUtils.php | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Civi/Api4/Generic/AbstractEntity.php b/Civi/Api4/Generic/AbstractEntity.php index 06cab37207..bc99c02a41 100644 --- a/Civi/Api4/Generic/AbstractEntity.php +++ b/Civi/Api4/Generic/AbstractEntity.php @@ -158,19 +158,30 @@ abstract class AbstractEntity { foreach (ReflectionUtils::getTraits(static::class) as $trait) { $info['type'][] = self::stripNamespace($trait); } + // Get DocBlock from APIv4 Entity class $reflection = new \ReflectionClass(static::class); - $info = array_merge($info, ReflectionUtils::getCodeDocs($reflection, NULL, ['entity' => $info['name']])); + $docBlock = ReflectionUtils::getCodeDocs($reflection, NULL, ['entity' => $info['name']]); + // Convert docblock keys to snake_case + foreach ($docBlock as $key => $val) { + $docBlock[\CRM_Utils_String::convertStringToSnakeCase($key)] = $val; + } + // Filter docblock to only declared entity fields + foreach (\Civi\Api4\Entity::$entityFields as $field) { + if (isset($docBlock[$field['name']])) { + $val = $docBlock[$field['name']]; + // Convert to array if data_type == Array + if (isset($field['data_type']) && $field['data_type'] === 'Array' && is_string($val)) { + $val = \CRM_Core_DAO::unSerializeField($val, \CRM_Core_DAO::SERIALIZE_COMMA); + } + $info[$field['name']] = $val; + } + } + if ($dao) { $info['description'] = $dao::getEntityDescription() ?? $info['description'] ?? NULL; } - unset($info['package'], $info['method']); - // Ensure all keys are snake_case - $keys = array_keys($info); - foreach ($keys as &$key) { - $key = \CRM_Utils_String::convertStringToSnakeCase($key); - } - return array_combine($keys, array_values($info)); + return $info; } /** diff --git a/Civi/Api4/Generic/Traits/DAOActionTrait.php b/Civi/Api4/Generic/Traits/DAOActionTrait.php index c7661f7075..d8e511e700 100644 --- a/Civi/Api4/Generic/Traits/DAOActionTrait.php +++ b/Civi/Api4/Generic/Traits/DAOActionTrait.php @@ -336,7 +336,6 @@ trait DAOActionTrait { if (!isset($record[$weightField]) && !empty($record[$idField])) { return; } - $daoFields = $daoName::getSupportedFields(); $newWeight = $record[$weightField] ?? NULL; $oldWeight = empty($record[$idField]) ? NULL : \CRM_Core_DAO::getFieldValue($daoName, $record[$idField], $weightField); diff --git a/Civi/Api4/Utils/ReflectionUtils.php b/Civi/Api4/Utils/ReflectionUtils.php index f09d8841d9..29e5c4d6c6 100644 --- a/Civi/Api4/Utils/ReflectionUtils.php +++ b/Civi/Api4/Utils/ReflectionUtils.php @@ -88,7 +88,7 @@ class ReflectionUtils { elseif ($key == 'return') { $info['return'] = explode('|', $words[0]); } - elseif ($key == 'options' || $key == 'ui_join_filters' || $key == 'groupWeightsBy') { + elseif ($key == 'options') { $val = str_replace(', ', ',', implode(' ', $words)); $info[$key] = explode(',', $val); } -- 2.25.1