1 <?php
2
3 namespace Docolight\Support;
4
5 use ArrayAccess;
6
7 /**
8 * A class to access your items in dot notation.
9 *
10 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
11 */
12 class Repository implements ArrayAccess
13 {
14 /**
15 * All of the configuration items.
16 *
17 * @var array
18 */
19 protected $items = [];
20
21 /**
22 * Create a new configuration repository.
23 *
24 * @param array $items
25 */
26 public function __construct(array $items = array())
27 {
28 $this->items = $items;
29 }
30
31 /**
32 * Determine if the given configuration value exists.
33 *
34 * @param string $key
35 *
36 * @return bool
37 */
38 public function has($key)
39 {
40 return array_has($this->items, $key);
41 }
42
43 /**
44 * Get the specified configuration value.
45 *
46 * @param string $key
47 * @param mixed $default
48 *
49 * @return mixed
50 */
51 public function get($key, $default = null)
52 {
53 return array_get($this->items, $key, $default);
54 }
55
56 /**
57 * Set a given configuration value.
58 *
59 * @param array|string $key
60 * @param mixed $value
61 */
62 public function set($key, $value = null)
63 {
64 if (is_array($key)) {
65 foreach ($key as $innerKey => $innerValue) {
66 array_set($this->items, $innerKey, $innerValue);
67 }
68 } else {
69 array_set($this->items, $key, $value);
70 }
71 }
72
73 /**
74 * Prepend a value onto an array configuration value.
75 *
76 * @param string $key
77 * @param mixed $value
78 */
79 public function prepend($key, $value)
80 {
81 $array = $this->get($key);
82
83 array_unshift($array, $value);
84
85 $this->set($key, $array);
86 }
87
88 /**
89 * Push a value onto an array configuration value.
90 *
91 * @param string $key
92 * @param mixed $value
93 */
94 public function push($key, $value)
95 {
96 $array = $this->get($key);
97
98 $array[] = $value;
99
100 $this->set($key, $array);
101 }
102
103 /**
104 * Get all of the configuration items for the application.
105 *
106 * @return array
107 */
108 public function all()
109 {
110 return $this->items;
111 }
112
113 /**
114 * Determine if the given configuration option exists.
115 *
116 * @param string $key
117 *
118 * @return bool
119 */
120 public function offsetExists($key)
121 {
122 return $this->has($key);
123 }
124
125 /**
126 * Get a configuration option.
127 *
128 * @param string $key
129 *
130 * @return mixed
131 */
132 public function offsetGet($key)
133 {
134 return $this->get($key);
135 }
136
137 /**
138 * Set a configuration option.
139 *
140 * @param string $key
141 * @param mixed $value
142 */
143 public function offsetSet($key, $value)
144 {
145 $this->set($key, $value);
146 }
147
148 /**
149 * Unset a configuration option.
150 *
151 * @param string $key
152 */
153 public function offsetUnset($key)
154 {
155 $this->set($key, null);
156 }
157 }
158