3f593f2c0b2db3b256098c7ca5cb9d3206b3a7fa
[yaffs-website] / src / Utility / StorageItem.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\bootstrap\Utility\StorageItem.
5  */
6
7 namespace Drupal\bootstrap\Utility;
8
9 use Drupal\Core\KeyValueStore\MemoryStorage;
10
11 /**
12  * Theme Storage Item.
13  *
14  * This is essentially the same object as Storage. The only exception is
15  * delegating any data changes to the primary Storage object this
16  * StorageItem object lives in.
17  *
18  * This storage object can be used in `foreach` loops.
19  *
20  * @ingroup utility
21  *
22  * @see \Drupal\bootstrap\Utility\Storage
23  */
24 class StorageItem extends MemoryStorage implements \Iterator {
25
26   /**
27    * Flag determining whether or not object has been initialized yet.
28    *
29    * @var bool
30    */
31   protected $initialized = FALSE;
32
33   /**
34    * The \Drupal\bootstrap\Storage instance this item belongs to.
35    *
36    * @var \Drupal\bootstrap\Utility\Storage
37    */
38   protected $storage;
39
40   /**
41    * {@inheritdoc}
42    */
43   public function __construct($data, Storage $storage) {
44     $this->storage = $storage;
45     $this->setMultiple($data);
46     $this->initialized = TRUE;
47   }
48
49   /**
50    * Notifies the main Storage object that data has changed.
51    */
52   public function changed() {
53     if ($this->initialized) {
54       $this->storage->changed();
55     }
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function current() {
62     return current($this->data);
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function delete($key) {
69     parent::delete($key);
70     $this->changed();
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function deleteMultiple(array $keys) {
77     parent::deleteMultiple($keys);
78     $this->changed();
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function deleteAll() {
85     parent::deleteAll();
86     $this->changed();
87   }
88
89   /**
90    * Determines if the cache is empty.
91    *
92    * @return bool
93    *   TRUE or FALSE
94    */
95   public function isEmpty() {
96     return empty($this->data);
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function key() {
103     return key($this->data);
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function next() {
110     return next($this->data);
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function rename($key, $new_key) {
117     parent::rename($key, $new_key);
118     $this->changed();
119   }
120
121   /**
122    * {@inheritdoc}
123    */
124   public function rewind() {
125     return reset($this->data);
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   public function set($key, $value) {
132     parent::set($key, $value);
133     $this->changed();
134   }
135
136   /**
137    * {@inheritdoc}
138    */
139   public function setIfNotExists($key, $value) {
140     if (!isset($this->data[$key])) {
141       $this->data[$key] = $value;
142       $this->changed();
143       return TRUE;
144     }
145     return FALSE;
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   public function setMultiple(array $data) {
152     parent::setMultiple($data);
153     $this->changed();
154   }
155
156   /**
157    * {@inheritdoc}
158    */
159   public function valid() {
160     return key($this->data) !== NULL;
161   }
162
163 }