-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1231 from sillsdev/staging
Release 1.10.0
- Loading branch information
Showing
33 changed files
with
1,004 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Api\Model\Shared\DeepDiff; | ||
|
||
use Litipk\Jiffy\UniversalTimestamp; | ||
use Palaso\Utilities\CodeGuard; | ||
|
||
class AddedDiff extends DiffBase | ||
{ | ||
/** @var mixed */ | ||
public $newData; | ||
|
||
public function __construct(Array $diff) { | ||
$this->kind = 'N'; | ||
$this->path = $diff['path']; | ||
$this->newData = $diff['rhs']; | ||
} | ||
|
||
public function getValue() { | ||
return $this->newData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Api\Model\Shared\DeepDiff; | ||
|
||
use Litipk\Jiffy\UniversalTimestamp; | ||
use Palaso\Utilities\CodeGuard; | ||
|
||
class ArrayDiff extends DiffBase | ||
{ | ||
/** @var integer */ | ||
public $idx; | ||
|
||
/** @var Array */ | ||
public $item; | ||
|
||
public function __construct(Array $diff) { | ||
$this->kind = 'A'; | ||
$this->path = $diff['path']; | ||
$this->idx = $diff['index']; | ||
$this->item = $diff['item']; | ||
} | ||
|
||
public function getValue() { | ||
return $this->item['rhs']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
namespace Api\Model\Shared\DeepDiff; | ||
|
||
use Litipk\Jiffy\UniversalTimestamp; | ||
use Palaso\Utilities\CodeGuard; | ||
|
||
class DeepDiffDecoder | ||
{ | ||
public static function prepareDeepDiff($diffs) { | ||
$result = []; | ||
foreach (static::reorderPushes($diffs) as $diff) { | ||
$diffInstance = DiffBase::fromDeepDiff($diff); | ||
if ($diffInstance) $result[] = $diffInstance; | ||
} | ||
return $result; | ||
} | ||
|
||
public static function reorderPushes($diffs) { | ||
$currentPath = []; | ||
$pushes = []; | ||
$result = []; | ||
foreach ($diffs as $diff) { | ||
if ($diff['kind'] == 'A' && $diff['item']['kind'] == 'N' && | ||
($currentPath == [] || $currentPath == $diff['path'])) { | ||
$currentPath = $diff['path']; | ||
$pushes[] = $diff; | ||
} else { | ||
if ($pushes) { | ||
foreach (array_reverse($pushes) as $push) { | ||
$result[] = $push; | ||
} | ||
$pushes = []; | ||
$currentPath = []; | ||
} | ||
$result[] = $diff; | ||
} | ||
} | ||
if ($pushes) { | ||
foreach (array_reverse($pushes) as $push) { | ||
$result[] = $push; | ||
} | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* Sets the public properties of $model to values from $values[propertyName] | ||
* @param object|MapOf|ArrayOf $model | ||
* @param array $deepDiff An array of diffs from the Javascript deep-diff library. | ||
* @throws \Exception | ||
*/ | ||
public static function applyDeepDiff($model, $deepDiff) | ||
{ | ||
CodeGuard::checkTypeAndThrow($deepDiff, 'array'); | ||
// TODO: Do we need something like this next line from JsonDecoder, or something similar to it? | ||
// $propertiesToIgnore = $this->getPrivateAndReadOnlyProperties($model); | ||
$diffs = static::prepareDeepDiff($deepDiff); | ||
foreach ($diffs as $diff) { | ||
static::applySingleDiff($model, $diff); | ||
} | ||
} | ||
|
||
/** | ||
* Sets the public properties of $model to values from $values[propertyName] | ||
* @param object|MapOf|ArrayOf $model | ||
* @param DiffBase $diff A single diff to apply to the model | ||
* @throws \Exception | ||
*/ | ||
public static function applySingleDiff($model, $diff) | ||
{ | ||
$path = $diff->path; | ||
$allButLast = $path; // This is a copy | ||
$last = array_pop($allButLast); | ||
$isLexValue = false; | ||
if ($last === 'value') { | ||
$isLexValue = true; | ||
$last = array_pop($allButLast); | ||
} | ||
$target = $model; | ||
foreach ($allButLast as $step) { | ||
$target = static::getNextStep($target, $step); | ||
} | ||
if ($diff instanceof ArrayDiff) { | ||
if ($diff->item['kind'] == 'N') { | ||
static::pushValue($target, $last, $diff->getValue()); | ||
} elseif ($diff->item['kind'] == 'D') { | ||
if ($target instanceof \ArrayObject) { | ||
array_pop($target[$last]); | ||
} else { | ||
array_pop($target->$last); | ||
} | ||
} else { | ||
// Invalid ArrayDiff; do nothing | ||
} | ||
} else { | ||
static::setValue($target, $last, $diff->getValue()); | ||
// TODO: Verify that this works as desired for deletions | ||
} | ||
} | ||
|
||
private static function getNextStep($target, $step) { | ||
if ($target instanceof \Api\Model\Shared\Mapper\MapOf && strpos($step, 'customField_') === 0) { | ||
// Custom fields should be created if they do not exist | ||
if (isset($target[$step])) { | ||
return $target[$step]; | ||
} else { | ||
if ($target->hasGenerator()) { | ||
return $target->generate([$step]); | ||
} else { | ||
// This will probably fail | ||
return $target[$step]; | ||
} | ||
} | ||
} else if ($target instanceof \ArrayObject) { | ||
return $target[$step]; | ||
} else { | ||
return $target->$step; | ||
} | ||
} | ||
|
||
private static function setValue(&$target, $last, $value) { | ||
if ($target instanceof \Api\Model\Languageforge\Lexicon\LexMultiText) { | ||
$target->form($last, $value); | ||
} else if ($target instanceof \ArrayObject) { | ||
$target[$last] = $value; | ||
} else { | ||
$target->$last = $value; | ||
} | ||
} | ||
|
||
private static function pushValue(&$target, $last, $value) { | ||
if ($target instanceof \ArrayObject) { | ||
$target[$last][] = $value; | ||
} else { | ||
$target->$last[] = $value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Api\Model\Shared\DeepDiff; | ||
|
||
use Litipk\Jiffy\UniversalTimestamp; | ||
use Palaso\Utilities\CodeGuard; | ||
|
||
class DeletedDiff extends DiffBase | ||
{ | ||
/** @var mixed */ | ||
public $oldData; | ||
|
||
public function __construct(Array $diff) { | ||
$this->kind = 'D'; | ||
$this->path = $diff['path']; | ||
$this->oldData = $diff['lhs']; | ||
} | ||
|
||
public function getValue() { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Api\Model\Shared\DeepDiff; | ||
|
||
use Litipk\Jiffy\UniversalTimestamp; | ||
use Palaso\Utilities\CodeGuard; | ||
|
||
class DiffBase | ||
{ | ||
/** @var callable The function <object> function($data = null) returns an instance of the object. */ | ||
private $_generator; | ||
|
||
/** @var char */ | ||
public $kind; | ||
|
||
/** @var array */ | ||
public $path; | ||
|
||
public function toMongoPath() { | ||
return join('.', $this->path); | ||
} | ||
|
||
public function getValue() {} | ||
|
||
public static function fromDeepDiff($deepDiff) { | ||
$kind = $deepDiff['kind']; | ||
switch ($kind) { | ||
case 'N': return new AddedDiff($deepDiff); | ||
case 'D': return new DeletedDiff($deepDiff); | ||
case 'E': return new EditedDiff($deepDiff); | ||
case 'A': return new ArrayDiff($deepDiff); | ||
default: return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Api\Model\Shared\DeepDiff; | ||
|
||
use Litipk\Jiffy\UniversalTimestamp; | ||
use Palaso\Utilities\CodeGuard; | ||
|
||
class EditedDiff extends DiffBase | ||
{ | ||
/** @var mixed */ | ||
public $oldData; | ||
|
||
/** @var mixed */ | ||
public $newData; | ||
|
||
public function __construct(Array $diff) { | ||
$this->kind = 'E'; | ||
$this->path = $diff['path']; | ||
$this->oldData = $diff['lhs']; | ||
$this->newData = $diff['rhs']; | ||
} | ||
|
||
public function getValue() { | ||
return $this->newData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.