PHP code example of bircher / php-merge

1. Go to this page and download the library: Download bircher/php-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/ */

    

bircher / php-merge example snippets


use PhpMerge\PhpMerge;

// Create a merger instance.
$merger = new PhpMerge();

// Get the texts to merge.
$original = <<<'EOD'
unchanged
replaced
unchanged
normal
unchanged
unchanged
removed

EOD;

$version1= <<<'EOD'
added
unchanged
replacement
unchanged
normal
unchanged
unchanged

EOD;

$version2 = <<<'EOD'
unchanged
replaced
unchanged
normal??
unchanged
unchanged

EOD;

$expected = <<<'EOD'
added
unchanged
replacement
unchanged
normal??
unchanged
unchanged

EOD;

$result = $merger->merge($original, $version1, $version2);
// $result === $expected;


// Continuing from before with:
use Phpmerge\MergeException;
use PhpMerge\MergeConflict;


$conflicting = <<<'EOD'
unchanged
replaced
unchanged
normal!!
unchanged
unchanged

EOD;

try {
    $merger->merge($original, $version2, $conflicting);
} catch (MergeException $exception) {
    /** @var MergeConflict[] $conflicts */
    $conflicts = $exception->getConflicts();

    $original_lines = $conflicts[0]->getBase();
    // $original_lines === ["normal\n"];
    
    $version2_lines = $conflicts[0]->getRemote();
    // $version2_lines === ["normal??\n"];
    
    $conflicting_lines = $conflicts[0]->getLocal();
    // $conflicting_lines === ["normal!!\n"];
    
    $line_numer_of_conflict = $conflicts[0]->getBaseLine();
    // $line_numer_of_conflict === 3; // Count starts with 0.
    
    // It is also possible to get the merged version using the first version
    // to resolve conflicts.
    $merged = $exception->getMerged();
    // $merged === $version2;
    // In this case, but in general there could be non-conflicting changes.
    
    $line_in_merged = $conflicts[0]->getMergedLine();
    // $line_in_merged === 3; // Count starts with 0.
}


use PhpMerge\GitMerge;

$merger = new GitMerge();

// Use as the previous example.
json
{
    "ircher/php-merge": "~4.0"
    }
}