Skip to content

Commit

Permalink
Removed $useIndex support of false
Browse files Browse the repository at this point in the history
  • Loading branch information
lilHermit committed Feb 6, 2017
1 parent f316b1f commit 8b05ce8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
20 changes: 4 additions & 16 deletions src/View/StringTemplate.php
Expand Up @@ -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')
Expand All @@ -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
Expand All @@ -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;
}
Expand Down
16 changes: 10 additions & 6 deletions tests/TestCase/View/StringTemplateTest.php
Expand Up @@ -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']
]);
}
}

0 comments on commit 8b05ce8

Please sign in to comment.