fix: [API] rearrange component - handle collections correctly

- return a new collection with the individual values transferred to it after changes
- avoids some weird quirks with unsetting related Objects not taking effect
cli-modification-summary
iglocska 2022-11-04 09:31:46 +01:00
parent 73ad04906a
commit 7504bfab10
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
1 changed files with 6 additions and 2 deletions

View File

@ -12,15 +12,19 @@ use Cake\Core\Configure;
use Cake\Core\Configure\Engine\PhpConfig;
use Cake\Utility\Inflector;
use Cake\Routing\Router;
use Cake\Collection\Collection;
class APIRearrangeComponent extends Component
{
public function rearrangeForAPI(object $data): object
public function rearrangeForAPI(object $data)
{
if (is_subclass_of($data, 'Iterator')) {
$data->each(function ($value, $key) {
$newData = [];
$data->each(function ($value, $key) use (&$newData) {
$value->rearrangeForAPI();
$newData[] = $value;
});
return new Collection($newData);
} else {
$data->rearrangeForAPI();
}