-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractRegistry.php
More file actions
116 lines (101 loc) · 2.45 KB
/
AbstractRegistry.php
File metadata and controls
116 lines (101 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/*
* This file is part of the Panda Registry Package.
*
* (c) Ioannis Papikas <papikas.ioan@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Panda\Registry;
use InvalidArgumentException;
use Panda\Support\Helpers\ArrayHelper;
use Panda\Support\Helpers\StringHelper;
/**
* Class AbstractRegistry
* @package Panda\Registry
*/
abstract class AbstractRegistry implements RegistryInterface
{
/**
* @return array
*/
abstract public function getItems();
/**
* @param array $items
*/
abstract public function setItems(array $items);
/**
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get($key, $default = null)
{
return ArrayHelper::get($this->getItems(), $key, $default, $useDotSyntax = true);
}
/**
* @param string $key
* @param mixed $value
*
* @return array
* @throws InvalidArgumentException
*/
public function set($key, $value)
{
$registry = ArrayHelper::set($this->getItems(), $key, $value, $useDotSyntax = true);
$this->setItems($registry);
return $this->getItems();
}
/**
* @param string $key
*
* @return bool
*/
public function exists($key)
{
return ArrayHelper::exists($this->getItems(), $key, $useDotSyntax = true);
}
/**
* {@inheritdoc}
*/
public function offsetExists($offset)
{
return $this->exists($offset);
}
/**
* {@inheritdoc}
*/
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* {@inheritdoc}
*/
public function offsetSet($offset, $value)
{
// Normalize offset
if (StringHelper::emptyString($offset, true)) {
// Get next key
$numericItems = ArrayHelper::filter($this->getItems(), function ($key) {
if (is_numeric($key) && is_int($key) && $key >= 0) {
return true;
}
return false;
}, []);
$keys = array_keys($numericItems);
$offset = count($keys) > 0 ? max($keys) + 1 : 0;
}
// Set
$this->set($offset, $value);
}
/**
* {@inheritdoc}
*/
public function offsetUnset($offset)
{
$this->set($offset, null);
}
}