WooCommerce
1 week ago
WordPress
1 week ago
ArrayContainer.php
1 week ago
ReferenceArrayContainer.php
1 week ago
ArrayContainer.php
49 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FSVendor\WPDesk\Persistence\Adapter; |
| 4 | |
| 5 | use FSVendor\WPDesk\Persistence\ElementNotExistsException; |
| 6 | use FSVendor\WPDesk\Persistence\PersistentContainer; |
| 7 | /** |
| 8 | * Container that uses array as a persistent memory. |
| 9 | * |
| 10 | * @package WPDesk\Persistence |
| 11 | */ |
| 12 | class ArrayContainer implements PersistentContainer |
| 13 | { |
| 14 | /** @var array */ |
| 15 | protected $array; |
| 16 | public function __construct(array $initial = []) |
| 17 | { |
| 18 | $this->array = $initial; |
| 19 | } |
| 20 | public function set($id, $value) |
| 21 | { |
| 22 | $this->array[$id] = $value; |
| 23 | } |
| 24 | public function delete($id) |
| 25 | { |
| 26 | unset($this->array[$id]); |
| 27 | } |
| 28 | public function has($id) |
| 29 | { |
| 30 | return key_exists($id, $this->array); |
| 31 | } |
| 32 | public function get($id) |
| 33 | { |
| 34 | if (!isset($this->array[$id])) { |
| 35 | throw new ElementNotExistsException(sprintf('Element %s not exists!', $id)); |
| 36 | } |
| 37 | return $this->array[$id]; |
| 38 | } |
| 39 | /** |
| 40 | * Return array that is used internally to save the data. |
| 41 | * |
| 42 | * @return array |
| 43 | */ |
| 44 | public function get_array() |
| 45 | { |
| 46 | return $this->array; |
| 47 | } |
| 48 | } |
| 49 |