File tree Expand file tree Collapse file tree 6 files changed +137
-4
lines changed
Expand file tree Collapse file tree 6 files changed +137
-4
lines changed Original file line number Diff line number Diff line change 11composer.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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 ;
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments