Skip to content

Commit

Permalink
Default to empty array value when normalizing arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Oct 22, 2023
1 parent ef8d52f commit 6a67bc9
Showing 1 changed file with 16 additions and 55 deletions.
71 changes: 16 additions & 55 deletions src/Listener/ViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,12 @@ protected function _getRelatedModels(array $associationTypes = []): array
}
}

$models = Hash::normalize($models);
$models = Hash::normalize($models, default: []);

$blacklist = $this->_action()->getConfig('scaffold.relations_blacklist');
if (!empty($blacklist)) {
$blacklist = Hash::normalize($blacklist);
$models = array_diff_key($models, $blacklist);
}

foreach ($models as $key => $value) {
if (!is_array($value)) {
$models[$key] = [];
}
$models = array_diff_key($models, array_flip($blacklist));
}

return $models;
Expand Down Expand Up @@ -316,14 +310,14 @@ protected function _getPageVariables(): array
protected function _scaffoldFields(array $associations = []): array
{
$action = $this->_action();
$scaffoldFields = (array)$action->getConfig('scaffold.fields');
if (!empty($scaffoldFields)) {
$scaffoldFields = Hash::normalize($scaffoldFields);
}
$scaffoldFields = Hash::normalize(
(array)$action->getConfig('scaffold.fields'),
default: []
);

if (empty($scaffoldFields) || $action->getConfig('scaffold.autoFields')) {
$cols = $this->_model()->getSchema()->columns();
$cols = Hash::normalize($cols);
$cols = Hash::normalize($cols, default: []);

$scope = $action->getConfig('scope');
if ($scope === 'entity' && !empty($associations['manyToMany'])) {
Expand All @@ -338,24 +332,20 @@ protected function _scaffoldFields(array $associations = []): array
}

// Check for blacklisted fields
$blacklist = $action->getConfig('scaffold.fields_blacklist');
if (!empty($blacklist)) {
$blacklist = $this->_blacklist();
if ($blacklist) {
$scaffoldFields = array_diff_key($scaffoldFields, array_combine($blacklist, $blacklist));
}

// Make sure all array values are an array
foreach ($scaffoldFields as $field => $options) {
if (!is_array($options)) {
$scaffoldFields[$field] = (array)$options;
}

$scaffoldFields[$field] += ['formatter' => null];
}

$fieldSettings = $action->getConfig('scaffold.field_settings');
if (empty($fieldSettings)) {
$fieldSettings = [];
}
$fieldSettings = Hash::normalize(
(array)$action->getConfig('scaffold.field_settings'),
default: []
);
$fieldSettings = array_intersect_key($fieldSettings, $scaffoldFields);
$scaffoldFields = Hash::merge($scaffoldFields, $fieldSettings);

Expand Down Expand Up @@ -405,14 +395,11 @@ protected function _getControllerActions(): array
$actionBlacklist = [];
$groups = $this->_action()->getConfig('scaffold.action_groups') ?: [];
foreach ($groups as $group) {
$group = Hash::normalize($group);
$group = Hash::normalize($group, default: []);
foreach ($group as $actionName => $config) {
if (isset($table[$actionName]) || isset($entity[$actionName])) {
continue;
}
if ($config === null) {
$config = [];
}
[$scope, $actionConfig] = $this->_getControllerActionConfiguration($actionName, $config);
$realAction = Hash::get($actionConfig, 'url.action', $actionName);
if (!isset(${$scope}[$realAction])) {
Expand Down Expand Up @@ -507,8 +494,8 @@ protected function _getAllowedActions(): array
$extraActions = $this->_action()->getConfig('scaffold.extra_actions') ?: [];

$allActions = array_merge(
$this->_normalizeActions($actions),
$this->_normalizeActions($extraActions)
Hash::normalize($actions, default: []),
Hash::normalize($extraActions, default: [])
);

$blacklist = (array)$this->_action()->getConfig('scaffold.actions_blacklist');
Expand All @@ -525,32 +512,6 @@ protected function _getAllowedActions(): array
return array_diff_key($allActions, $blacklist);
}

/**
* Convert mixed action configs to unified structure
*
* [
* 'ACTION_1' => [..config...],
* 'ACTION_2' => [..config...],
* 'ACTION_N' => [..config...]
* ]
*
* @param array $actions Actions
* @return array
*/
protected function _normalizeActions(array $actions): array
{
$normalized = [];
foreach ($actions as $key => $config) {
if (is_array($config)) {
$normalized[$key] = $config;
} else {
$normalized[$config] = [];
}
}

return $normalized;
}

/**
* Returns associations for controllers models.
*
Expand Down

0 comments on commit 6a67bc9

Please sign in to comment.