PHP code example of wp-php-toolkit / merge

1. Go to this page and download the library: Download wp-php-toolkit/merge library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wp-php-toolkit / merge example snippets



WordPress\Merge\Diff\Diff;
use WordPress\Merge\Diff\LineDiffer;

$diff = ( new LineDiffer() )->diff(
	"alpha\nbeta\ngamma\n",
	"alpha\nBETA\ngamma\ndelta\n"
);

$labels = array( Diff::DIFF_EQUAL => '=', Diff::DIFF_DELETE => '-', Diff::DIFF_INSERT => '+' );
foreach ( $diff->get_changes() as $change ) {
	echo $labels[ $change[0] ] . ' ' . rtrim( $change[1] ) . "\n";
}


WordPress\Merge\Diff\LineDiffer;

$old = "title: Hello\nauthor: Alice\nstatus: draft\n";
$new = "title: Hello, world\nauthor: Alice\nstatus: published\ntags: greeting\n";

$diff = ( new LineDiffer() )->diff( $old, $new );
echo $diff->format_as_git_patch( array(
	'a_source' => 'a/post.yml',
	'b_source' => 'b/post.yml',
) );


WordPress\Merge\Diff\LineDiffer;
use WordPress\Merge\Merge\LineMerger;
use WordPress\Merge\MergeStrategy;

$strategy = new MergeStrategy( new LineDiffer(), new LineMerger() );

$result = $strategy->merge(
	"intro\nbody\noutro\n",
	"intro updated\nbody\noutro\n",
	"intro\nbody\noutro\nappendix\n"
);

echo $result->has_conflicts() ? "conflicts!\n" : "clean merge:\n";
echo $result->get_merged_content();


WordPress\Merge\Diff\LineDiffer;
use WordPress\Merge\Merge\LineMerger;
use WordPress\Merge\MergeStrategy;

$strategy = new MergeStrategy( new LineDiffer(), new LineMerger() );
$result = $strategy->merge(
	"line 1\nline 2\n",
	"line 1\nline 2 from Alice\n",
	"line 1\nline 2 from Bob\n"
);

if ( $result->has_conflicts() ) {
	foreach ( $result->get_conflicts() as $c ) {
		echo "ours:   " . trim( $c->ours ) . "\n";
		echo "theirs: " . trim( $c->theirs ) . "\n";
	}
}
echo "\n--- merged content with markers ---\n";
echo $result->get_merged_content();


WordPress\Merge\Diff\LineDiffer;
use WordPress\Merge\Merge\LineMerger;
use WordPress\Merge\MergeStrategy;

$strategy = new MergeStrategy( new LineDiffer(), new LineMerger() );

$posts = array(
	'hello.md' => array(
		'base' => "# Hello\nDraft body.\n",
		'disk' => "# Hello\nDraft body, expanded on disk.\n",
		'db'   => "# Hello\nDraft body.\nNew section from the editor.\n",
	),
	'about.md' => array(
		'base' => "# About\nWho we are.\n",
		'disk' => "# About\nWho *they* are.\n",
		'db'   => "# About\nWho we really are.\n",
	),
);

foreach ( $posts as $name => $sides ) {
	$result = $strategy->merge( $sides['base'], $sides['disk'], $sides['db'] );
	echo "=== {$name} ===\n";
	echo $result->has_conflicts() ? "(conflict — needs review)\n" : "(auto-merged)\n";
	echo $result->get_merged_content() . "\n";
}