|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Konsulting; |
| 4 | + |
| 5 | +use Konsulting\Exceptions\JsonDecodeFailed; |
| 6 | + |
| 7 | +class JsonDiff |
| 8 | +{ |
| 9 | + protected $exclude = []; |
| 10 | + protected $original; |
| 11 | + protected $new; |
| 12 | + |
| 13 | + /** |
| 14 | + * JsonDiff constructor. |
| 15 | + * |
| 16 | + * @param string $original |
| 17 | + */ |
| 18 | + public function __construct($original = '') |
| 19 | + { |
| 20 | + $this->original = $original; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @param string|array $value |
| 25 | + * |
| 26 | + * @return $this |
| 27 | + */ |
| 28 | + public function exclude($value) |
| 29 | + { |
| 30 | + $value = ! $value ? [] : $value; |
| 31 | + |
| 32 | + $this->exclude = is_array($value) ? $value : [$value]; |
| 33 | + |
| 34 | + return $this; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @param string $original |
| 39 | + * @param string $new |
| 40 | + * |
| 41 | + * @return array |
| 42 | + * @throws \Exception |
| 43 | + */ |
| 44 | + public function compare($original, $new) |
| 45 | + { |
| 46 | + $this->original = $original; |
| 47 | + |
| 48 | + return $this->compareTo($new); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param string $new |
| 53 | + * |
| 54 | + * @return array |
| 55 | + * @throws \Konsulting\Exceptions\JsonDecodeFailed |
| 56 | + */ |
| 57 | + public function compareTo($new) |
| 58 | + { |
| 59 | + $original = $this->decode($this->original); |
| 60 | + $new = $this->decode($new); |
| 61 | + |
| 62 | + $flatOriginal = $this->flatten($original); |
| 63 | + $flatNew = $this->flatten($new); |
| 64 | + |
| 65 | + // array_diff_assoc only returns the values in array1 that are not in array2 |
| 66 | + // so if we don't find any, we're going to check the other way around too |
| 67 | + // otherwise we may miss some important changes, that sucks big-time. |
| 68 | + $added = array_diff_assoc($flatNew, $flatOriginal); |
| 69 | + $removed = array_diff_assoc($flatOriginal, $flatNew); |
| 70 | + |
| 71 | + $diff = [ |
| 72 | + 'added' => $this->inflate($added), |
| 73 | + 'removed' => $this->inflate($removed), |
| 74 | + ]; |
| 75 | + |
| 76 | + $changed = ! empty($added) || ! empty($removed); |
| 77 | + |
| 78 | + return compact('original', 'new', 'diff', 'changed'); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @param $json |
| 83 | + * |
| 84 | + * @return array |
| 85 | + * @throws \Konsulting\Exceptions\JsonDecodeFailed |
| 86 | + */ |
| 87 | + public function decode($json) |
| 88 | + { |
| 89 | + $array = json_decode($json, true); |
| 90 | + |
| 91 | + if (json_last_error() !== 0) { |
| 92 | + throw new JsonDecodeFailed('Failed decoding json'); |
| 93 | + } |
| 94 | + |
| 95 | + return $this->stripColumns($this->exclude, $array); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @param array $columns |
| 100 | + * @param array $array |
| 101 | + * |
| 102 | + * @return array |
| 103 | + */ |
| 104 | + public function stripColumns($columns = [], $array = []) |
| 105 | + { |
| 106 | + if (! is_array($array)) { |
| 107 | + return $array; |
| 108 | + } |
| 109 | + |
| 110 | + $array = array_diff_key($array, array_fill_keys($columns, null)); |
| 111 | + |
| 112 | + foreach($array as $k => $v) { |
| 113 | + if(is_array($v)) { |
| 114 | + $array[$k] = $this->stripColumns($columns, $v); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return $array; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param $arr |
| 123 | + * @param string $base |
| 124 | + * @param string $divider_char |
| 125 | + * |
| 126 | + * @return array |
| 127 | + */ |
| 128 | + public function flatten($arr, $base = "", $divider_char = "||") |
| 129 | + { |
| 130 | + $ret = []; |
| 131 | + if (is_array($arr)) { |
| 132 | + foreach ($arr as $k => $v) { |
| 133 | + if (is_array($v)) { |
| 134 | + $tmp_array = $this->flatten($v, $base.$k.$divider_char, $divider_char); |
| 135 | + $ret = array_merge($ret, $tmp_array); |
| 136 | + } else { |
| 137 | + $ret[$base.$k] = $v; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + return $ret; |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @param $arr |
| 146 | + * @param string $divider_char |
| 147 | + * |
| 148 | + * @return array|bool |
| 149 | + */ |
| 150 | + public function inflate($arr, $divider_char = "||") |
| 151 | + { |
| 152 | + if (!is_array($arr)) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + $split = '/' . preg_quote($divider_char, '/') . '/'; |
| 157 | + |
| 158 | + $ret = []; |
| 159 | + foreach ($arr as $key => $val) { |
| 160 | + $parts = preg_split($split, $key, -1, PREG_SPLIT_NO_EMPTY); |
| 161 | + $leafpart = array_pop($parts); |
| 162 | + $parent = &$ret; |
| 163 | + foreach ($parts as $part) { |
| 164 | + if (!isset($parent[$part])) { |
| 165 | + $parent[$part] = []; |
| 166 | + } elseif (!is_array($parent[$part])) { |
| 167 | + $parent[$part] = []; |
| 168 | + } |
| 169 | + $parent = &$parent[$part]; |
| 170 | + } |
| 171 | + |
| 172 | + if (empty($parent[$leafpart])) { |
| 173 | + $parent[$leafpart] = $val; |
| 174 | + } |
| 175 | + } |
| 176 | + return $ret; |
| 177 | + } |
| 178 | +} |
0 commit comments