PluginProbe ʕ •ᴥ•ʔ
JetBackup – Backup, Restore & Migrate / trunk
JetBackup – Backup, Restore & Migrate vtrunk
3.1.22.3 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.8.1 1.4.9 1.5.0 1.5.1 1.5.1.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.10 1.6.11 1.6.12 1.6.13 1.6.15 1.6.5.1 1.6.8.8 1.6.9 1.6.9.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7.5 2.0.8.7 2.0.9.11 2.0.9.14 2.0.9.15 2.0.9.6 2.0.9.7 2.0.9.9 3.1.10.7 3.1.11.1 3.1.12.3 3.1.13.4 3.1.14.17 3.1.15.4 3.1.16.1 3.1.17.5 3.1.18.10 3.1.18.8 3.1.18.9 3.1.19.8 3.1.20.3 3.1.21.3 3.1.7.9 3.1.9.2 trunk 1.1.90 1.1.91 1.2.0 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2
backup / src / JetBackup / 3rdparty / SleekDB / Classes / NestedHelper.php
backup / src / JetBackup / 3rdparty / SleekDB / Classes Last commit date
.htaccess 1 year ago CacheHandler.php 1 year ago ConditionsHandler.php 1 year ago DocumentFinder.php 1 year ago DocumentReducer.php 1 year ago DocumentUpdater.php 4 months ago IoHelper.php 4 months ago NestedHelper.php 1 year ago index.html 1 year ago web.config 1 year ago
NestedHelper.php
140 lines
1 <?php
2
3
4 namespace SleekDB\Classes;
5
6
7 use SleekDB\Exceptions\InvalidArgumentException;
8
9 /**
10 * Class NestedHelper
11 * Helper to handle arrays.
12 */
13 class NestedHelper
14 {
15
16 /**
17 * Get nested properties of a store object.
18 * @param string $fieldName
19 * @param array $data
20 * @return mixed
21 * @throws InvalidArgumentException
22 */
23 public static function getNestedValue(string $fieldName, array $data)
24 {
25 $fieldName = trim($fieldName);
26 if (empty($fieldName)) {
27 throw new InvalidArgumentException('fieldName is not allowed to be empty');
28 }
29 // Dive deep step by step.
30 foreach (explode('.', $fieldName) as $i) {
31 // If the field does not exists we return null;
32 if (!isset($data[$i])) {
33 return null;
34 }
35 // The index is valid, collect the data.
36 $data = $data[$i];
37 }
38 return $data;
39 }
40
41 /**
42 * Check if a nested Property exists
43 * @param string $fieldName
44 * @param array $data
45 * @return mixed
46 * @throws InvalidArgumentException
47 */
48 public static function nestedFieldExists(string $fieldName, array $data)
49 {
50 $fieldName = trim($fieldName);
51 if (empty($fieldName)) {
52 throw new InvalidArgumentException('fieldName is not allowed to be empty');
53 }
54
55 // Dive deep step by step.
56 foreach (explode('.', $fieldName) as $i) {
57 // check if field exists
58 if (!is_array($data) || !array_key_exists($i, $data)) {
59 return false;
60 }
61 // The index is valid, dive deeper.
62 $data = $data[$i];
63 }
64 return true;
65 }
66
67 public static function updateNestedValue(string $fieldName, array &$data, $newValue){
68 $fieldNameArray = explode(".", $fieldName);
69 $value = $newValue;
70 if(count($fieldNameArray) > 1){
71 $data = self::_updateNestedValueHelper($fieldNameArray, $data, $newValue, count($fieldNameArray));
72 return;
73 }
74 $data[$fieldNameArray[0]] = $value;
75 }
76
77 public static function createNestedArray(string $fieldName, $fieldValue): array
78 {
79 $temp = [];
80 $fieldNameArray = explode('.', $fieldName);
81 $fieldNameArrayReverse = array_reverse($fieldNameArray);
82 foreach ($fieldNameArrayReverse as $index => $i) {
83 if($index === 0){
84 $temp = array($i => $fieldValue);
85 } else {
86 $temp = array($i => $temp);
87 }
88 }
89
90 return $temp;
91 }
92
93 public static function removeNestedField(array &$document, string $fieldToRemove){
94 if (array_key_exists($fieldToRemove, $document)) {
95 unset($document[$fieldToRemove]);
96 return;
97 }
98 // should be a nested array at this point
99 $temp = &$document;
100 $fieldNameArray = explode('.', $fieldToRemove);
101 $fieldNameArrayCount = count($fieldNameArray);
102 foreach ($fieldNameArray as $index => $i) {
103 // last iteration
104 if(($fieldNameArrayCount - 1) === $index){
105 if(is_array($temp) && array_key_exists($i, $temp)) {
106 unset($temp[$i]);
107 }
108 break;
109 }
110 if(!is_array($temp) || !array_key_exists($i, $temp)){
111 break;
112 }
113 $temp = &$temp[$i];
114 }
115 }
116
117 /**
118 * @param array $keysArray
119 * @param $data
120 * @param $newValue
121 * @param int $originalKeySize
122 * @return mixed
123 */
124 private static function _updateNestedValueHelper(array $keysArray, $data, $newValue, int $originalKeySize)
125 {
126 if(empty($keysArray)){
127 return $newValue;
128 }
129 $currentKey = $keysArray[0];
130 $result = (is_array($data)) ? $data : [];
131 if(!is_array($data) || !array_key_exists($currentKey, $data)){
132 $result[$currentKey] = self::_updateNestedValueHelper(array_slice($keysArray, 1), $data, $newValue, $originalKeySize);
133 if(count($keysArray) !== $originalKeySize){
134 return $result;
135 }
136 }
137 $result[$currentKey] = self::_updateNestedValueHelper(array_slice($keysArray, 1), $data[$currentKey], $newValue, $originalKeySize);
138 return $result;
139 }
140 }