-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
137 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
composer.phar | ||
/vendor/ | ||
|
||
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file | ||
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file | ||
# composer.lock | ||
composer.lock | ||
.idea |
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,20 @@ | ||
{ | ||
"name": "bavix/diff", | ||
"description": "diff", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "REZ1DENT3", | ||
"email": "info@babichev.net" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Bavix\\": "src/" | ||
} | ||
}, | ||
"require": { | ||
"yetanotherape/diff-match-patch": "^1.0", | ||
"bavix/exceptions": "^1.0" | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
include_once dirname(__DIR__) . '/vendor/autoload.php'; | ||
|
||
//$differ = new \Bavix\Diff\Differ(); | ||
$differ = new \Bavix\Diff\Native(); | ||
|
||
$patch = $differ->diff('foo', 'bar'); | ||
|
||
print $patch . PHP_EOL; | ||
print $differ->patch('foo', $patch) . PHP_EOL . PHP_EOL; |
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,50 @@ | ||
<?php | ||
|
||
namespace Bavix\Diff; | ||
|
||
use DiffMatchPatch\DiffMatchPatch; | ||
|
||
class Differ implements Driver | ||
{ | ||
|
||
/** | ||
* @var DiffMatchPatch | ||
*/ | ||
protected $dmp; | ||
|
||
/** | ||
* Diff constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->dmp = new DiffMatchPatch(); | ||
} | ||
|
||
/** | ||
* @param string $oldData | ||
* @param string $newData | ||
* | ||
* @return string | ||
*/ | ||
public function diff($oldData, $newData) | ||
{ | ||
return $this->dmp->patch_toText( | ||
$this->dmp->patch_make($oldData, $newData) | ||
); | ||
} | ||
|
||
/** | ||
* @param string $data | ||
* @param string $patch | ||
* | ||
* @return string | ||
*/ | ||
public function patch($data, $patch) | ||
{ | ||
return $this->dmp->patch_apply( | ||
$this->dmp->patch_fromText($patch), | ||
$data | ||
)[0] ?? ''; | ||
} | ||
|
||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace Bavix\Diff; | ||
|
||
interface Driver | ||
{ | ||
public function diff($oldData, $newData); | ||
public function patch($data, $patch); | ||
} |
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,45 @@ | ||
<?php | ||
|
||
namespace Bavix\Diff; | ||
|
||
use Bavix\Exceptions\Runtime; | ||
|
||
class Native implements Driver | ||
{ | ||
|
||
/** | ||
* Native constructor. | ||
* | ||
* @throws Runtime | ||
*/ | ||
public function __construct() | ||
{ | ||
if (!function_exists('xdiff_string_diff')) | ||
{ | ||
throw new Runtime('Extension `xdiff` not found'); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $oldData | ||
* @param string $newData | ||
* | ||
* @return string | ||
*/ | ||
public function diff($oldData, $newData) | ||
{ | ||
return \xdiff_string_diff($oldData, $newData); | ||
} | ||
|
||
/** | ||
* @param string $data | ||
* @param string $patch | ||
* | ||
* @return string | ||
*/ | ||
public function patch($data, $patch) | ||
{ | ||
return \xdiff_string_patch($data, $patch); | ||
} | ||
|
||
} |