From c61fafb955e921d2c9a008c6c9d581ccd82345d9 Mon Sep 17 00:00:00 2001 From: Kirill Matasov <33geek@gmail.com> Date: Tue, 16 Feb 2021 22:29:20 +0300 Subject: [PATCH 01/15] Prevent lost value when type definied in request --- src/Admin/AbstractAdmin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index 6ecf194e4b..980005e98d 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -709,7 +709,7 @@ public function getFilterParameters() } } - $parameters = array_merge($parameters, $filters); + $parameters = array_replace_recursive($parameters, $filters); // always force the parent value if ($this->isChild() && $this->getParentAssociationMapping()) { From 0ba1691b2d20e70c88460674a568127c2d04d00a Mon Sep 17 00:00:00 2001 From: Kirill Matasov <33geek@gmail.com> Date: Tue, 16 Feb 2021 22:31:12 +0300 Subject: [PATCH 02/15] Allow clear default non empty values --- src/Resources/public/Admin.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/Resources/public/Admin.js b/src/Resources/public/Admin.js index 29d941813b..0a3c523b79 100644 --- a/src/Resources/public/Admin.js +++ b/src/Resources/public/Admin.js @@ -334,22 +334,16 @@ var Admin = { return; } - var defaultValues = $.param({'filter': JSON.parse(this.dataset.defaultValues)}).split('&'), - submittedValues = $form.serialize().split('&'); - - // Compare default and submitted filter values in `keyValue` representation. (`keyValue` ex: "filter[publish][value][end]=2020-12-12") - // Only allow to submit non default and non empty values, because empty values means they are not present. - var changedValues = submittedValues.filter(function (keyValue) { - return defaultValues.indexOf(keyValue) === -1 && keyValue.split('=')[1] !== ''; + var defaults = Admin.convert_query_string_to_object( + $.param({'filter': JSON.parse(this.dataset.defaultValues)}) + ); + + // Keep only changed values + $form.find('[name*=filter]').each(function (i, field) { + if ((defaults[field.name] || '') === field.value) { + field.removeAttribute('name'); + } }); - - // Disable all inputs and enable only the required ones - $form.find('[name*=filter]').attr('disabled', 'disabled'); - changedValues - .map(function (keyValue) { return decodeURIComponent(keyValue.split('=')[0]); }) - .forEach(function (key) { - $form.find('[name="' + key + '"]').removeAttr('disabled'); - }); }); /* Advanced filters */ @@ -782,6 +776,13 @@ var Admin = { } }); }, + convert_query_string_to_object: function (str) { + return str.split('&').reduce(function (accumulator, keyValue) { + accumulator[decodeURIComponent(keyValue.split('=')[0])] = keyValue.split('=')[1]; + + return accumulator; + }, {}); + }, /** * Remember open tab after refreshing page. */ From 8c0c75784b2a8abe81ee4046e946f85c010988b4 Mon Sep 17 00:00:00 2001 From: Kirill Matasov <33geek@gmail.com> Date: Tue, 16 Feb 2021 23:39:55 +0300 Subject: [PATCH 03/15] code style --- src/Resources/public/Admin.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Resources/public/Admin.js b/src/Resources/public/Admin.js index 0a3c523b79..70cd7d679f 100644 --- a/src/Resources/public/Admin.js +++ b/src/Resources/public/Admin.js @@ -668,6 +668,7 @@ var Admin = { Admin.handleScroll(footer, navbar, wrapper); } }, + handleScroll: function(footer, navbar, wrapper) { if (footer.length && jQuery(window).scrollTop() + jQuery(window).height() != jQuery(document).height()) { jQuery(footer).addClass('stuck'); @@ -697,6 +698,7 @@ var Admin = { }, 250) ); }, + handleResize: function(footer, navbar, wrapper) { if (navbar.length && jQuery(navbar).hasClass('stuck')) { jQuery(navbar).width(jQuery(wrapper).outerWidth()); @@ -706,6 +708,7 @@ var Admin = { jQuery(footer).width(jQuery(wrapper).outerWidth()); } }, + refreshNavbarStuckClass: function(topNavbar) { var stuck = jQuery('#navbar-stuck'); @@ -718,6 +721,7 @@ var Admin = { stuck.html('body.fixed .content-header .navbar.stuck { top: ' + jQuery(topNavbar).outerHeight() + 'px; }'); }, + // http://davidwalsh.name/javascript-debounce-function debounce: function (func, wait, immediate) { var timeout; @@ -744,6 +748,7 @@ var Admin = { } }; }, + setup_readmore_elements: function(subject) { Admin.log('[core|setup_readmore_elements] setup readmore elements on', subject); @@ -755,9 +760,11 @@ var Admin = { }); }); }, + handle_top_navbar_height: function() { jQuery('body.fixed .content-wrapper').css('padding-top', jQuery('.navbar-static-top').outerHeight()); }, + setup_form_submit: function(subject) { Admin.log('[core|setup_form_submit] setup form submit on', subject); @@ -776,6 +783,7 @@ var Admin = { } }); }, + convert_query_string_to_object: function (str) { return str.split('&').reduce(function (accumulator, keyValue) { accumulator[decodeURIComponent(keyValue.split('=')[0])] = keyValue.split('=')[1]; @@ -783,6 +791,7 @@ var Admin = { return accumulator; }, {}); }, + /** * Remember open tab after refreshing page. */ From 84928377d43befec343361981bf3c096161bc504 Mon Sep 17 00:00:00 2001 From: Willem Verspyck <> Date: Wed, 17 Feb 2021 12:20:21 +0100 Subject: [PATCH 04/15] Fix merge problem for filters with array value --- src/Admin/AbstractAdmin.php | 19 +++++- tests/Admin/AdminTest.php | 131 ++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+), 1 deletion(-) diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index 980005e98d..215d848d82 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -709,7 +709,7 @@ public function getFilterParameters() } } - $parameters = array_replace_recursive($parameters, $filters); + $parameters = $this->mergeParameters($parameters, $filters); // always force the parent value if ($this->isChild() && $this->getParentAssociationMapping()) { @@ -3335,6 +3335,23 @@ private function buildRoutes(): void $extension->configureRoutes($this, $this->routes); } } + + /** + * Merge parameters but replace them when it's a subarray + * Merge parameters but replace them when it's a subarray + */ + private function mergeParameters(array $parameters, array $filters): array + { + foreach (array_intersect_key($parameters, $filters) as $key => $parameter) { + if (is_array($parameter)) { + $parameters[$key] = array_replace($parameter, $filters[$key]); + } else { + $parameters[$key] = $filters[$key]; + } + } + + return array_merge($parameters, array_diff_key($filters, $parameters)); + } } class_exists(\Sonata\Form\Validator\ErrorElement::class); diff --git a/tests/Admin/AdminTest.php b/tests/Admin/AdminTest.php index a878f4c5ba..2f07267d25 100644 --- a/tests/Admin/AdminTest.php +++ b/tests/Admin/AdminTest.php @@ -2832,4 +2832,135 @@ public function getDeprecatedAbstractAdminConstructorArgs(): iterable [1, 1, 1], ]; } + + public function provideMergeParameters() + { + return [ + [ + [ + '_sort_order' => 'DESC', + '_sort_by' => 'id', + 'status' => [ + 'type' => '1', + 'value' => 'foo', + ], + ], + [ + 'status' => [ + 'type' => '2', + 'value' => 'foo', + ], + ], + [ + '_sort_order' => 'DESC', + '_sort_by' => 'id', + 'status' => [ + 'type' => '2', + 'value' => 'foo', + ], + ], + ], + [ + [ + 'status' => [ + 'type' => '1', + ], + ], + [ + 'status' => [ + 'value' => 'foo', + ], + ], + [ + 'status' => [ + 'type' => '1', + 'value' => 'foo', + ], + ], + ], + [ + [ + 'status' => [ + 'type' => '1', + 'value' => 'foo', + ], + ], + [ + 'status' => [ + 'type' => '2', + ], + '_page' => 2, + '_per_page' => 25, + ], + [ + 'status' => [ + 'type' => '2', + 'value' => 'foo', + ], + '_page' => 2, + '_per_page' => 25, + ], + ], + [ + [ + 'status' => [ + 'type' => '1', + 'value' => [ + 'foo', + 'bar', + ], + ], + ], + [ + 'status' => [ + 'value' => [ + 'foo', + ], + ], + ], + [ + 'status' => [ + 'type' => '1', + 'value' => [ + 'foo', + ], + ], + ], + ], + [ + [ + 'status' => [ + 'value' => [ + 'foo', + 'bar', + ], + ], + ], + [ + 'status' => [ + 'value' => [ + 'baz', + ], + ], + ], + [ + 'status' => [ + 'value' => [ + 'baz', + ], + ], + ], + ], + ]; + } + + /** + * @dataProvider provideMergeParameters + */ + public function testMergeParameters(array $parameters, array $filters, array $result): void + { + $admin = new FilteredAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController'); + + $this->assertSame($result, $admin->mergeParameters($parameters, $filters)); + } } From d9d93a4dc0aefbb766792081183d827b971d78a4 Mon Sep 17 00:00:00 2001 From: Willem Verspyck Date: Wed, 17 Feb 2021 12:31:05 +0100 Subject: [PATCH 05/15] Remove duplicate comment --- src/Admin/AbstractAdmin.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index 215d848d82..9ae1e7e65a 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -3337,7 +3337,6 @@ private function buildRoutes(): void } /** - * Merge parameters but replace them when it's a subarray * Merge parameters but replace them when it's a subarray */ private function mergeParameters(array $parameters, array $filters): array From 39460e5b6a1f95b5e4e14d494593fdac0cecfafc Mon Sep 17 00:00:00 2001 From: Willem Verspyck <> Date: Wed, 17 Feb 2021 12:49:22 +0100 Subject: [PATCH 06/15] Fix test after making it private --- tests/Admin/AdminTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/Admin/AdminTest.php b/tests/Admin/AdminTest.php index 2f07267d25..d8ac6b7a9b 100644 --- a/tests/Admin/AdminTest.php +++ b/tests/Admin/AdminTest.php @@ -17,6 +17,7 @@ use Knp\Menu\FactoryInterface; use Knp\Menu\ItemInterface; use PHPUnit\Framework\TestCase; +use ReflectionClass; use Sonata\AdminBundle\Admin\AbstractAdmin; use Sonata\AdminBundle\Admin\AbstractAdminExtension; use Sonata\AdminBundle\Admin\AdminExtensionInterface; @@ -2961,6 +2962,11 @@ public function testMergeParameters(array $parameters, array $filters, array $re { $admin = new FilteredAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController'); - $this->assertSame($result, $admin->mergeParameters($parameters, $filters)); + $reflectionClass = new ReflectionClass($admin); + + $method = $reflectionClass->getMethod('mergeParameters'); + $method->setAccessible(true); + + $this->assertSame($result, $method->invokeArgs($admin, [$parameters, $filters])); } } From 7b500625cb30c348d587963b1772a3c318412a12 Mon Sep 17 00:00:00 2001 From: Willem Verspyck <> Date: Wed, 17 Feb 2021 12:56:43 +0100 Subject: [PATCH 07/15] Add global namespace to "is_array" --- src/Admin/AbstractAdmin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index 9ae1e7e65a..165addae28 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -3342,7 +3342,7 @@ private function buildRoutes(): void private function mergeParameters(array $parameters, array $filters): array { foreach (array_intersect_key($parameters, $filters) as $key => $parameter) { - if (is_array($parameter)) { + if (\is_array($parameter)) { $parameters[$key] = array_replace($parameter, $filters[$key]); } else { $parameters[$key] = $filters[$key]; From 52aaf38fefb23e52b9b0f69b2a194becab0d3276 Mon Sep 17 00:00:00 2001 From: Willem Verspyck <> Date: Wed, 17 Feb 2021 13:09:11 +0100 Subject: [PATCH 08/15] CS fixes --- src/Admin/AbstractAdmin.php | 2 +- tests/Admin/AdminTest.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index 165addae28..6725fde01e 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -3337,7 +3337,7 @@ private function buildRoutes(): void } /** - * Merge parameters but replace them when it's a subarray + * Merge parameters but replace them when it's a subarray */ private function mergeParameters(array $parameters, array $filters): array { diff --git a/tests/Admin/AdminTest.php b/tests/Admin/AdminTest.php index d8ac6b7a9b..a8f8742c45 100644 --- a/tests/Admin/AdminTest.php +++ b/tests/Admin/AdminTest.php @@ -17,7 +17,6 @@ use Knp\Menu\FactoryInterface; use Knp\Menu\ItemInterface; use PHPUnit\Framework\TestCase; -use ReflectionClass; use Sonata\AdminBundle\Admin\AbstractAdmin; use Sonata\AdminBundle\Admin\AbstractAdminExtension; use Sonata\AdminBundle\Admin\AdminExtensionInterface; @@ -2962,7 +2961,7 @@ public function testMergeParameters(array $parameters, array $filters, array $re { $admin = new FilteredAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController'); - $reflectionClass = new ReflectionClass($admin); + $reflectionClass = new \ReflectionClass($admin); $method = $reflectionClass->getMethod('mergeParameters'); $method->setAccessible(true); From f7bf1af5a2702119efab5a558e3af115ef7842fb Mon Sep 17 00:00:00 2001 From: Willem Verspyck <> Date: Wed, 17 Feb 2021 13:12:20 +0100 Subject: [PATCH 09/15] CS fixes --- src/Admin/AbstractAdmin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index 6725fde01e..c5fac8a9b1 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -3337,7 +3337,7 @@ private function buildRoutes(): void } /** - * Merge parameters but replace them when it's a subarray + * Merge parameters but replace them when it's a subarray. */ private function mergeParameters(array $parameters, array $filters): array { From b551279b4221f9e75f5759982c06541c70f96e23 Mon Sep 17 00:00:00 2001 From: Willem Verspyck <> Date: Wed, 17 Feb 2021 14:49:14 +0100 Subject: [PATCH 10/15] Move mergeParameters to new Util\ParametersManipulator class --- src/Admin/AbstractAdmin.php | 19 +-- tests/Admin/AdminTest.php | 136 ------------------- tests/Util/ParametersManipulatorTest.php | 159 +++++++++++++++++++++++ 3 files changed, 161 insertions(+), 153 deletions(-) create mode 100644 tests/Util/ParametersManipulatorTest.php diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index c5fac8a9b1..36c7bf5b1e 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -30,6 +30,7 @@ use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface; use Sonata\AdminBundle\Show\ShowMapper; use Sonata\AdminBundle\Templating\MutableTemplateRegistryInterface; +use Sonata\AdminBundle\Util\ParametersManipulator; use Sonata\Form\Validator\Constraints\InlineConstraint; use Sonata\Form\Validator\ErrorElement; use Symfony\Component\Form\Extension\Core\Type\HiddenType; @@ -709,7 +710,7 @@ public function getFilterParameters() } } - $parameters = $this->mergeParameters($parameters, $filters); + $parameters = ParametersManipulator::merge($parameters, $filters); // always force the parent value if ($this->isChild() && $this->getParentAssociationMapping()) { @@ -3335,22 +3336,6 @@ private function buildRoutes(): void $extension->configureRoutes($this, $this->routes); } } - - /** - * Merge parameters but replace them when it's a subarray. - */ - private function mergeParameters(array $parameters, array $filters): array - { - foreach (array_intersect_key($parameters, $filters) as $key => $parameter) { - if (\is_array($parameter)) { - $parameters[$key] = array_replace($parameter, $filters[$key]); - } else { - $parameters[$key] = $filters[$key]; - } - } - - return array_merge($parameters, array_diff_key($filters, $parameters)); - } } class_exists(\Sonata\Form\Validator\ErrorElement::class); diff --git a/tests/Admin/AdminTest.php b/tests/Admin/AdminTest.php index a8f8742c45..a878f4c5ba 100644 --- a/tests/Admin/AdminTest.php +++ b/tests/Admin/AdminTest.php @@ -2832,140 +2832,4 @@ public function getDeprecatedAbstractAdminConstructorArgs(): iterable [1, 1, 1], ]; } - - public function provideMergeParameters() - { - return [ - [ - [ - '_sort_order' => 'DESC', - '_sort_by' => 'id', - 'status' => [ - 'type' => '1', - 'value' => 'foo', - ], - ], - [ - 'status' => [ - 'type' => '2', - 'value' => 'foo', - ], - ], - [ - '_sort_order' => 'DESC', - '_sort_by' => 'id', - 'status' => [ - 'type' => '2', - 'value' => 'foo', - ], - ], - ], - [ - [ - 'status' => [ - 'type' => '1', - ], - ], - [ - 'status' => [ - 'value' => 'foo', - ], - ], - [ - 'status' => [ - 'type' => '1', - 'value' => 'foo', - ], - ], - ], - [ - [ - 'status' => [ - 'type' => '1', - 'value' => 'foo', - ], - ], - [ - 'status' => [ - 'type' => '2', - ], - '_page' => 2, - '_per_page' => 25, - ], - [ - 'status' => [ - 'type' => '2', - 'value' => 'foo', - ], - '_page' => 2, - '_per_page' => 25, - ], - ], - [ - [ - 'status' => [ - 'type' => '1', - 'value' => [ - 'foo', - 'bar', - ], - ], - ], - [ - 'status' => [ - 'value' => [ - 'foo', - ], - ], - ], - [ - 'status' => [ - 'type' => '1', - 'value' => [ - 'foo', - ], - ], - ], - ], - [ - [ - 'status' => [ - 'value' => [ - 'foo', - 'bar', - ], - ], - ], - [ - 'status' => [ - 'value' => [ - 'baz', - ], - ], - ], - [ - 'status' => [ - 'value' => [ - 'baz', - ], - ], - ], - ], - ]; - } - - /** - * @dataProvider provideMergeParameters - */ - public function testMergeParameters(array $parameters, array $filters, array $result): void - { - $admin = new FilteredAdmin('sonata.post.admin.model', 'Application\Sonata\FooBundle\Entity\Model', 'Sonata\FooBundle\Controller\ModelAdminController'); - - $reflectionClass = new \ReflectionClass($admin); - - $method = $reflectionClass->getMethod('mergeParameters'); - $method->setAccessible(true); - - $this->assertSame($result, $method->invokeArgs($admin, [$parameters, $filters])); - } } diff --git a/tests/Util/ParametersManipulatorTest.php b/tests/Util/ParametersManipulatorTest.php new file mode 100644 index 0000000000..0b78e37fba --- /dev/null +++ b/tests/Util/ParametersManipulatorTest.php @@ -0,0 +1,159 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Tests\Util; + +use PHPUnit\Framework\TestCase; +use Sonata\AdminBundle\Util\ParametersManipulator; + +/** + * @author Willem Verspyck + */ +class ParametersManipulatorTest extends TestCase +{ + /** + * @return array + */ + public function provideMergeParameters(): array + { + return [ + [ + [ + '_sort_order' => 'DESC', + '_sort_by' => 'id', + 'status' => [ + 'type' => '1', + 'value' => 'foo', + ], + ], + [ + 'status' => [ + 'type' => '2', + 'value' => 'foo', + ], + ], + [ + '_sort_order' => 'DESC', + '_sort_by' => 'id', + 'status' => [ + 'type' => '2', + 'value' => 'foo', + ], + ], + ], + [ + [ + 'status' => [ + 'type' => '1', + ], + ], + [ + 'status' => [ + 'value' => 'foo', + ], + ], + [ + 'status' => [ + 'type' => '1', + 'value' => 'foo', + ], + ], + ], + [ + [ + 'status' => [ + 'type' => '1', + 'value' => 'foo', + ], + ], + [ + 'status' => [ + 'type' => '2', + ], + '_page' => 2, + '_per_page' => 25, + ], + [ + 'status' => [ + 'type' => '2', + 'value' => 'foo', + ], + '_page' => 2, + '_per_page' => 25, + ], + ], + [ + [ + 'status' => [ + 'type' => '1', + 'value' => [ + 'foo', + 'bar', + ], + ], + ], + [ + 'status' => [ + 'value' => [ + 'foo', + ], + ], + ], + [ + 'status' => [ + 'type' => '1', + 'value' => [ + 'foo', + ], + ], + ], + ], + [ + [ + 'status' => [ + 'value' => [ + 'foo', + 'bar', + ], + ], + ], + [ + 'status' => [ + 'value' => [ + 'baz', + ], + ], + ], + [ + 'status' => [ + 'value' => [ + 'baz', + ], + ], + ], + ], + ]; + } + + /** + * @dataProvider provideMergeParameters + * + * @param array $parameters + * @param array $filters + * @param array $result + */ + public function testMergeParameters(array $parameters, array $filters, array $result): void + { + $this->assertSame($result, ParametersManipulator::merge($parameters, $filters)); + } +} From d9e0227b589b4d7ae3b388c73c7cabcb3d2e1888 Mon Sep 17 00:00:00 2001 From: Willem Verspyck <> Date: Wed, 17 Feb 2021 14:52:45 +0100 Subject: [PATCH 11/15] Move mergeParameters to new Util\ParametersManipulator class --- src/Util/ParametersManipulator.php | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/Util/ParametersManipulator.php diff --git a/src/Util/ParametersManipulator.php b/src/Util/ParametersManipulator.php new file mode 100644 index 0000000000..d34d724bd4 --- /dev/null +++ b/src/Util/ParametersManipulator.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Util; + +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; + +/** + * @author Willem Verspyck + */ +final class ParametersManipulator +{ + /** + * Merge parameters, but replace them when it's a subarray. + * + * @param array $parameters + * @param array $filters + * + * @return array + */ + public static function merge(array $parameters, array $filters): array + { + foreach (array_intersect_key($parameters, $filters) as $key => $parameter) { + if (\is_array($parameter)) { + $parameters[$key] = array_replace($parameter, $filters[$key]); + } else { + $parameters[$key] = $filters[$key]; + } + } + + return array_merge($parameters, array_diff_key($filters, $parameters)); + } +} From a94434dfba67e5621a555fa3fe7e7de9f90b00bc Mon Sep 17 00:00:00 2001 From: Willem Verspyck <> Date: Wed, 17 Feb 2021 15:22:13 +0100 Subject: [PATCH 12/15] CS fixes --- src/Util/ParametersManipulator.php | 8 -------- tests/Util/ParametersManipulatorTest.php | 7 ------- 2 files changed, 15 deletions(-) diff --git a/src/Util/ParametersManipulator.php b/src/Util/ParametersManipulator.php index d34d724bd4..176e7667a7 100644 --- a/src/Util/ParametersManipulator.php +++ b/src/Util/ParametersManipulator.php @@ -13,9 +13,6 @@ namespace Sonata\AdminBundle\Util; -use Doctrine\Common\Collections\ArrayCollection; -use Doctrine\Common\Collections\Collection; - /** * @author Willem Verspyck */ @@ -23,11 +20,6 @@ final class ParametersManipulator { /** * Merge parameters, but replace them when it's a subarray. - * - * @param array $parameters - * @param array $filters - * - * @return array */ public static function merge(array $parameters, array $filters): array { diff --git a/tests/Util/ParametersManipulatorTest.php b/tests/Util/ParametersManipulatorTest.php index 0b78e37fba..f57fdf0bbd 100644 --- a/tests/Util/ParametersManipulatorTest.php +++ b/tests/Util/ParametersManipulatorTest.php @@ -21,9 +21,6 @@ */ class ParametersManipulatorTest extends TestCase { - /** - * @return array - */ public function provideMergeParameters(): array { return [ @@ -147,10 +144,6 @@ public function provideMergeParameters(): array /** * @dataProvider provideMergeParameters - * - * @param array $parameters - * @param array $filters - * @param array $result */ public function testMergeParameters(array $parameters, array $filters, array $result): void { From 0c75e5933a2ee67090beef7f6116e29649256923 Mon Sep 17 00:00:00 2001 From: Willem Verspyck Date: Mon, 22 Feb 2021 19:58:09 +0100 Subject: [PATCH 13/15] Set return type to "iterable" --- tests/Util/ParametersManipulatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Util/ParametersManipulatorTest.php b/tests/Util/ParametersManipulatorTest.php index f57fdf0bbd..c317462d18 100644 --- a/tests/Util/ParametersManipulatorTest.php +++ b/tests/Util/ParametersManipulatorTest.php @@ -21,7 +21,7 @@ */ class ParametersManipulatorTest extends TestCase { - public function provideMergeParameters(): array + public function provideMergeParameters(): iterable { return [ [ From 2e2d08afb3402a0efdc6eff732672017bc64c6e4 Mon Sep 17 00:00:00 2001 From: Willem Verspyck Date: Mon, 22 Feb 2021 19:59:21 +0100 Subject: [PATCH 14/15] Rename second argument --- src/Util/ParametersManipulator.php | 10 +++++----- tests/Util/ParametersManipulatorTest.php | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Util/ParametersManipulator.php b/src/Util/ParametersManipulator.php index b0995774ad..06712712cb 100644 --- a/src/Util/ParametersManipulator.php +++ b/src/Util/ParametersManipulator.php @@ -21,16 +21,16 @@ final class ParametersManipulator /** * Merge parameters, but replace them when it's a subarray. */ - public static function merge(array $parameters, array $filters): array + public static function merge(array $parameters, array $newParameters): array { - foreach (array_intersect_key($parameters, $filters) as $key => $parameter) { + foreach (array_intersect_key($parameters, $newParameters) as $key => $parameter) { if (\is_array($parameter)) { - $parameters[$key] = array_replace($parameter, $filters[$key]); + $parameters[$key] = array_replace($parameter, $newParameters[$key]); } else { - $parameters[$key] = $filters[$key]; + $parameters[$key] = $newParameters[$key]; } } - return array_merge($parameters, array_diff_key($filters, $parameters)); + return array_merge($parameters, array_diff_key($newParameters, $parameters)); } } diff --git a/tests/Util/ParametersManipulatorTest.php b/tests/Util/ParametersManipulatorTest.php index c317462d18..2dfaf8cbb9 100644 --- a/tests/Util/ParametersManipulatorTest.php +++ b/tests/Util/ParametersManipulatorTest.php @@ -145,8 +145,8 @@ public function provideMergeParameters(): iterable /** * @dataProvider provideMergeParameters */ - public function testMergeParameters(array $parameters, array $filters, array $result): void + public function testMergeParameters(array $parameters, array $newParameters, array $result): void { - $this->assertSame($result, ParametersManipulator::merge($parameters, $filters)); + $this->assertSame($result, ParametersManipulator::merge($parameters, $newParameters)); } } From fcd0bdc15e112e81a791eecc93b0f55d48389464 Mon Sep 17 00:00:00 2001 From: Willem Verspyck Date: Mon, 22 Feb 2021 20:01:10 +0100 Subject: [PATCH 15/15] Add e-mail to test --- tests/Util/ParametersManipulatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Util/ParametersManipulatorTest.php b/tests/Util/ParametersManipulatorTest.php index 2dfaf8cbb9..d42dbb51b3 100644 --- a/tests/Util/ParametersManipulatorTest.php +++ b/tests/Util/ParametersManipulatorTest.php @@ -17,7 +17,7 @@ use Sonata\AdminBundle\Util\ParametersManipulator; /** - * @author Willem Verspyck + * @author Willem Verspyck */ class ParametersManipulatorTest extends TestCase {