AttributeArray.php

Definition of Drupal\Core\Template\AttributeArray.

Namespace

Drupal\Core\Template

File

drupal/core/lib/Drupal/Core/Template/AttributeArray.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\Core\Template\AttributeArray.
  5. */
  6. namespace Drupal\Core\Template;
  7. use Drupal\Component\Utility\String;
  8. /**
  9. * A class that defines a type of Attribute that can be added to as an array.
  10. *
  11. * To use with Attribute, the array must be specified.
  12. * Correct:
  13. * @code
  14. * $attributes = new Attribute(array());
  15. * $attributes['class'] = array();
  16. * $attributes['class'][] = 'cat';
  17. * @endcode
  18. * Incorrect:
  19. * @code
  20. * $attributes = new Attribute(array());
  21. * $attributes['class'][] = 'cat';
  22. * @endcode
  23. *
  24. * @see Drupal\Core\Template\Attribute
  25. */
  26. class AttributeArray extends AttributeValueBase implements \ArrayAccess, \IteratorAggregate {
  27. /**
  28. * Implements ArrayAccess::offsetGet().
  29. */
  30. public function offsetGet($offset) {
  31. return $this->value[$offset];
  32. }
  33. /**
  34. * Implements ArrayAccess::offsetSet().
  35. */
  36. public function offsetSet($offset, $value) {
  37. if (isset($offset)) {
  38. $this->value[$offset] = $value;
  39. }
  40. else {
  41. $this->value[] = $value;
  42. }
  43. }
  44. /**
  45. * Implements ArrayAccess::offsetUnset().
  46. */
  47. public function offsetUnset($offset) {
  48. unset($this->value[$offset]);
  49. }
  50. /**
  51. * Implements ArrayAccess::offsetExists().
  52. */
  53. public function offsetExists($offset) {
  54. return isset($this->value[$offset]);
  55. }
  56. /**
  57. * Implements the magic __toString() method.
  58. */
  59. public function __toString() {
  60. $this->printed = TRUE;
  61. return implode(' ', array_map(array('Drupal\Component\Utility\String', 'checkPlain'), $this->value));
  62. }
  63. /**
  64. * Implements IteratorAggregate::getIterator().
  65. */
  66. public function getIterator() {
  67. return new \ArrayIterator($this->value);
  68. }
  69. /**
  70. * Returns the whole array.
  71. */
  72. public function value() {
  73. return $this->value;
  74. }
  75. }

Classes

Namesort descending Description
AttributeArray A class that defines a type of Attribute that can be added to as an array.