Unverified Commit a30bd454 authored by Seamus Lee's avatar Seamus Lee Committed by GitHub

Merge pull request #16873 from colemanw/api4ArrayValue

[REF] Civi/Api4 - Refactor out 'use CRM_Utils_Array'
parents 363ce69a 1b8e3bc8
......@@ -20,7 +20,6 @@ use Civi\Api4\Utils\FormattingUtil;
use Civi\Api4\Utils\CoreUtil;
use Civi\Api4\Utils\SelectUtil;
use CRM_Core_DAO_AllCoreTables as AllCoreTables;
use CRM_Utils_Array as UtilsArray;
/**
* A query `node` may be in one of three formats:
......@@ -200,7 +199,7 @@ class Api4SelectQuery extends SelectQuery {
$this->selectFields[$this->fkSelectAliases[$fieldName]] = $fieldName;
}
elseif ($field && in_array($field['name'], $this->entityFieldNames)) {
$this->selectFields[self::MAIN_TABLE_ALIAS . "." . UtilsArray::value('column_name', $field, $field['name'])] = $field['name'];
$this->selectFields[self::MAIN_TABLE_ALIAS . "." . ($field['column_name'] ?? $field['name'])] = $field['name'];
}
}
}
......@@ -326,7 +325,7 @@ class Api4SelectQuery extends SelectQuery {
if (count($fieldPath) > 1) {
$fieldName = implode('.', array_slice($fieldPath, -2));
}
return UtilsArray::value($fieldName, $this->apiFieldSpec);
return $this->apiFieldSpec[$fieldName] ?? NULL;
}
return NULL;
}
......
......@@ -29,7 +29,6 @@ use Civi\Api4\Service\Schema\Joinable\Joinable;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Civi\Api4\Service\Schema\Joinable\OptionValueJoinable;
use CRM_Core_DAO_AllCoreTables as AllCoreTables;
use CRM_Utils_Array as UtilsArray;
class SchemaMapBuilder {
/**
......@@ -89,18 +88,18 @@ class SchemaMapBuilder {
* @param array $data
*/
private function addJoins(Table $table, $field, array $data) {
$fkClass = UtilsArray::value('FKClassName', $data);
$fkClass = $data['FKClassName'] ?? NULL;
// can there be multiple methods e.g. pseudoconstant and fkclass
if ($fkClass) {
$tableName = AllCoreTables::getTableForClass($fkClass);
$fkKey = UtilsArray::value('FKKeyColumn', $data, 'id');
$fkKey = $data['FKKeyColumn'] ?? 'id';
$alias = str_replace('_id', '', $field);
$joinable = new Joinable($tableName, $fkKey, $alias);
$joinable->setJoinType($joinable::JOIN_TYPE_MANY_TO_ONE);
$table->addTableLink($field, $joinable);
}
elseif (UtilsArray::value('pseudoconstant', $data)) {
elseif (!empty($data['pseudoconstant'])) {
$this->addPseudoConstantJoin($table, $field, $data);
}
}
......@@ -111,22 +110,22 @@ class SchemaMapBuilder {
* @param array $data
*/
private function addPseudoConstantJoin(Table $table, $field, array $data) {
$pseudoConstant = UtilsArray::value('pseudoconstant', $data);
$tableName = UtilsArray::value('table', $pseudoConstant);
$optionGroupName = UtilsArray::value('optionGroupName', $pseudoConstant);
$keyColumn = UtilsArray::value('keyColumn', $pseudoConstant, 'id');
$pseudoConstant = $data['pseudoconstant'] ?? NULL;
$tableName = $pseudoConstant['table'] ?? NULL;
$optionGroupName = $pseudoConstant['optionGroupName'] ?? NULL;
$keyColumn = $pseudoConstant['keyColumn'] ?? 'id';
if ($tableName) {
$alias = str_replace('civicrm_', '', $tableName);
$joinable = new Joinable($tableName, $keyColumn, $alias);
$condition = UtilsArray::value('condition', $pseudoConstant);
$condition = $pseudoConstant['condition'] ?? NULL;
if ($condition) {
$joinable->addCondition($condition);
}
$table->addTableLink($field, $joinable);
}
elseif ($optionGroupName) {
$keyColumn = UtilsArray::value('keyColumn', $pseudoConstant, 'value');
$keyColumn = $pseudoConstant['keyColumn'] ?? 'value';
$joinable = new OptionValueJoinable($optionGroupName, NULL, $keyColumn);
if (!empty($data['serialize'])) {
......
......@@ -21,7 +21,6 @@
namespace Civi\Api4\Service\Spec;
use CRM_Utils_Array as ArrayHelper;
use CRM_Core_DAO_AllCoreTables as AllCoreTables;
class SpecFormatter {
......@@ -64,32 +63,32 @@ class SpecFormatter {
$field->setCustomTableName($data['custom_group.table_name']);
$field->setCustomFieldColumnName($data['column_name']);
}
$field->setCustomFieldId(ArrayHelper::value('id', $data));
$field->setCustomFieldId($data['id'] ?? NULL);
$field->setCustomGroupName($data['custom_group.name']);
$field->setTitle(ArrayHelper::value('label', $data));
$field->setHelpPre(ArrayHelper::value('help_pre', $data));
$field->setHelpPost(ArrayHelper::value('help_post', $data));
$field->setTitle($data['label'] ?? NULL);
$field->setHelpPre($data['help_pre'] ?? NULL);
$field->setHelpPost($data['help_post'] ?? NULL);
$field->setOptions(self::customFieldHasOptions($data));
if (\CRM_Core_BAO_CustomField::isSerialized($data)) {
$field->setSerialize(\CRM_Core_DAO::SERIALIZE_SEPARATOR_BOOKEND);
}
}
else {
$name = ArrayHelper::value('name', $data);
$name = $data['name'] ?? NULL;
$field = new FieldSpec($name, $entity, $dataTypeName);
$field->setRequired((bool) ArrayHelper::value('required', $data, FALSE));
$field->setTitle(ArrayHelper::value('title', $data));
$field->setRequired(!empty($data['required']));
$field->setTitle($data['title'] ?? NULL);
$field->setOptions(!empty($data['pseudoconstant']));
$field->setSerialize(ArrayHelper::value('serialize', $data));
$field->setSerialize($data['serialize'] ?? NULL);
}
$field->setDefaultValue(ArrayHelper::value('default', $data));
$field->setDescription(ArrayHelper::value('description', $data));
$field->setDefaultValue($data['default'] ?? NULL);
$field->setDescription($data['description'] ?? NULL);
self::setInputTypeAndAttrs($field, $data, $dataTypeName);
$field->setPermission(ArrayHelper::value('permission', $data));
$fkAPIName = ArrayHelper::value('FKApiName', $data);
$fkClassName = ArrayHelper::value('FKClassName', $data);
$field->setPermission($data['permission'] ?? NULL);
$fkAPIName = $data['FKApiName'] ?? NULL;
$fkClassName = $data['FKClassName'] ?? NULL;
if ($fkAPIName || $fkClassName) {
$field->setFkEntity($fkAPIName ?: AllCoreTables::getBriefName($fkClassName));
}
......@@ -132,7 +131,7 @@ class SpecFormatter {
return !empty($data['time_format']) ? 'Timestamp' : $data['data_type'];
}
$dataTypeInt = ArrayHelper::value('type', $data);
$dataTypeInt = $data['type'] ?? NULL;
$dataTypeName = \CRM_Utils_Type::typeToString($dataTypeInt);
return $dataTypeName;
......@@ -144,8 +143,8 @@ class SpecFormatter {
* @param string $dataTypeName
*/
public static function setInputTypeAndAttrs(FieldSpec &$fieldSpec, $data, $dataTypeName) {
$inputType = $data['html']['type'] ?? ArrayHelper::value('html_type', $data);
$inputAttrs = ArrayHelper::value('html', $data, []);
$inputType = $data['html']['type'] ?? $data['html_type'] ?? NULL;
$inputAttrs = $data['html'] ?? [];
unset($inputAttrs['type']);
if (strstr($inputType, 'Multi-Select') || ($inputType == 'Select' && !empty($data['serialize']))) {
......@@ -158,7 +157,7 @@ class SpecFormatter {
'Select Date' => 'Date',
'Link' => 'Url',
];
$inputType = ArrayHelper::value($inputType, $map, $inputType);
$inputType = $map[$inputType] ?? $inputType;
if ($inputType == 'Date' && !empty($inputAttrs['formatType'])) {
self::setLegacyDateFormat($inputAttrs);
}
......
......@@ -21,8 +21,6 @@
namespace Civi\Api4\Utils;
use CRM_Utils_Array as UtilsArray;
class ArrayInsertionUtil {
/**
......@@ -67,11 +65,11 @@ class ArrayInsertionUtil {
* @return array|mixed
*/
private static function filterValues($parentArray, $isMulti, $values) {
$parentID = UtilsArray::value('id', $parentArray);
$parentID = $parentArray['id'] ?? NULL;
if ($parentID) {
$values = array_filter($values, function ($value) use ($parentID) {
return UtilsArray::value('_parent_id', $value) == $parentID;
return ($value['_parent_id'] ?? NULL) == $parentID;
});
}
......
......@@ -21,8 +21,6 @@
namespace api\v4\Traits;
use CRM_Utils_Array as ArrayHelper;
trait QueryCounterTrait {
/**
......@@ -56,7 +54,7 @@ trait QueryCounterTrait {
throw new \Exception('Database object not set so cannot count queries');
}
return ArrayHelper::value('RESULTSEQ', $_DB_DATAOBJECT, 0);
return $_DB_DATAOBJECT['RESULTSEQ'] ?? 0;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment