d738cdca96b1e7de626e2dd735da64800862c010
[yaffs-website] / src / Utility / Variables.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Utility\Variables.
5  */
6
7 namespace Drupal\bootstrap\Utility;
8
9 /**
10  * Class to help modify template variables.
11  *
12  * @ingroup utility
13  */
14 class Variables extends DrupalAttributes {
15
16   /**
17    * An element object.
18    *
19    * @var \Drupal\bootstrap\Utility\Element|FALSE
20    */
21   public $element = FALSE;
22
23   /**
24    * Element constructor.
25    *
26    * @param array $variables
27    *   A theme hook variables array.
28    */
29   public function __construct(array &$variables) {
30     $this->array = &$variables;
31     if (isset($variables['element']) && Element::isRenderArray($variables['element'])) {
32       $this->element = Element::create($variables['element']);
33     }
34     elseif (isset($variables['elements']) && Element::isRenderArray($variables['elements'])) {
35       $this->element = Element::create($variables['elements']);
36     }
37   }
38
39   /**
40    * Creates a new \Drupal\bootstrap\Utility\Variables instance.
41    *
42    * @param array $variables
43    *   A theme hook variables array.
44    *
45    * @return \Drupal\bootstrap\Utility\Variables
46    *   The newly created variables instance.
47    */
48   public static function create(array &$variables) {
49     return new self($variables);
50   }
51
52   /**
53    * Retrieves a context value from the variables array or its element, if any.
54    *
55    * @param string $name
56    *   The name of the context key to retrieve.
57    * @param mixed $default
58    *   Optional. The default value to use if the context $name isn't set.
59    *
60    * @return mixed|NULL
61    *   The context value or the $default value if not set.
62    */
63   public function &getContext($name, $default = NULL) {
64     $context = &$this->offsetGet($this->attributePrefix . 'context', []);
65     if (!isset($context[$name])) {
66       // If there is no context on the variables array but there is an element
67       // present, proxy the method to the element.
68       if ($this->element) {
69         return $this->element->getContext($name, $default);
70       }
71       $context[$name] = $default;
72     }
73     return $context[$name];
74   }
75
76   /**
77    * Maps an element's properties to the variables attributes array.
78    *
79    * @param array $map
80    *   An associative array whose keys are element property names and whose
81    *   values are the variable names to set in the variables array; e.g.,
82    *   array('#property_name' => 'variable_name'). If both names are identical
83    *   except for the leading '#', then an attribute name value is sufficient
84    *   and no property name needs to be specified.
85    * @param bool $overwrite
86    *   If the variable exists, it will be overwritten. This does not apply to
87    *   attribute arrays, they will always be merged recursively.
88    *
89    * @return $this
90    */
91   public function map(array $map, $overwrite = TRUE) {
92     // Immediately return if there is no element in the variable array.
93     if (!$this->element) {
94       return $this;
95     }
96
97     // Iterate over each map item.
98     foreach ($map as $property => $variable) {
99       // If the key is numeric, the attribute name needs to be taken over.
100       if (is_int($property)) {
101         $property = $variable;
102       }
103
104       // Merge attributes from the element.
105       if (strpos($property, 'attributes') !== FALSE) {
106         $this->setAttributes($this->element->getAttributes($property)->getArrayCopy(), $variable);
107       }
108       // Set normal variable.
109       elseif ($overwrite || !$this->offsetExists($variable)) {
110         $this->offsetSet($variable, $this->element->getProperty($property));
111       }
112     }
113     return $this;
114   }
115
116 }