From 7504bfab10a464db3e3eb5848e9087e69aef0e68 Mon Sep 17 00:00:00 2001 From: iglocska Date: Fri, 4 Nov 2022 09:31:46 +0100 Subject: [PATCH] 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 --- src/Controller/Component/APIRearrangeComponent.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Controller/Component/APIRearrangeComponent.php b/src/Controller/Component/APIRearrangeComponent.php index e7523c0..561b271 100644 --- a/src/Controller/Component/APIRearrangeComponent.php +++ b/src/Controller/Component/APIRearrangeComponent.php @@ -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(); }