承接 sebvie/php-object-history 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sebvie/php-object-history

Composer 安装命令:

composer require sebvie/php-object-history

包简介

Record the change of a PHP object and display it in a csv file

README 文档

README

Build Status Coverage Status codecov

php-object-history

The package records changes of a PHP object and persists it to a storage. This can be useful if you have a stream of data and you only want the get the change set and throw away the other data to reduce memory usage.

Installation:

composer require sebvie/php-object-history

Example:

Inititate Storage and Comparer:

use PhpObjectHistory\Comparer\ObjectComparer;
use PhpObjectHistory\Storage\CsvFileStorage;
use PhpObjectHistory\ObjectHistory;

$storage = new CsvFileStorage();
$storage->setCsvFilePath($filePath);

$objectComparer = new ObjectComparer();

$objectHistory = new ObjectHistory(
    $storage,
    $objectComparer
);

Add objects:


$object = new \stdClass();
$object->testProperty = 1;
$object->testPropertyUnchanged = true;

$objectHistory->addObject($object);

$object = new \stdClass();
$object->testProperty = 2;
$object->testPropertyUnchanged = true;

$objectHistory->addObject($object);

Result is csv file containing:

testProperty,testPropertyUnchanged
1;true
2

Custom Formatters

You can add custom Formatters to flatten your object:

use PhpObjectHistory\Formatter\ObjectFormatterInterface

class ToStringFormatter implements ObjectFormatterInterface
{

    /**
     * @param object $object
     * @return string
     */
    public function format(object $object): string
    {
        return $object->__toString();
    }

    /**
     * @param object $object
     * @return bool
     */
    public function supports(object $object): bool
    {
        return method_exists($object, '__toString');
    }
}

add the formatter:

$formatter = new CustomFormatter();

$storage = new CsvFileStorage();
$storage->setCsvFilePath($filePath);
$storage->getObjectFormatterHandler()->addFormatter($formatter);

$objectComparer = new ObjectComparer();
$objectComparer->getObjectFormatterHandler()->addFormatter($formatter);

Implement custom Storages

use PhpObjectHistory\Entity\ObjectChange;

class InMemoryStorage implements StorageInterface
{
    
    /**
     * @var array
     */
    protected $result = [];
    
    /**
     * @param object $object
     */
    public function setInitialObject(object $object): void
    {
        $this->result[] = $object;
    }

    /**
     * @param ObjectChange[] $objectChanges
     */
    public function addObjectChanges(array $objectChanges): void
    {
        if (empty($objectChanges)) {
            return;
        }

        $this->result[] = $objectChanges;
    }

    /**
     * @return array
     */
    public function getResult(): array
    {
        return $this->result;
    }

}

Available Storage

  • CsvFileStorage
  • InMemoryStorage

Available Formatters

  • DatetimeFormatter
  • ToStringFormatter

ignore object properties

You can ignore changes of certian properties. This will not trigger an object change.

This is useful when you have properties like timestamps which always change but can be ignored.

$ignoreAttributes = ['boolProperty'];
$objectComparer = new ObjectComparer();
$objectComparer->setIgnoreAttributes($ignoreAttributes);

统计信息

  • 总下载量: 565
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: mit
  • 更新时间: 2019-10-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固