PHP code example of muhammetsafak / data-versions

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

    

muhammetsafak / data-versions example snippets


use \MuhammetSafak\DataVersions\{Version, Diff};

/** @var Version[] $versions */
$versions = [
    new Version(['name' => 'Muhammet', 'surname' => 'Şafak'], ['user' => 10]),
    new Version(['name' => 'Muhammet', 'surname' => 'Şafak'], ['user' => 12]),
    new Version(['name' => 'Muhammet', 'surname' => 'Şafak', 'age' => 30], ['user' => 5]),
    new Version(['name' => 'Ahmet', 'surname' => 'Şafak'], ['user' => 7]),
];

$compare = new Version(['name' => 'Muhammet'], ['user' => 1]);
foreach ($versions as $version) {
    echo '<h2>' . $version->getID() . ' Changer: #' . $version->getInfo()['user'] . '</h2>';
    
    $diff = $compare->diff($version);
    
    if ($diff->isDiff()) {
        echo '<ul>' . PHP_EOL;
        foreach ($differences as $difference) {
            echo '<li>' . $difference['name'] . ' : ';
            switch ($difference['type']) {
                case Diff::ADDED:
                    echo ' added "' . $difference['value'] . '"';
                    break;
                case Diff::CHANGING:
                    echo '"' . $difference['value'][0] . '" has been replaced with; "' .$difference['value'][1] . '"';
                    break;
                case Diff::REMOVED:
                    echo ' removed "' . $difference['value'] . '"';
                    break;
            }
            echo '</li>' . PHP_EOL;
        }
        echo '</ul>';
    } else {
        echo 'It does not contain any changes.';
    }
    
    
    $compare = $version;
}

namespace MuhammetSafak\DataVersions;

class Version
{
    public function __construct(array $data, array $info = [], ?string $id = null, ?int $time = null);
    public function getID(): string;
    public function toJSON(): string;
    public function getDate(string $format = 'c'): string;
    public function getData(): array;
    public function getInfo(): array;
    public function diff(\MuhammetSafak\DataVersions\Version $nextVersion): \MuhammetSafak\DataVersions\Diff;
}

class Diff
{
    public const REMOVED = 0;
    public const ADDED = 1;
    public const CHANGING = 2;
    
    public function __construct(array $diff, array $info = []);
    public function getDiff(): array;
    public function getInfo(): array;
    public function isDiff(): bool;
    public function count(): int;
}