Skip to content

Commit

Permalink
Merge pull request #16873 from colemanw/api4ArrayValue
Browse files Browse the repository at this point in the history
[REF] Civi/Api4 - Refactor out 'use CRM_Utils_Array'
  • Loading branch information
seamuslee001 authored Mar 24, 2020
2 parents 363ce69 + 1b8e3bc commit a30bd45
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 38 deletions.
5 changes: 2 additions & 3 deletions Civi/Api4/Query/Api4SelectQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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:
Expand Down Expand Up @@ -200,7 +199,7 @@ protected function buildSelectFields() {
$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'];
}
}
}
Expand Down Expand Up @@ -326,7 +325,7 @@ protected function getField($fieldName) {
if (count($fieldPath) > 1) {
$fieldName = implode('.', array_slice($fieldPath, -2));
}
return UtilsArray::value($fieldName, $this->apiFieldSpec);
return $this->apiFieldSpec[$fieldName] ?? NULL;
}
return NULL;
}
Expand Down
19 changes: 9 additions & 10 deletions Civi/Api4/Service/Schema/SchemaMapBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
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 {
/**
Expand Down Expand Up @@ -89,18 +88,18 @@ private function loadTables(SchemaMap $map) {
* @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);
}
}
Expand All @@ -111,22 +110,22 @@ private function addJoins(Table $table, $field, array $data) {
* @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'])) {
Expand Down
35 changes: 17 additions & 18 deletions Civi/Api4/Service/Spec/SpecFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

namespace Civi\Api4\Service\Spec;

use CRM_Utils_Array as ArrayHelper;
use CRM_Core_DAO_AllCoreTables as AllCoreTables;

class SpecFormatter {
Expand Down Expand Up @@ -64,32 +63,32 @@ public static function arrayToField(array $data, $entity) {
$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));
}
Expand Down Expand Up @@ -132,7 +131,7 @@ private static function getDataType(array $data) {
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;
Expand All @@ -144,8 +143,8 @@ private static function getDataType(array $data) {
* @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']))) {
Expand All @@ -158,7 +157,7 @@ public static function setInputTypeAndAttrs(FieldSpec &$fieldSpec, $data, $dataT
'Select Date' => 'Date',
'Link' => 'Url',
];
$inputType = ArrayHelper::value($inputType, $map, $inputType);
$inputType = $map[$inputType] ?? $inputType;
if ($inputType == 'Date' && !empty($inputAttrs['formatType'])) {
self::setLegacyDateFormat($inputAttrs);
}
Expand Down
6 changes: 2 additions & 4 deletions Civi/Api4/Utils/ArrayInsertionUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

namespace Civi\Api4\Utils;

use CRM_Utils_Array as UtilsArray;

class ArrayInsertionUtil {

/**
Expand Down Expand Up @@ -67,11 +65,11 @@ public static function insert(&$array, $parts, $values) {
* @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;
});
}

Expand Down
4 changes: 1 addition & 3 deletions tests/phpunit/api/v4/Traits/QueryCounterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

namespace api\v4\Traits;

use CRM_Utils_Array as ArrayHelper;

trait QueryCounterTrait {

/**
Expand Down Expand Up @@ -56,7 +54,7 @@ private function getCurrentGlobalQueryCount() {
throw new \Exception('Database object not set so cannot count queries');
}

return ArrayHelper::value('RESULTSEQ', $_DB_DATAOBJECT, 0);
return $_DB_DATAOBJECT['RESULTSEQ'] ?? 0;
}

}

0 comments on commit a30bd45

Please sign in to comment.