Commit 80c73227 authored by alexpott's avatar alexpott

Issue #2325517 by lauriii, Alienpruts, Tom Verhaeghe, joelpittet, rteijeiro |...

Issue #2325517 by lauriii, Alienpruts, Tom Verhaeghe, joelpittet, rteijeiro | Cottser: Add methods for adding/removing attributes (not classes) on Attribute objects.
parent 44f8f787
...@@ -160,6 +160,47 @@ public function addClass() { ...@@ -160,6 +160,47 @@ public function addClass() {
return $this; return $this;
} }
/**
* Sets values for an attribute key.
*
* @param string $attribute
* Name of the attribute.
* @param string|array $value
* Value(s) to set for the given attribute key.
*
* @return $this
*/
public function setAttribute($attribute, $value) {
$this->offsetSet($attribute, $value);
return $this;
}
/**
* Removes an attribute from an Attribute object.
*
* @param string|array ...
* Attributes to remove from the attribute array.
*
* @return $this
*/
public function removeAttribute() {
$args = func_get_args();
foreach ($args as $arg) {
// Support arrays or multiple arguments.
if (is_array($arg)) {
foreach ($arg as $value) {
unset($this->storage[$value]);
}
}
else {
unset($this->storage[$arg]);
}
}
return $this;
}
/** /**
* Removes argument values from array of existing CSS classes. * Removes argument values from array of existing CSS classes.
* *
......
...@@ -86,13 +86,6 @@ public function getIterator() { ...@@ -86,13 +86,6 @@ public function getIterator() {
return new \ArrayIterator($this->value); return new \ArrayIterator($this->value);
} }
/**
* Returns the whole array.
*/
public function value() {
return $this->value;
}
/** /**
* Exchange the array for another one. * Exchange the array for another one.
* *
......
...@@ -61,6 +61,13 @@ public function render() { ...@@ -61,6 +61,13 @@ public function render() {
} }
} }
/**
* Returns the raw value.
*/
public function value() {
return $this->value;
}
/** /**
* Implements the magic __toString() method. * Implements the magic __toString() method.
*/ */
......
...@@ -25,6 +25,11 @@ public function testConstructor() { ...@@ -25,6 +25,11 @@ public function testConstructor() {
$attribute = new Attribute(array('class' => array('example-class'))); $attribute = new Attribute(array('class' => array('example-class')));
$this->assertTrue(isset($attribute['class'])); $this->assertTrue(isset($attribute['class']));
$this->assertEquals(new AttributeArray('class', array('example-class')), $attribute['class']); $this->assertEquals(new AttributeArray('class', array('example-class')), $attribute['class']);
// Test adding boolean attributes through the constructor.
$attribute = new Attribute(['selected' => TRUE, 'checked' => FALSE]);
$this->assertTrue($attribute['selected']->value());
$this->assertFalse($attribute['checked']->value());
} }
/** /**
...@@ -57,6 +62,73 @@ public function testRemove() { ...@@ -57,6 +62,73 @@ public function testRemove() {
$this->assertFalse(isset($attribute['class'])); $this->assertFalse(isset($attribute['class']));
} }
/**
* Tests setting attributes.
* @covers ::setAttribute()
*/
public function testSetAttribute() {
$attribute = new Attribute();
// Test adding various attributes.
$attributes = ['alt', 'id', 'src', 'title', 'value'];
foreach ($attributes as $key) {
foreach (['kitten', ''] as $value) {
$attribute = new Attribute();
$attribute->setAttribute($key, $value);
$this->assertEquals($value, $attribute[$key]);
}
}
// Test adding array to class.
$attribute = new Attribute();
$attribute->setAttribute('class', ['kitten', 'cat']);
$this->assertArrayEquals(['kitten', 'cat'], $attribute['class']->value());
// Test adding boolean attributes.
$attribute = new Attribute();
$attribute['checked'] = TRUE;
$this->assertTrue($attribute['checked']->value());
}
/**
* Tests removing attributes.
* @covers ::removeAttribute()
*/
public function testRemoveAttribute() {
$attributes = [
'alt' => 'Alternative text',
'id' => 'bunny',
'src' => 'zebra',
'style' => 'color: pink;',
'title' => 'kitten',
'value' => 'ostrich',
'checked' => TRUE,
];
$attribute = new Attribute($attributes);
// Single value.
$attribute->removeAttribute('alt');
$this->assertEmpty($attribute['alt']);
// Multiple values.
$attribute->removeAttribute('id', 'src');
$this->assertEmpty($attribute['id']);
$this->assertEmpty($attribute['src']);
// Single value in array.
$attribute->removeAttribute(['style']);
$this->assertEmpty($attribute['style']);
// Boolean value.
$attribute->removeAttribute('checked');
$this->assertEmpty($attribute['checked']);
// Multiple values in array.
$attribute->removeAttribute(['title', 'value']);
$this->assertEmpty((string) $attribute);
}
/** /**
* Tests adding class attributes with the AttributeArray helper method. * Tests adding class attributes with the AttributeArray helper method.
* @covers ::addClass() * @covers ::addClass()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment