Skip to content

Commit

Permalink
addClass and other operations
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Jun 7, 2017
1 parent db47bdd commit 87dbda5
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 10 deletions.
71 changes: 63 additions & 8 deletions src/Form/Field/AbstractField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -937,6 +947,8 @@ public function readonly($value = true)
* @param string $value
*
* @return static
*
* @deprecated Use class() instead.
*/
public function setClass($value)
{
Expand All @@ -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)));
}

/**
Expand Down Expand Up @@ -987,7 +1023,6 @@ public function setAttribute($name, $value)
return $this;
}


/**
* Get attribute. Alias of `getAttribute()`.
*
Expand Down Expand Up @@ -1246,7 +1281,11 @@ public function escape($text)
*/
protected function getAccessors()
{
return [];
return [
'class',
'labelClass',
'controlClass'
];
}

/**
Expand Down Expand Up @@ -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));
}
}
3 changes: 1 addition & 2 deletions src/Form/Field/AbstractHtml5Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ protected function getAccessors()
'min' => 'min',
'step' => 'step',
'patten' => 'patten',
]
);
]);
}
}
42 changes: 42 additions & 0 deletions src/Form/Test/Field/AbstractFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*
Expand Down

0 comments on commit 87dbda5

Please sign in to comment.