Skip to content

Commit 6bdbe7d

Browse files
committed
Remove illuminate/support dependency and refactor
1 parent 8c16870 commit 6bdbe7d

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/vendor/
2-
composer.lock
2+
composer.lock
3+
.idea
4+
*.cache

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
"name": "divineomega/array_undot",
33
"description": "array_undot (the opposite of the array_dot helper function) expands a dot notation array into a full multi-dimensional array.",
44
"type": "library",
5-
"require": {
6-
"illuminate/support": "^5.1"
7-
},
85
"require-dev": {
96
"phpunit/phpunit": "^7.0 || ^8.0",
10-
"satooshi/php-coveralls": "^2.0"
7+
"php-coveralls/php-coveralls": "^2.1"
118
},
129
"license": "LGPL-3.0-only",
1310
"authors": [
@@ -19,6 +16,9 @@
1916
"autoload":{
2017
"files": [
2118
"src/array_undot.php"
22-
]
19+
],
20+
"psr-4": {
21+
"DivineOmega\\ArrayUndot\\": "src/"
22+
}
2323
}
2424
}

src/ArrayHelpers.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace DivineOmega\ArrayUndot;
4+
5+
6+
class ArrayHelpers
7+
{
8+
/**
9+
* Expands a dot notation array into a full multi-dimensional array
10+
*
11+
* @param array $dotNotationArray
12+
* @return array
13+
*/
14+
public function undot(array $dotNotationArray)
15+
{
16+
$array = [];
17+
foreach ($dotNotationArray as $key => $value) {
18+
$this->set($array, $key, $value);
19+
}
20+
21+
return $array;
22+
}
23+
24+
/**
25+
* Set an array item to a given value using "dot" notation.
26+
*
27+
* If no key is given to the method, the entire array will be replaced.
28+
*
29+
* @param array $array
30+
* @param string $key
31+
* @param mixed $value
32+
* @return array
33+
*/
34+
public function set(&$array, $key, $value)
35+
{
36+
if (is_null($key)) {
37+
return $array = $value;
38+
}
39+
$keys = explode('.', $key);
40+
while (count($keys) > 1) {
41+
$key = array_shift($keys);
42+
// If the key doesn't exist at this depth, we will just create an empty array
43+
// to hold the next value, allowing us to create the arrays to hold final
44+
// values at the correct depth. Then we'll keep digging into the array.
45+
if (! isset($array[$key]) || ! is_array($array[$key])) {
46+
$array[$key] = [];
47+
}
48+
$array = &$array[$key];
49+
}
50+
$array[array_shift($keys)] = $value;
51+
return $array;
52+
}
53+
}

src/array_undot.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use DivineOmega\ArrayUndot\ArrayHelpers;
4+
35
if (!function_exists('array_undot')) {
46
/**
57
* Process dot array will return undot array.
@@ -10,11 +12,6 @@
1012
*/
1113
function array_undot(array $dotNotationArray)
1214
{
13-
$array = [];
14-
foreach ($dotNotationArray as $key => $value) {
15-
array_set($array, $key, $value);
16-
}
17-
18-
return $array;
15+
return (new ArrayHelpers())->undot($dotNotationArray);
1916
}
2017
}

0 commit comments

Comments
 (0)