|
8 | 8 | namespace Ibexa\Bundle\Core\Command; |
9 | 9 |
|
10 | 10 | use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface; |
| 11 | +use Ibexa\Core\Base\Exceptions\InvalidArgumentException; |
11 | 12 | use Ibexa\Core\MVC\Symfony\SiteAccess; |
12 | 13 | use Symfony\Component\Console\Attribute\AsCommand; |
13 | 14 | use Symfony\Component\Console\Command\Command; |
@@ -73,7 +74,7 @@ public function configure(): void |
73 | 74 | 'sort', |
74 | 75 | null, |
75 | 76 | InputOption::VALUE_REQUIRED, |
76 | | - 'Sort list of hashes by this key, ascending. For example: --sort template' |
| 77 | + 'Sort list of hashes by this key, ascending. For example: --sort position' |
77 | 78 | ); |
78 | 79 | $this->addOption( |
79 | 80 | 'reverse-sort', |
@@ -105,17 +106,26 @@ protected function execute(InputInterface $input, OutputInterface $output): int |
105 | 106 | $parameter = $input->getArgument('parameter'); |
106 | 107 | $namespace = $input->getOption('namespace'); |
107 | 108 | $scope = $input->getOption('scope'); |
| 109 | + $sort = $input->getOption('sort'); |
108 | 110 | $parameterData = $this->configResolver->getParameter($parameter, $namespace, $scope); |
109 | 111 |
|
110 | | - if (null !== ($sort = $input->getOption('sort')) && is_array($parameterData) && is_array($parameterData[0]) && array_key_exists($sort, $parameterData[0]) && is_scalar($parameterData[0][$sort])) { |
| 112 | + if (null !== $sort && !empty($parameterData)) { |
| 113 | + if (!is_array($parameterData)) { |
| 114 | + throw new InvalidArgumentException('--sort', "'$parameter' isn't a list. Sort can be used only on list."); |
| 115 | + } |
| 116 | + if (!array_is_list($parameterData)) { |
| 117 | + throw new InvalidArgumentException('--sort', "'$parameter' is a hash or an object. Sort can be used only on list."); |
| 118 | + } |
| 119 | + if (!array_key_exists($sort, $parameterData[0])) { |
| 120 | + throw new InvalidArgumentException('--sort', "'$sort' property doesn't exist on '$parameter' list items."); |
| 121 | + } |
| 122 | + if (!is_scalar($parameterData[0][$sort])) { |
| 123 | + throw new InvalidArgumentException('--sort', "'{$parameter}[n][{$sort}]' properties aren't scalar and can't be sorted."); |
| 124 | + } |
111 | 125 | if ($input->getOption('reverse-sort')) { |
112 | | - usort($parameterData, static function ($a, $b) use ($sort) { |
113 | | - return $b[$sort] <=> $a[$sort]; |
114 | | - }); |
| 126 | + usort($parameterData, static fn (array $a, array $b): int => $b[$sort] <=> $a[$sort]); |
115 | 127 | } else { |
116 | | - usort($parameterData, static function ($a, $b) use ($sort) { |
117 | | - return $a[$sort] <=> $b[$sort]; |
118 | | - }); |
| 128 | + usort($parameterData, static fn (array $a, array $b): int => $a[$sort] <=> $b[$sort]); |
119 | 129 | } |
120 | 130 | } |
121 | 131 |
|
|
0 commit comments