brunohanai/object-comparator

比较两个对象并检查它们之间是否存在差异。

0.1.1 2016-01-09 03:44 UTC

This package is not auto-updated.

Last update: 2024-09-18 19:12:42 UTC


README

Total Downloads Latest Stable Version Reference Status

注意:此组件是为我的学习而开发的,不应被视为可信。请根据您的判断使用。

不要依赖此组件。它仅用于学习。

ObjectComparator接受两个对象(相同类型)并告知其属性是否存在差异。

该组件可以概括为三个功能

  • Comparator:告知是否存在两个对象之间的差异。
  • Differ:返回包含差异的列表(DiffCollection)。
  • Logger:记录找到的差异。Logger附带两个默认实现,并具有一个接口,以便用户进行自己的实现。

安装

使用composer安装最新版本

$ composer require brunohanai/object-comparator

基本用法 - Comparator

<?php

use brunohanai\ObjectComparator\Comparator;

// create a Comparator
$comparator = new Comparator();

// create two identical objects
$object1 = new stdClass();
$object1->description = 'Description';
$object1->code = 'DESC';
// ---
$object2 = new stdClass();
$object2->description = 'Description';
$object2->code = 'DESC';

// compare
$comparator->isEquals($object1, $object2)); // outputs true
$comparator->isNotEquals($object1, $object2)); // outputs false


// change one property
$object2->description = 'New Description';

// compare again
var_dump($comparator->isEquals($object1, $object2)); // outputs false
var_dump($comparator->isNotEquals($object1, $object2)); // outputs true

基本用法 - Differ

<?php

use brunohanai\ObjectComparator\Comparator;
use brunohanai\ObjectComparator\Differ\Differ;

// create a Differ (injecting a Comparator)
$differ = new Differ(new Comparator());

// create two identical objects
$object1 = new stdClass();
$object1->description = 'Description';
$object1->code = 'DESC';
// ---
$object2 = new stdClass();
$object2->description = 'Description';
$object2->code = 'DESC';

// get the diffs
$diffs = $differ->diff($object1, $object2); // returns a DiffCollection with no diffs
var_dump($diffs->getDiffs()->count()); // outputs 0
var_dump($diffs->getArrayCopy()); // outputs the result as array
var_dump($diffs->printAsJson()); // outputs the result as json


// change one property
$object2->description = 'New Description';

// get the diffs again
$diffs = $differ->diff($object1, $object2); // returns a DiffCollection with the diffs
var_dump($diffs->getDiffs()->count()); // outputs 1
var_dump($diffs->getArrayCopy()); // outputs the result as array
var_dump($diffs->printAsJson()); // outputs the result as json

基本用法 - Logger

<?php

use brunohanai\ObjectComparator\Differ\Differ;
use brunohanai\ObjectComparator\Comparator;
use brunohanai\ObjectComparator\Differ\Logger\DefaultLogger;
use Psr\Log\LogLevel;

// create a Differ (injecting a Comparator)
$differ = new Differ(new Comparator());

// create a Logger
$logger = new DefaultLogger();

// create two different objects
$object1 = new stdClass();
$object1->description = 'Description';
$object1->code = 'DESC';
// ---
$object2 = new stdClass();
$object2->description = 'New Description';
$object2->code = 'NEW_DESC';

// get the diffs
$diffs = $differ->diff($object1, $object2); //returns a DiffCollection with two diffs (description and code)

// log (passing the diffs and log level)
$logger->log($diffs, false, LogLevel::INFO); // the DefaultLogger puts a new line in your default PHP error_log file

高级 - 创建自己的Logger

示例Logger,将信息记录到数据库中(根据您的需求调整代码)

<?php

use brunohanai\ObjectComparator\Differ\DiffCollection;
use brunohanai\ObjectComparator\Differ\Logger\ILogger;
use Psr\Log\LogLevel;

// adjust the class name
class DatabaseDifferLogger implements ILogger
{
    private $data;

    public function __construct()
    {
        $this->data = new ClogLoginHistData(); // this is my own class, adjust it
    }

    public function log(DiffCollection $diffs, $slim_version = false, $level = LogLevel::DEBUG)
    {
        // this is my code, adjust it:
        
        $obj = new ClogLoginHist(); // this is my own object, adjust it

        $obj->setData(date('Y-m-d'), false); // info that are not in DiffCollection
        $obj->setHora(date('H:i:s')); // info that are not in DiffCollection
        
        $obj->setLogin($_SESSION['USER_ID']);  // this is my own info, are not in DiffCollection
        
        $obj->setSessao($diffs->printAsJson($slim_version), false); // DiffCollection result as JSON

        $this->data->insertClogLoginHist($obj); // this is my own method, adjust it 
    }
}

使用Differ + Logger

<?php
require_once('DatabaseDifferLogger.php');

use brunohanai\ObjectComparator\Differ\Differ;
use brunohanai\ObjectComparator\Comparator;
use Psr\Log\LogLevel;

// create a Differ (injecting a Comparator)
$differ = new Differ(new Comparator());

// create your new Logger
$logger = new DatabaseDifferLogger();

// create two different objects
$object1 = new stdClass();
$object1->description = 'Description';
$object1->code = 'DESC';
// ---
$object2 = new stdClass();
$object2->description = 'New Description';
$object2->code = 'NEW_DESC';

// get the diffs
$diffs = $differ->diff($object1, $object2); //returns a DiffCollection with two diffs (description and code)

// log (passing the diffs and log level)
$logger->log($diffs, true, LogLevel::INFO); // your Logger will execute your code

关于

要求

  • 支持PHP 5.3.3或更高版本。

报告错误并请求改进

  • 错误和改进将通过GitHub处理。

作者

  • Bruno Hanai

许可

MIT许可 - 有关详细信息,请参阅LICENSE文件。