2017 © Pedro Peláez
 

library diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

image

diff/diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  • PHP
  • 12 Dependents
  • 0 Suggesters
  • 9 Forks
  • 5 Open issues
  • 27 Versions
  • 7 % Grown

The README.md

Diff

Build Status Code Coverage Scrutinizer Quality Score Packagist Version Packagist Downloads, (*1)

Diff is a small standalone PHP library for representing differences between data structures, computing such differences, and applying them as patches. It is extremely well tested and allows users to define their own comparison strategies., (*2)

Diff does not provide any support for computing or representing the differences between unstructured data, ie text., (*3)

A full history of the different versions of Diff can be found in the release notes., (*4)

Requirements

Diff 3.x:, (*5)

  • PHP 7.2 or later (tested with PHP 7.4 up to PHP 8.4)

Diff 2.x:, (*6)

  • PHP 5.3 or later (tested with PHP 5.3 up to PHP 7.1 and HHVM)

Installation

To add this package as a local, per-project dependency to your project, simply add a dependency on diff/diff to your project's composer.json file. Here is a minimal example of a composer.json file that just defines a dependency on Diff 3.x:, (*7)

{
    "require": {
        "diff/diff": "~3.0"
    }
}

High level structure

The Diff library can be subdivided into several components. The main components are:, (*8)

  • DiffOp Value objects that represent add, change, remove and composite operations.
  • Differ Service objects to create a diff between two sets of data.
  • Patcher Service objects to apply a diff as patch to a set of data.

There are two support components, which are nevertheless package public:, (*9)

  • Comparer Service objects for determining if two values are equal.
  • ArrayComparer Service objects for computing the difference between to arrays.

Usage

Representing diffs

A diff consists out of diff operations. These can be atomic operations such as add, change and remove. These can also be diffs themselves, when dealing with nested structures. Hence the composite pattern is used., (*10)

Diff operations implement the DiffOp interface., (*11)

The available operations are:, (*12)

  • DiffOpAdd - addition of a value (newValue)
  • DiffOpChange - modification of a value (oldValue, newValue)
  • DiffOpRemove - removal of a value (oldValue)
  • Diff - a collection of diff operations

These can all be found in src/DiffOp., (*13)

The Diff class can be set to be either associative or non-associative. In case of the later, only DiffOpAdd and DiffOpRemove are allowed in it., (*14)

Diffing data

To compute the difference between two data structures, an instance of Differ is used. The Differ interface has a single method., (*15)

/**
 * Takes two arrays, computes the diff, and returns this diff as an array of DiffOp.
 *
 * @param array $oldValues The first array
 * @param array $newValues The second array
 *
 * @throws Exception
 * @return DiffOp[]
 */
public function doDiff( array $oldValues, array $newValues ): array;

Implementations provided by Diff:, (*16)

  • ListDiffer: Differ that only looks at the values of the arrays (and thus ignores key differences).
  • MapDiffer: Differ that does an associative diff between two arrays, with the option to do this recursively.
  • CallbackListDiffer: Differ that only looks at the values of the arrays and compares them with a callback.
  • OrderedListDiffer: Differ that looks at the order of the values and the values of the arrays.

All differ functionality can be found in src/Differ., (*17)

Applying patches

To apply a diff as a patch onto a data structure, an instance of Patcher is used. The Patcher interface has a single method., (*18)

/**
 * Applies the applicable operations from the provided diff to
 * the provided base value.
 *
 * @param array $base
 * @param Diff $diffOps
 *
 * @return array
 */
public function patch( array $base, Diff $diffOps ): array;

Implementations provided by Diff:, (*19)

  • ListPatcher: Applies non-associative diffs to a base. With default options does the reverse of ListDiffer
  • MapPatcher: Applies diff to a base, recursively if needed. With default options does the reverse of MapDiffer

All classes part of the patcher component can be found in src/Patcher, (*20)

ValueComparer

The ValueComparer interface contains one method:, (*21)

/**
 * @param mixed $firstValue
 * @param mixed $secondValue
 *
 * @return bool
 */
public function valuesAreEqual( $firstValue, $secondValue ): bool;

Implementations provided by Diff:, (*22)

  • StrictComparer: Value comparer that uses PHPs native strict equality check (ie ===).
  • CallbackComparer: Adapter around a comparison callback that implements the ValueComparer interface.
  • ComparableComparer: Value comparer for objects that provide an equals method taking a single argument.

All classes part of the ValueComparer component can be found in src/Comparer, (*23)

ArrayComparer

The ArrayComposer interface contains one method:, (*24)

/**
 * Returns an array containing all the entries from arrayOne that are not present
 * in arrayTwo.
 *
 * Implementations are allowed to hold quantity into account or to disregard it.
 *
 * @param array $firstArray
 * @param array $secondArray
 *
 * @return array
 */
public function diffArrays( array $firstArray, array $secondArray ): array;

Implementations provided by Diff:, (*25)

  • NativeArrayComparer: Adapter for PHPs native array_diff method.
  • StrategicArrayComparer: Computes the difference between two arrays by comparing elements with a ValueComparer.
  • StrictArrayComparer: Does strict comparison of values and holds quantity into account.
  • OrderedArrayComparer: Computes the difference between two ordered arrays by comparing elements with a ValueComparer.

All classes part of the ArrayComparer component can be found in src/ArrayComparer, (*26)

Examples

Manually constructing a diff

$diff = new Diff( array(
    'email' => new DiffOpAdd( 'nyan@c.at' ),
    'awesome' => new DiffOpChange( 42, 9001 ),
) );

Computing a diff

$oldVersion = array(
    'awesome' => 42,
);

$newVersion = array(
    'email' => 'nyan@c.at',
    'awesome' => 9001,
);

$differ = new MapDiffer();
$diff = $differ->doDiff( $oldVersion, $newVersion );

Applying a diff as patch

$oldVersion = array(
    /* ... */
);

$diff = new Diff( /* ... */ );

$patcher = new MapPatcher();
$newVersion = $patcher->patch( $oldVersion, $diff );

The Versions

05/07 2018

dev-master

9999999-dev https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0+ GPL-2.0-or-later

The Requires

  • php >=7.0

 

The Development Requires

diff wikidata diffing patch patching diffop

05/07 2018

dev-upcv

dev-upcv https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0-or-later

The Requires

  • php >=7.0

 

The Development Requires

diff wikidata diffing patch patching diffop

05/07 2018

dev-ba

dev-ba https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0-or-later

The Requires

  • php >=7.0

 

The Development Requires

diff wikidata diffing patch patching diffop

24/04 2018

dev-MapDifferInterface

dev-MapDifferInterface https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0-or-later

The Requires

  • php >=7.0

 

The Development Requires

diff wikidata diffing patch patching diffop

24/04 2018

dev-phpunit7

dev-phpunit7 https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0-or-later

The Requires

  • php >=7.0

 

The Development Requires

diff wikidata diffing patch patching diffop

17/04 2018

2.x-dev

2.9999999.9999999.9999999-dev https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

The Development Requires

diff wikidata diffing patching diffop

17/04 2018

3.1.0

3.1.0.0 https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0-or-later

The Requires

  • php >=7.0

 

The Development Requires

diff wikidata diffing patch patching diffop

11/04 2018

2.3.0

2.3.0.0 https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

The Development Requires

diff wikidata diffing patching diffop

29/11 2017

dev-diffIsEmpty

dev-diffIsEmpty https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0+

The Requires

  • php >=7.0

 

The Development Requires

diff wikidata diffing patch patching diffop

09/08 2017

2.2.0

2.2.0.0 https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

The Development Requires

diff wikidata diffing patching diffop

09/06 2017

dev-upperbound

dev-upperbound https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0+

The Requires

  • php >=7.0

 

The Development Requires

diff wikidata diffing patch patching diffop

10/05 2017

3.0.0

3.0.0.0 https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0+

The Requires

  • php >=7.0

 

The Development Requires

diff wikidata diffing patch patching diffop

07/03 2017

dev-php70

dev-php70 https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0+

The Requires

  • php >=7.0

 

The Development Requires

diff wikidata diffing patch patching diffop

01/09 2016

2.1.0

2.1.0.0 https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

The Development Requires

diff wikidata diffing patching diffop

24/08 2016

dev-listChange

dev-listChange https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

The Development Requires

diff wikidata diffing patching diffop

10/02 2016

dev-listDiffer

dev-listDiffer https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

The Development Requires

diff wikidata diffing patching diffop

27/04 2015

dev-longvar

dev-longvar https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

The Development Requires

diff wikidata diffing patching diffop

17/03 2015

2.0.0

2.0.0.0 https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

The Development Requires

diff wikidata diffing patching diffop

23/02 2015

dev-composerMd

dev-composerMd https://github.com/wmde/Diff

Small standalone library for representing differences between data structures, computing such differences, and applying them as patches

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

The Development Requires

diff wikidata diffing patching diffop

07/05 2014

1.0.1

1.0.1.0 https://github.com/wmde/Diff

Library for diffing, patching and representing differences between complex objects

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

diff wikidata diffing patching diffop

10/04 2014

1.0

1.0.0.0 https://github.com/wmde/Diff

Library for diffing, patching and representing differences between complex objects

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

diff wikidata diffing patching diffop

04/10 2013

0.9

0.9.0.0 https://github.com/wikimedia/mediawiki-extensions-Diff

Library for diffing, patching and representing differences between complex objects

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

The Development Requires

diff wikidata

26/08 2013

0.8

0.8.0.0 https://github.com/wikimedia/mediawiki-extensions-Diff

Library for diffing, patching and representing differences between complex objects

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

The Development Requires

diff wikidata

16/07 2013

0.7

0.7.0.0 https://www.mediawiki.org/wiki/Extension:Diff

Library for computing and representing diffs between JSON like data structures

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

diff wikidata

08/05 2013

0.6

0.6.0.0 https://www.mediawiki.org/wiki/Extension:Diff

Library for computing and representing diffs between JSON like data structures

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

The Development Requires

diff

26/02 2013

0.5

0.5.0.0 https://www.mediawiki.org/wiki/Extension:Diff

Library for computing and representing diffs between JSON like data structures

  Sources   Download

GPL-2.0+

The Requires

  • php >=5.3.0

 

The Development Requires

diff

08/01 2013

0.4

0.4.0.0 https://www.mediawiki.org/wiki/Extension:Diff

Library for computing and representing diffs between JSON like data structures

  Sources   Download

GNU GPL v2+

The Requires

  • php >=5.3.0

 

diff