diff --git a/src/View/StringTemplate.php b/src/View/StringTemplate.php index da724bb11be..05d296fa008 100644 --- a/src/View/StringTemplate.php +++ b/src/View/StringTemplate.php @@ -328,15 +328,9 @@ protected function _formatAttribute($key, $value, $escape = true) /** * Adds a class and returns a unique list either in array or space separated * - * ### Options - * - * - `useIndex` if you are inputting an array with an 'element' other than 'class'. - * Also setting to 'false' will manipulate the whole array (default is 'class') - * * @param array|string $input The array or string to add the class to * @param array|string $newClass the new class or classes to add - * @param string $useIndex if you are inputting an array with an 'element' other than 'class'. - * Also setting to 'false' will manipulate the whole array (default is 'class') + * @param string $useIndex if you are inputting an array with an element other than default of 'class'. * @return array|string */ public function addClass($input, $newClass, $useIndex = 'class') @@ -346,10 +340,11 @@ public function addClass($input, $newClass, $useIndex = 'class') return $input; } - if (is_string($useIndex) && is_array($input)) { + if (is_array($input)) { $class = Hash::get($input, $useIndex, []); } else { $class = $input; + $input = []; } // Convert and sanitise the inputs @@ -367,14 +362,7 @@ public function addClass($input, $newClass, $useIndex = 'class') $class = array_unique(array_merge($class, $newClass)); - if (is_string($useIndex)) { - if (!is_array($input)) { - $input = []; - } - $input = Hash::insert($input, $useIndex, $class); - } else { - $input = $class; - } + $input = Hash::insert($input, $useIndex, $class); return $input; } diff --git a/tests/TestCase/View/StringTemplateTest.php b/tests/TestCase/View/StringTemplateTest.php index 96eba99033b..f59349d0eaa 100644 --- a/tests/TestCase/View/StringTemplateTest.php +++ b/tests/TestCase/View/StringTemplateTest.php @@ -397,16 +397,20 @@ public function testAddClassMethodUseIndex() $result = $this->template->addClass( [ - 'current_class', - 'text' + 'class' => [ + 'current_class', + 'text' + ] ], 'new_class', - false + 'non-existent' ); $this->assertEquals($result, [ - 'current_class', - 'text', - 'new_class' + 'class' => [ + 'current_class', + 'text' + ], + 'non-existent' => ['new_class'] ]); } }