pitpit / php-diff
Compare variables (array, object, string...) easily
Installs: 14 745
Dependents: 2
Suggesters: 0
Security: 0
Stars: 5
Watchers: 0
Forks: 1
Open Issues: 2
Requires
- php: >=5.3.3
This package is auto-updated.
Last update: 2023-03-16 09:23:11 UTC
README
A comprehensive library for get differences between two variables (array, object, string...)
Installation
php composer.phar require "pitpit/diff":"@dev"
Usage
Comparing string
$engine = new \Pitpit\Component\Diff\DiffEngine(); $diff = $engine->compare('test1', 'test2'); echo $diff->isModified();
Comparing objects
class MyClassToCompare { public $value; public function __construct($value) { $this->value = $value; } public function getValue() { return $this->value; } } $toCompare1 = new MyClassToCompare(4); $toCompare2 = new MyClassToCompare(9); $engine = new \Pitpit\Component\Diff\DiffEngine(); $diff = $engine->compare($toCompare1, $toCompare2); //this closure iterate on each child properties and display where differences are $trace = function($diff, $tab = '') use (&$trace) { foreach ($diff as $element) { $c = $element->isTypeChanged()?'T':($element->isModified()?'M':($element->isCreated()?'+':($element->isDeleted()?'-':'='))); // print_r(sprintf("%s* %s [%s -> %s] (%s)\n", $tab, $element->getIdentifier(), is_object($element->getOld())?get_class($element->getOld()):gettype($element->getOld()), is_object($element->getNew())?get_class($element->getNew()):gettype($element->getNew()), $c)); print_r(sprintf("%s* %s [%s -> %s] (%s)\n", $tab, $element->getIdentifier(), gettype($element->getOld()), gettype($element->getNew()), $c)); if ($diff->isModified()) { $trace($element, $tab . ' '); } } };
Run the tests
Get and install composer: https://getcomposer.org/doc/00-intro.md#installation-nix
The resolve and download dependencies:
php composer.phar install
Run the tests:
phpunit