diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index d0ff9c4..6697207 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -6,6 +6,8 @@ Latest release: ## Version 3.2 (dev) +* Added ability to construct a recursive `MapDiffer` that uses itself to diff elements (via `MapDiffer::newRecursiveDiffer`) +* You can now pass non-list-differs as the second constructor argument in `MapDiffer` * Deprecated constant `Diff_VERSION` ## Version 3.1 (2018-04-17) diff --git a/src/Differ/Differ.php b/src/Differ/Differ.php index de57a85..1624e60 100644 --- a/src/Differ/Differ.php +++ b/src/Differ/Differ.php @@ -10,6 +10,8 @@ /** * Interface for objects that can diff two arrays to an array of DiffOp. * + * If the diff is done associatively, the differ should implement MapDifferInterface. + * * @since 0.4 * * @license GPL-2.0+ diff --git a/src/Differ/MapDiffer.php b/src/Differ/MapDiffer.php index c3a0b94..56e0cfe 100644 --- a/src/Differ/MapDiffer.php +++ b/src/Differ/MapDiffer.php @@ -23,7 +23,7 @@ * @license GPL-2.0+ * @author Jeroen De Dauw < jeroendedauw@gmail.com > */ -class MapDiffer implements Differ { +class MapDiffer implements MapDifferInterface { /** * @var bool @@ -33,7 +33,7 @@ class MapDiffer implements Differ { /** * @var Differ */ - private $listDiffer; + private $elementDiffer; /** * @var ValueComparer @@ -43,12 +43,26 @@ class MapDiffer implements Differ { /** * The third argument ($comparer) was added in 3.0 */ - public function __construct( bool $recursively = false, Differ $listDiffer = null, ValueComparer $comparer = null ) { + public function __construct( bool $recursively = false, Differ $elementDiffer = null, + ValueComparer $comparer = null ) { + $this->recursively = $recursively; - $this->listDiffer = $listDiffer ?? new ListDiffer(); + $this->elementDiffer = $elementDiffer ?? new ListDiffer(); $this->valueComparer = $comparer ?? new StrictComparer(); } + /** + * Creates a recursive MapDiffer that by default uses itself to diff elements recursively + * + * @since 3.2 + */ + public static function newRecursiveDiffer( ValueComparer $comparer = null, Differ $elementDiffer = null ) { + $differ = new self( true, null, $comparer ); + $differ->elementDiffer = $elementDiffer ?? $differ; + + return $differ; + } + /** * @see Differ::doDiff * @@ -134,7 +148,10 @@ private function getDiffForArrays( array $old, array $new ): Diff { return new Diff( $this->doDiff( $old, $new ), true ); } - return new Diff( $this->listDiffer->doDiff( $old, $new ), false ); + return new Diff( + $this->elementDiffer->doDiff( $old, $new ), + $this->elementDiffer instanceof MapDifferInterface + ); } /** diff --git a/src/Differ/MapDifferInterface.php b/src/Differ/MapDifferInterface.php new file mode 100644 index 0000000..ea29ebf --- /dev/null +++ b/src/Differ/MapDifferInterface.php @@ -0,0 +1,17 @@ + + */ +interface MapDifferInterface extends Differ { + +}