From 87dbda5b3006b9d26e027a1c68a127b360f3e949 Mon Sep 17 00:00:00 2001 From: Simon Asika Date: Wed, 7 Jun 2017 16:22:45 +0800 Subject: [PATCH] addClass and other operations --- src/Form/Field/AbstractField.php | 71 ++++++++++++++++++++--- src/Form/Field/AbstractHtml5Field.php | 3 +- src/Form/Test/Field/AbstractFieldTest.php | 42 ++++++++++++++ 3 files changed, 106 insertions(+), 10 deletions(-) diff --git a/src/Form/Field/AbstractField.php b/src/Form/Field/AbstractField.php index d8e813f15..4e88091f6 100644 --- a/src/Form/Field/AbstractField.php +++ b/src/Form/Field/AbstractField.php @@ -21,7 +21,17 @@ /** * The AbstractField class. - * + * + * @method $this class($value) + * @method $this labelClass($value) + * @method $this controlClass($value) + * @method $this addClass($value) + * @method $this removeClass($value) + * @method $this addLabelClass($value) + * @method $this removeLabelClass($value) + * @method $this addControlClass($value) + * @method $this removeControlClass($value) + * * @since 2.0 */ abstract class AbstractField @@ -937,6 +947,8 @@ public function readonly($value = true) * @param string $value * * @return static + * + * @deprecated Use class() instead. */ public function setClass($value) { @@ -946,17 +958,41 @@ public function setClass($value) } /** - * labelClass + * addClassName * - * @param string $value + * @param string $to + * @param mixed $value * * @return static + * + * @TODO Use Accessors and magic call to handle all attributes. */ - public function labelClass($value) + protected function addClassName($to = 'class', $value) { - $this->setAttribute('labelClass', $value); + $classes = explode(' ', (string) $this->getAttribute($to)); - return $this; + $value = array_merge($classes, is_string($value) ? explode(' ', $value) : $value); + + return $this->set($to, implode(' ', array_unique($value))); + } + + /** + * removeClass + * + * @param string $from + * @param string|array $value + * + * @return static + * + * @TODO Use Accessors and magic call to handle all attributes. + */ + public function removeClassName($from = 'class', $value) + { + $classes = explode(' ', (string) $this->getAttribute($from)); + + $value = array_diff($classes, is_string($value) ? explode(' ', $value) : $value); + + return $this->set($from, implode(' ', array_unique($value))); } /** @@ -987,7 +1023,6 @@ public function setAttribute($name, $value) return $this; } - /** * Get attribute. Alias of `getAttribute()`. * @@ -1246,7 +1281,11 @@ public function escape($text) */ protected function getAccessors() { - return []; + return [ + 'class', + 'labelClass', + 'controlClass' + ]; } /** @@ -1279,6 +1318,22 @@ public function __call($method, $args) } } + switch($method) + { + case 'addClass': + return $this->addClassName('class', $args[0]); + case 'removeClass': + return $this->removeClassName('class', $args[0]); + case 'addLabelClass': + return $this->addClassName('labelClass', $args[0]); + case 'removeLabelClass': + return $this->removeClassName('labelClass', $args[0]); + case 'addControlClass': + return $this->addClassName('controlClass', $args[0]); + case 'removeControlClass': + return $this->removeClassName('controlClass', $args[0]); + } + throw new \BadMethodCallException(sprintf('Method: %s not exists', $method)); } } diff --git a/src/Form/Field/AbstractHtml5Field.php b/src/Form/Field/AbstractHtml5Field.php index 006cdb5c4..fbe1165fb 100644 --- a/src/Form/Field/AbstractHtml5Field.php +++ b/src/Form/Field/AbstractHtml5Field.php @@ -51,7 +51,6 @@ protected function getAccessors() 'min' => 'min', 'step' => 'step', 'patten' => 'patten', - ] - ); + ]); } } diff --git a/src/Form/Test/Field/AbstractFieldTest.php b/src/Form/Test/Field/AbstractFieldTest.php index 07fc3b573..134aa6c5f 100644 --- a/src/Form/Test/Field/AbstractFieldTest.php +++ b/src/Form/Test/Field/AbstractFieldTest.php @@ -562,6 +562,48 @@ public function testSetControl() ); } + /** + * testAddClass + * + * @return void + */ + public function testAddClass() + { + $this->instance->addClass('foo'); + + self::assertEquals('stub-flower foo', $this->instance->get('class')); + + $this->instance->addClass(['stub-flower', 'bar']); + + self::assertEquals('stub-flower foo bar', $this->instance->get('class')); + + $this->instance->addClass(['yoo goo']); + + self::assertEquals('stub-flower foo bar yoo goo', $this->instance->get('class')); + } + + /** + * testRemoveClass + * + * @return void + */ + public function testRemoveClass() + { + $this->instance->addClass('foo bar yoo goo'); + + $this->instance->removeClass('bar'); + + self::assertEquals('stub-flower foo yoo goo', $this->instance->get('class')); + + $this->instance->removeClass('yoo goo'); + + self::assertEquals('stub-flower foo', $this->instance->get('class')); + + $this->instance->removeClass(['foo', 'yoo']); + + self::assertEquals('stub-flower', $this->instance->get('class')); + } + /** * Method to test getAttribute(). *