.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 | } |