Version 1
[yaffs-website] / web / modules / contrib / bootstrap_layouts / src / BootstrapLayout.php
1 <?php
2
3 namespace Drupal\bootstrap_layouts;
4
5 use Drupal\Component\Utility\DiffArray;
6 use Drupal\Component\Utility\NestedArray;
7
8 /**
9  * Class BootstrapLayout.
10  */
11 class BootstrapLayout {
12
13   /**
14    * The layout data.
15    *
16    * @var array
17    */
18   protected $data;
19
20   /**
21    * The original layout data, used to determine if layout data has changed.
22    *
23    * @var array
24    */
25   protected $original;
26
27   /**
28    * BootstrapLayout constructor.
29    *
30    * @param string $id
31    *   The layout identifier.
32    * @param array $regions
33    *   The layout regions.
34    * @param array $settings
35    *   The layout settings.
36    * @param string $path
37    *   The path to the layout.
38    */
39   public function __construct($id, array $regions = [], array $settings = [], $path = NULL) {
40     $this->data = [
41       'id' => $id,
42       'regions' => $regions,
43       'settings' => $settings,
44       'path' => $path,
45     ];
46     $this->original = $this->data;
47   }
48
49   /**
50    * Indicates whether or not the layout data has changed.
51    *
52    * @return bool
53    *   TRUE or FALSE
54    */
55   public function hasChanged() {
56     return !!DiffArray::diffAssocRecursive($this->original, $this->data);
57   }
58
59   /**
60    * Retrieves the layout identifier.
61    *
62    * @return string
63    */
64   public function getId() {
65     return $this->data['id'];
66   }
67
68   /**
69    * Retrieves the path to the layout, may not be set.
70    *
71    * @return string|null
72    */
73   public function getPath() {
74     return $this->data['path'];
75   }
76
77   /**
78    * Retrieves a specific layout region.
79    *
80    * @param string $name
81    *   The layout region to retrieve.
82    * @param mixed $default_value
83    *   The default value to use if layout region does not exists.
84    *
85    * @return mixed
86    *   The layout region value or $default_value if it does not exist.
87    */
88   public function getRegion($name, $default_value = NULL) {
89     return isset($this->data['regions'][$name]) ? $this->data['regions'][$name] : $default_value;
90   }
91
92   /**
93    * Retrieves all defined layout regions.
94    *
95    * @return array
96    *   An associative array of layout regions, keyed by their machine name.
97    */
98   public function getRegions() {
99     return $this->data['regions'];
100   }
101
102   /**
103    * Retrieves a specific layout setting.
104    *
105    * @param string $name
106    *   The layout setting name. Can be dot notation to indicate a deeper key in
107    *   the settings array.
108    * @param mixed $default_value
109    *   The default value to use if layout setting does not exists.
110    *
111    * @return mixed
112    *   The layout setting value or $default_value if it does not exist.
113    */
114   public function getSetting($name, $default_value = NULL) {
115     $parts = explode('.', $name);
116     if (count($parts) === 1) {
117       return isset($this->data['settings'][$name]) ? $this->data['settings'][$name] : $default_value;
118     }
119     $value = NestedArray::getValue($this->data['settings'], $parts, $key_exists);
120     return $key_exists ? $value : NULL;
121   }
122
123   /**
124    * Retrieves all defined layout settings.
125    *
126    * @return array
127    *   An associative array of layout settings, keyed by their machine name.
128    */
129   public function getSettings() {
130     return $this->data['settings'];
131   }
132
133   /**
134    * Indicates if this layout is a Bootstrap Layouts layout.
135    *
136    * @return bool
137    *   TRUE or FALSE
138    *
139    * @todo This seems backwards, maybe refactor?
140    */
141   public function isBootstrapLayout() {
142     static $bootstrap_manager;
143     if (!isset($bootstrap_manager)) {
144       /** @var \Drupal\bootstrap_layouts\BootstrapLayoutsManager $bootstrap_manager */
145       $bootstrap_manager = \Drupal::service('plugin.manager.bootstrap_layouts');
146     }
147     return $bootstrap_manager->isBootstrapLayout($this->data['id']);
148   }
149
150   /**
151    * Sets the layout identifier.
152    *
153    * @param string $id
154    *   The layout identifier.
155    *
156    * @return \Drupal\bootstrap_layouts\BootstrapLayout
157    *   The current BootstrapLayout instance.
158    */
159   public function setId($id) {
160     $this->data['id'] = $id;
161     return $this;
162   }
163
164   /**
165    * Sets the path to the layout.
166    *
167    * @param string $path
168    *   The path to the layout.
169    *
170    * @return string|null
171    */
172   public function setPath($path) {
173     $this->data['path'] = $path;
174     return $this;
175   }
176
177   /**
178    * Sets a specific layout region.
179    *
180    * @param string $name
181    *   The layout region name.
182    * @param mixed $value
183    *   The layout region value.
184    *
185    * @return \Drupal\bootstrap_layouts\BootstrapLayout
186    *   The current BootstrapLayout instance.
187    */
188   public function setRegion($name, $value = NULL) {
189     $this->data['regions'][$name] = $value;
190     return $this;
191   }
192
193   /**
194    * Sets a specific layout setting.
195    *
196    * @param string $name
197    *   The layout setting name. Can be dot notation to indicate a deeper key in
198    *   the settings array.
199    * @param mixed $value
200    *   The layout setting value.
201    *
202    * @return \Drupal\bootstrap_layouts\BootstrapLayout
203    *   The current BootstrapLayout instance.
204    */
205   public function setSetting($name, $value = NULL) {
206     $parts = explode('.', $name);
207     if (count($parts) === 1) {
208       $this->data['settings'][$name] = $value;
209     }
210     else {
211       NestedArray::setValue($this->data['settings'], $parts, $value);
212     }
213     return $this;
214   }
215
216   /**
217    * Removes a layout region.
218    *
219    * @param string $name
220    *   The layout region to remove.
221    *
222    * @return mixed
223    *   The region that was removed.
224    */
225   public function unsetRegion($name) {
226     $old = isset($this->data['regions'][$name]) ? $this->data['regions'][$name] : NULL;
227     unset($this->data['regions'][$name]);
228     return $old;
229   }
230
231   /**
232    * Removes a layout setting.
233    *
234    * @param string $name
235    *   The layout region to remove.
236    *
237    * @return mixed
238    *   The setting that was removed.
239    */
240   public function unsetSetting($name) {
241     $old = isset($this->data['settings'][$name]) ? $this->data['settings'][$name] : NULL;
242     unset($this->data['settings'][$name]);
243     return $old;
244   }
245
246 }