Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Oct 11, 2017
1 parent 05bf748 commit dbcf663
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 4 deletions.
6 changes: 2 additions & 4 deletions .gitignore
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
20 changes: 20 additions & 0 deletions composer.json
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"
}
}
11 changes: 11 additions & 0 deletions demo/differ.php
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;
50 changes: 50 additions & 0 deletions src/Diff/Differ.php
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] ?? '';
}

}
9 changes: 9 additions & 0 deletions src/Diff/Driver.php
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);
}
45 changes: 45 additions & 0 deletions src/Diff/Native.php
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);
}

}

0 comments on commit dbcf663

Please sign in to comment.