Skip to content

Commit

Permalink
Removed the asString option from StringTemplate::addClass method
Browse files Browse the repository at this point in the history
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 f316b1f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 58 deletions.
16 changes: 3 additions & 13 deletions src/View/StringTemplate.php
Expand Up @@ -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 {
Expand All @@ -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 = [];
Expand Down
50 changes: 5 additions & 45 deletions tests/TestCase/View/StringTemplateTest.php
Expand Up @@ -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(
[
Expand All @@ -372,9 +372,7 @@ public function testAddClassMethodUseIndexOption()
'type' => 'text'
],
'new_class',
[
'useIndex' => 'class'
]
'class'
);
$this->assertEquals($result, [
'class' => ['current_class', 'new_class'],
Expand All @@ -389,9 +387,7 @@ public function testAddClassMethodUseIndexOption()
'type' => 'text'
],
'new_class',
[
'useIndex' => 'my_class'
]
'my_class'
);
$this->assertEquals($result, [
'other_index1' => false,
Expand All @@ -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.