Skip to content

Commit 2fdcf47

Browse files
authored
Add files via upload
1 parent 13e45f4 commit 2fdcf47

File tree

2 files changed

+273
-0
lines changed

2 files changed

+273
-0
lines changed

composer.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "adbario/php-dot-notation",
3+
"description": "PHP dot notation array access",
4+
"keywords": ["php", "dotnotation", "arrayaccess"],
5+
"homepage": "https://github.com/adbario/php-dot-notation",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "AdBar"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"AdBar\\": "src"
15+
}
16+
}
17+
}

src/Dot.php

+256
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
<?php
2+
3+
namespace AdBar;
4+
5+
use ArrayAccess;
6+
7+
/**
8+
* Dot Notation
9+
*
10+
* This class provides dot notation access to arrays, so it's easy to handle
11+
* multidimensional data in a clean way.
12+
*/
13+
class Dot implements ArrayAccess
14+
{
15+
/** @var array Data */
16+
protected $data = [];
17+
18+
/**
19+
* Constructor
20+
*
21+
* @param array|null $data Data
22+
*/
23+
public function __construct(array $data = null)
24+
{
25+
if ($data !== null) {
26+
$this->data = $data;
27+
}
28+
}
29+
30+
/**
31+
* Set value or array of values to path
32+
*
33+
* @param mixed $key Path or array of paths and values
34+
* @param mixed $value Value to set if path is not an array
35+
*/
36+
public function set($key, $value = null)
37+
{
38+
if (is_string($key)) {
39+
if (is_array($value)) {
40+
// Iterate values
41+
foreach ($value as $k => $v) {
42+
$this->set("$key.$k", $v);
43+
}
44+
} else {
45+
// Iterate path
46+
$keys = explode('.', $key);
47+
$data = &$this->data;
48+
foreach ($keys as $key) {
49+
if (!isset($data[$key]) || !is_array($data[$key])) {
50+
$data[$key] = [];
51+
}
52+
$data = &$data[$key];
53+
}
54+
// Set value to path
55+
$data = $value;
56+
}
57+
} elseif (is_array($key)) {
58+
// Iterate array of paths and values
59+
foreach ($key as $k => $v) {
60+
$this->set($k, $v);
61+
}
62+
}
63+
}
64+
65+
/**
66+
* Add value or array of values to path
67+
*
68+
* @param mixed $key Path or array of paths and values
69+
* @param mixed $value Value to set if path is not an array
70+
* @param boolean $pop Helper to pop out last key if value is an array
71+
*/
72+
public function add($key, $value = null, $pop = false)
73+
{
74+
if (is_string($key)) {
75+
if (is_array($value)) {
76+
// Iterate values
77+
foreach ($value as $k => $v) {
78+
$this->add("$key.$k", $v, true);
79+
}
80+
} else {
81+
// Iterate path
82+
$keys = explode('.', $key);
83+
$data = &$this->data;
84+
if ($pop === true) {
85+
array_pop($keys);
86+
}
87+
foreach ($keys as $key) {
88+
if (!isset($data[$key]) || !is_array($data[$key])) {
89+
$data[$key] = [];
90+
}
91+
$data = &$data[$key];
92+
}
93+
// Add value to path
94+
$data[] = $value;
95+
}
96+
} elseif (is_array($key)) {
97+
// Iterate array of paths and values
98+
foreach ($key as $k => $v) {
99+
$this->add($k, $v);
100+
}
101+
}
102+
}
103+
104+
/**
105+
* Get value of path, default value if path doesn't exist or all data
106+
*
107+
* @param string $key Path
108+
* @param mixed $default Default value
109+
* @return mixed Value of path
110+
*/
111+
public function get($key = null, $default = null)
112+
{
113+
if (is_string($key)) {
114+
// Iterate path
115+
$keys = explode('.', $key);
116+
$data = &$this->data;
117+
foreach ($keys as $key) {
118+
if (!isset($data[$key])) {
119+
return $default;
120+
}
121+
$data = &$data[$key];
122+
}
123+
// Get value
124+
return $data;
125+
} elseif (is_null($key)) {
126+
// Get all data
127+
return $this->data;
128+
}
129+
}
130+
131+
/**
132+
* Check if path exists
133+
*
134+
* @param string $key Path
135+
* @return boolean
136+
*/
137+
public function has(string $key)
138+
{
139+
$keys = explode('.', $key);
140+
$data = &$this->data;
141+
foreach ($keys as $key) {
142+
if (!isset($data[$key])) {
143+
return false;
144+
}
145+
$data = &$data[$key];
146+
}
147+
148+
return true;
149+
}
150+
151+
/**
152+
* Delete path or array of paths
153+
*
154+
* @param mixed $key Path or array of paths to delete
155+
*/
156+
public function delete($key)
157+
{
158+
if (is_string($key)) {
159+
// Iterate path
160+
$keys = explode('.', $key);
161+
$data = &$this->data;
162+
$last = array_pop($keys);
163+
foreach ($keys as $key) {
164+
if (!isset($data[$key])) {
165+
return;
166+
}
167+
$data = &$data[$key];
168+
}
169+
if (isset($data[$last])) {
170+
// Detele path
171+
unset($data[$last]);
172+
}
173+
} elseif (is_array($key)) {
174+
// Iterate array of paths
175+
foreach ($key as $k) {
176+
$this->delete($k);
177+
}
178+
}
179+
}
180+
181+
/**
182+
* Delete all data, data from path or array of paths and
183+
* optionally format path if it doesn't exist
184+
*
185+
* @param mixed $key Path or array of paths to clean
186+
* @param boolean $format Format option
187+
*/
188+
public function clear($key = null, $format = false)
189+
{
190+
if (is_string($key)) {
191+
// Iterate path
192+
$keys = explode('.', $key);
193+
$data = &$this->data;
194+
foreach ($keys as $key) {
195+
if (!isset($data[$key]) || !is_array($data[$key])) {
196+
if ($format === true) {
197+
$data[$key] = [];
198+
} else {
199+
return;
200+
}
201+
}
202+
$data = &$data[$key];
203+
}
204+
// Clear path
205+
$data = [];
206+
} elseif (is_array($key)) {
207+
// Iterate array
208+
foreach ($key as $k) {
209+
$this->clear($k, $format);
210+
}
211+
} elseif (is_null($key)) {
212+
// Clear all data
213+
$this->data = [];
214+
}
215+
}
216+
217+
/**
218+
* Set data
219+
*
220+
* @param array $data
221+
*/
222+
public function setData(array $data)
223+
{
224+
$this->data = $data;
225+
}
226+
227+
/**
228+
* Set data as a reference
229+
*
230+
* @param array $data
231+
*/
232+
public function setDataAsRef(array &$data)
233+
{
234+
$this->data = &$data;
235+
}
236+
237+
/**
238+
* ArrayAccess abstract methods
239+
*/
240+
public function offsetSet($offset, $value)
241+
{
242+
$this->set($offset, $value);
243+
}
244+
public function offsetExists($offset)
245+
{
246+
return $this->has($offset);
247+
}
248+
public function offsetGet($offset)
249+
{
250+
return $this->get($offset);
251+
}
252+
public function offsetUnset($offset)
253+
{
254+
$this->delete($offset);
255+
}
256+
}

0 commit comments

Comments
 (0)