Skip to content

Commit dbcf663

Browse files
committed
first version
1 parent 05bf748 commit dbcf663

File tree

6 files changed

+137
-4
lines changed

6 files changed

+137
-4
lines changed

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
composer.phar
22
/vendor/
3-
4-
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
5-
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
6-
# composer.lock
3+
composer.lock
4+
.idea

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "bavix/diff",
3+
"description": "diff",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "REZ1DENT3",
8+
"email": "info@babichev.net"
9+
}
10+
],
11+
"autoload": {
12+
"psr-4": {
13+
"Bavix\\": "src/"
14+
}
15+
},
16+
"require": {
17+
"yetanotherape/diff-match-patch": "^1.0",
18+
"bavix/exceptions": "^1.0"
19+
}
20+
}

demo/differ.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
include_once dirname(__DIR__) . '/vendor/autoload.php';
4+
5+
//$differ = new \Bavix\Diff\Differ();
6+
$differ = new \Bavix\Diff\Native();
7+
8+
$patch = $differ->diff('foo', 'bar');
9+
10+
print $patch . PHP_EOL;
11+
print $differ->patch('foo', $patch) . PHP_EOL . PHP_EOL;

src/Diff/Differ.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Bavix\Diff;
4+
5+
use DiffMatchPatch\DiffMatchPatch;
6+
7+
class Differ implements Driver
8+
{
9+
10+
/**
11+
* @var DiffMatchPatch
12+
*/
13+
protected $dmp;
14+
15+
/**
16+
* Diff constructor.
17+
*/
18+
public function __construct()
19+
{
20+
$this->dmp = new DiffMatchPatch();
21+
}
22+
23+
/**
24+
* @param string $oldData
25+
* @param string $newData
26+
*
27+
* @return string
28+
*/
29+
public function diff($oldData, $newData)
30+
{
31+
return $this->dmp->patch_toText(
32+
$this->dmp->patch_make($oldData, $newData)
33+
);
34+
}
35+
36+
/**
37+
* @param string $data
38+
* @param string $patch
39+
*
40+
* @return string
41+
*/
42+
public function patch($data, $patch)
43+
{
44+
return $this->dmp->patch_apply(
45+
$this->dmp->patch_fromText($patch),
46+
$data
47+
)[0] ?? '';
48+
}
49+
50+
}

src/Diff/Driver.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Bavix\Diff;
4+
5+
interface Driver
6+
{
7+
public function diff($oldData, $newData);
8+
public function patch($data, $patch);
9+
}

src/Diff/Native.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Bavix\Diff;
4+
5+
use Bavix\Exceptions\Runtime;
6+
7+
class Native implements Driver
8+
{
9+
10+
/**
11+
* Native constructor.
12+
*
13+
* @throws Runtime
14+
*/
15+
public function __construct()
16+
{
17+
if (!function_exists('xdiff_string_diff'))
18+
{
19+
throw new Runtime('Extension `xdiff` not found');
20+
}
21+
}
22+
23+
/**
24+
* @param string $oldData
25+
* @param string $newData
26+
*
27+
* @return string
28+
*/
29+
public function diff($oldData, $newData)
30+
{
31+
return \xdiff_string_diff($oldData, $newData);
32+
}
33+
34+
/**
35+
* @param string $data
36+
* @param string $patch
37+
*
38+
* @return string
39+
*/
40+
public function patch($data, $patch)
41+
{
42+
return \xdiff_string_patch($data, $patch);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)