Skip to content
Permalink
Browse files

Added addClass method to StringTemplate with accompanying tests

Switched MultiCheckboxWidget and RadioWidget to use addClass
  • Loading branch information...
lilHermit committed Jan 30, 2017
1 parent 8ff6564 commit 4fb6107a77c2d3d19760be52ce11ee66f6a8a966
@@ -16,6 +16,7 @@
use Cake\Core\Configure\Engine\PhpConfig;
use Cake\Core\InstanceConfigTrait;
use Cake\Utility\Hash;
use RuntimeException;
/**
@@ -323,4 +324,65 @@ protected function _formatAttribute($key, $value, $escape = true)
return $key . '="' . ($escape ? h($value) : $value) . '"';
}
/**
* 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' called 'class' and want the same returning
* you should set this to 'class'
* - `returnArray` Setting this to `false` will return a space separated string (default is `true`)
*
* @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
* @return array|string
*/
public function addClass($input, $newClass, $options = [])
{
// NOOP
if (empty($newClass)) {
return $input;
}
$options = $options + [
'useIndex' => null,
'returnArray' => true
];
$useIndex = $options['useIndex'];
if (is_string($useIndex)) {
$class = Hash::get($input, $useIndex, []);
} else {
$class = $input;
}
// Convert and sanitise the inputs
if (!is_array($class)) {
if (is_string($class) && !empty($class)) {
$class = explode(' ', $class);
} else {
$class = [];
}
}
if (is_string($newClass)) {
$newClass = explode(' ', $newClass);
}
$class = array_unique(array_merge($class, $newClass));
if ($options['returnArray'] === false) {
$class = implode(" ", $class);
}
if (is_string($useIndex)) {
$input = Hash::insert($input, $useIndex, $class);
} else {
$input = $class;
}
return $input;
}
}
@@ -212,15 +212,7 @@ protected function _renderInput($checkbox, $context)
}
if (isset($checkbox['checked']) && $checkbox['checked']) {
if (isset($labelAttrs['class'])) {
if (is_array($labelAttrs['class'])) {
$labelAttrs['class'][] = 'selected';
} elseif (is_string($labelAttrs['class'])) {
$labelAttrs['class'] .= ' selected';
}
} else {
$labelAttrs['class'] = 'selected';
}
$labelAttrs = $this->_templates->addClass($labelAttrs, 'selected', ['useIndex' => 'class']);
}
$label = $this->_label->render($labelAttrs, $context);
@@ -183,15 +183,7 @@ protected function _renderInput($val, $text, $data, $context)
$data['label']['class'] = [];
}
if (isset($radio['checked']) && $radio['checked']) {
if (isset($data['label']['class'])) {
if (is_array($data['label']['class'])) {
$data['label']['class'][] = 'selected';
} elseif (is_string($data['label']['class'])) {
$data['label']['class'] .= ' selected';
}
} else {
$data['label']['class'] = 'selected';
}
$data['label'] = $this->_templates->addClass($data['label'], 'selected', ['useIndex' => 'class']);
}
}
@@ -300,4 +300,97 @@ public function testPushPopTemplates()
$this->assertNull($this->template->pop());
$this->assertNull($this->template->pop());
}
public function testAddClassMethod()
{
// Test new class as null, string, array, false etc
$result = $this->template->addClass([], 'new_class');
$this->assertEquals($result, ['new_class']);
$result = $this->template->addClass([], ['new_class']);
$this->assertEquals($result, ['new_class']);
$result = $this->template->addClass([], false);
$this->assertEquals($result, []);
$result = $this->template->addClass([], null);
$this->assertEquals($result, []);
$result = $this->template->addClass(null, null);
$this->assertNull($result);
// Test current class as null, string, array, false etc
$result = $this->template->addClass(['current'], 'new_class');
$this->assertEquals($result, ['current', 'new_class']);
$result = $this->template->addClass('current', 'new_class');
$this->assertEquals($result, ['current', 'new_class']);
$result = $this->template->addClass('', 'new_class');
$this->assertEquals($result, ['new_class']);
$result = $this->template->addClass(null, 'new_class');
$this->assertEquals($result, ['new_class']);
$result = $this->template->addClass(false, 'new_class');
$this->assertEquals($result, ['new_class']);
$result = $this->template->addClass(new \StdClass(), 'new_class');
$this->assertEquals($result, ['new_class']);
// Test we don't repeat the class
$result = $this->template->addClass(['new_class'], 'new_class');
$this->assertEquals($result, ['new_class']);
}
public function testAddClassMethodOptions()
{
// Test useIndex option
$result = $this->template->addClass(
[
'class' => 'current_class',
'other_index1' => false,
'type' => 'text'
],
'new_class',
[
'useIndex' => 'class'
]
);
$this->assertEquals($result, [
'class' => ['current_class', 'new_class'],
'other_index1' => false,
'type' => 'text'
]);
// Test the use index which doesn't exist in input
$result = $this->template->addClass(
[
'class' => 'current_class',
'other_index1' => false,
'type' => 'text'
],
'new_class',
[
'useIndex' => 'non_existent_class'
]
);
$this->assertEquals($result, [
'class' => 'current_class',
'other_index1' => false,
'type' => 'text',
'non_existent_class' => ['new_class']
]);
// Test returnArray option (will return space separated string
$result = $this->template->addClass(
['current_class'],
'new_class',
[
'returnArray' => false
]
);
$this->assertEquals($result, 'current_class new_class');
$result = $this->template->addClass(
null,
'new_class',
[
'returnArray' => false
]
);
$this->assertEquals($result, 'new_class');
}
}

0 comments on commit 4fb6107

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