|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Api\Model\Shared\DeepDiff; |
| 4 | + |
| 5 | +use Litipk\Jiffy\UniversalTimestamp; |
| 6 | +use Palaso\Utilities\CodeGuard; |
| 7 | + |
| 8 | +class DeepDiffDecoder |
| 9 | +{ |
| 10 | + public static function prepareDeepDiff($diffs) { |
| 11 | + $result = []; |
| 12 | + foreach (static::reorderPushes($diffs) as $diff) { |
| 13 | + $diffInstance = DiffBase::fromDeepDiff($diff); |
| 14 | + if ($diffInstance) $result[] = $diffInstance; |
| 15 | + } |
| 16 | + return $result; |
| 17 | + } |
| 18 | + |
| 19 | + public static function reorderPushes($diffs) { |
| 20 | + $currentPath = []; |
| 21 | + $pushes = []; |
| 22 | + $result = []; |
| 23 | + foreach ($diffs as $diff) { |
| 24 | + if ($diff['kind'] == 'A' && $diff['item']['kind'] == 'N' && |
| 25 | + ($currentPath == [] || $currentPath == $diff['path'])) { |
| 26 | + $currentPath = $diff['path']; |
| 27 | + $pushes[] = $diff; |
| 28 | + } else { |
| 29 | + if ($pushes) { |
| 30 | + foreach (array_reverse($pushes) as $push) { |
| 31 | + $result[] = $push; |
| 32 | + } |
| 33 | + $pushes = []; |
| 34 | + $currentPath = []; |
| 35 | + } |
| 36 | + $result[] = $diff; |
| 37 | + } |
| 38 | + } |
| 39 | + if ($pushes) { |
| 40 | + foreach (array_reverse($pushes) as $push) { |
| 41 | + $result[] = $push; |
| 42 | + } |
| 43 | + } |
| 44 | + return $result; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Sets the public properties of $model to values from $values[propertyName] |
| 49 | + * @param object|MapOf|ArrayOf $model |
| 50 | + * @param array $deepDiff An array of diffs from the Javascript deep-diff library. |
| 51 | + * @throws \Exception |
| 52 | + */ |
| 53 | + public static function applyDeepDiff($model, $deepDiff) |
| 54 | + { |
| 55 | + CodeGuard::checkTypeAndThrow($deepDiff, 'array'); |
| 56 | + // TODO: Do we need something like this next line from JsonDecoder, or something similar to it? |
| 57 | + // $propertiesToIgnore = $this->getPrivateAndReadOnlyProperties($model); |
| 58 | + $diffs = static::prepareDeepDiff($deepDiff); |
| 59 | + foreach ($diffs as $diff) { |
| 60 | + static::applySingleDiff($model, $diff); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Sets the public properties of $model to values from $values[propertyName] |
| 66 | + * @param object|MapOf|ArrayOf $model |
| 67 | + * @param DiffBase $diff A single diff to apply to the model |
| 68 | + * @throws \Exception |
| 69 | + */ |
| 70 | + public static function applySingleDiff($model, $diff) |
| 71 | + { |
| 72 | + $path = $diff->path; |
| 73 | + $allButLast = $path; // This is a copy |
| 74 | + $last = array_pop($allButLast); |
| 75 | + $isLexValue = false; |
| 76 | + if ($last === 'value') { |
| 77 | + $isLexValue = true; |
| 78 | + $last = array_pop($allButLast); |
| 79 | + } |
| 80 | + $target = $model; |
| 81 | + foreach ($allButLast as $step) { |
| 82 | + $target = static::getNextStep($target, $step); |
| 83 | + } |
| 84 | + if ($diff instanceof ArrayDiff) { |
| 85 | + if ($diff->item['kind'] == 'N') { |
| 86 | + static::pushValue($target, $last, $diff->getValue()); |
| 87 | + } elseif ($diff->item['kind'] == 'D') { |
| 88 | + if ($target instanceof \ArrayObject) { |
| 89 | + array_pop($target[$last]); |
| 90 | + } else { |
| 91 | + array_pop($target->$last); |
| 92 | + } |
| 93 | + } else { |
| 94 | + // Invalid ArrayDiff; do nothing |
| 95 | + } |
| 96 | + } else { |
| 97 | + static::setValue($target, $last, $diff->getValue()); |
| 98 | + // TODO: Verify that this works as desired for deletions |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private static function getNextStep($target, $step) { |
| 103 | + if ($target instanceof \Api\Model\Shared\Mapper\MapOf && strpos($step, 'customField_') === 0) { |
| 104 | + // Custom fields should be created if they do not exist |
| 105 | + if (isset($target[$step])) { |
| 106 | + return $target[$step]; |
| 107 | + } else { |
| 108 | + if ($target->hasGenerator()) { |
| 109 | + return $target->generate([$step]); |
| 110 | + } else { |
| 111 | + // This will probably fail |
| 112 | + return $target[$step]; |
| 113 | + } |
| 114 | + } |
| 115 | + } else if ($target instanceof \ArrayObject) { |
| 116 | + return $target[$step]; |
| 117 | + } else { |
| 118 | + return $target->$step; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static function setValue(&$target, $last, $value) { |
| 123 | + if ($target instanceof \Api\Model\Languageforge\Lexicon\LexMultiText) { |
| 124 | + $target->form($last, $value); |
| 125 | + } else if ($target instanceof \ArrayObject) { |
| 126 | + $target[$last] = $value; |
| 127 | + } else { |
| 128 | + $target->$last = $value; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private static function pushValue(&$target, $last, $value) { |
| 133 | + if ($target instanceof \ArrayObject) { |
| 134 | + $target[$last][] = $value; |
| 135 | + } else { |
| 136 | + $target->$last[] = $value; |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments