Skip to content
Permalink
Browse files

Removed the `asString` option from `StringTemplate::addClass` method

Switched from $options array to string `$useIndex` as we only have one option now
  • Loading branch information...
lilHermit committed Feb 3, 2017
1 parent ebde157 commit f316b1f87da583fd8dc4b146a013aa2a5a08a027
Showing with 8 additions and 58 deletions.
  1. +3 −13 src/View/StringTemplate.php
  2. +5 −45 tests/TestCase/View/StringTemplateTest.php
@@ -332,26 +332,20 @@ protected function _formatAttribute($key, $value, $escape = true)
*
* - `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')
* - `asString` Setting this to `true` will return a space separated string (default is `false`)
*
* @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 array $options See above for options
* @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')
* @return array|string
*/
public function addClass($input, $newClass, $options = [])
public function addClass($input, $newClass, $useIndex = 'class')
{
// NOOP
if (empty($newClass)) {
return $input;
}
$options += [
'useIndex' => 'class',
'asString' => false
];
$useIndex = $options['useIndex'];
if (is_string($useIndex) && is_array($input)) {
$class = Hash::get($input, $useIndex, []);
} else {
@@ -373,10 +367,6 @@ public function addClass($input, $newClass, $options = [])
$class = array_unique(array_merge($class, $newClass));
if ($options['asString'] === true) {
$class = implode(' ', $class);
}
if (is_string($useIndex)) {
if (!is_array($input)) {
$input = [];
@@ -359,11 +359,11 @@ public function testAddClassMethodUnique()
}
/**
* Test addClass method useIndex option
* Test addClass method useIndex param
*
* Tests for useIndex being the default, 'my_class' and false
*/
public function testAddClassMethodUseIndexOption()
public function testAddClassMethodUseIndex()
{
$result = $this->template->addClass(
[
@@ -372,9 +372,7 @@ public function testAddClassMethodUseIndexOption()
'type' => 'text'
],
'new_class',
[
'useIndex' => 'class'
]
'class'
);
$this->assertEquals($result, [
'class' => ['current_class', 'new_class'],
@@ -389,9 +387,7 @@ public function testAddClassMethodUseIndexOption()
'type' => 'text'
],
'new_class',
[
'useIndex' => 'my_class'
]
'my_class'
);
$this->assertEquals($result, [
'other_index1' => false,
@@ -405,48 +401,12 @@ public function testAddClassMethodUseIndexOption()
'text'
],
'new_class',
[
'useIndex' => false
]
false
);
$this->assertEquals($result, [
'current_class',
'text',
'new_class'
]);
}
/**
* Test addClass method to make sure `asString` option is handle correctly
*/
public function testAddClassMethodAsStringOption()
{
$result = $this->template->addClass(
['class' => 'current_class'],
'new_class',
[
'asString' => true
]
);
$this->assertEquals($result, ['class' => 'current_class new_class']);
$result = $this->template->addClass(
['current_class'],
'new_class',
[
'useIndex' => false,
'asString' => true
]
);
$this->assertEquals($result, 'current_class new_class');
$result = $this->template->addClass(
null,
'new_class',
[
'asString' => true
]
);
$this->assertEquals($result, ['class' => 'new_class']);
}
}

0 comments on commit f316b1f

Please sign in to comment.
You can’t perform that action at this time.