咨询/json-diff

1.0.1 2018-03-09 17:02 UTC

This package is auto-updated.

Last update: 2024-09-17 02:18:55 UTC


README

从两段JSON中获取可读的差异。

安装

composer require konsulting/json-diff

用法

有几种使用类的方法。这取决于您的偏好,请参见以下示例。也可以从差异中排除一组键,如下所示。

<?php

use Konsulting\JsonDiff;

// Using 'new'
$diff = (new JsonDiff('["json"]'))->exclude(['key'])->compareTo('["different_json"]');

// Using a simple factory method
$diff = JsonDiff::original('["json"]')->exclude(['key'])->compareTo('["different_json"]');

// Using a simple 'all-in-one' static method.
$diff = JsonDiff::compare($original = '["json"]', $new = '["different_json"]', $exclude = ['key']);

输出是一个JsonDiffResult,它存储以下信息

  • original - 原始json作为数组
  • new - 新json作为数组
  • added - 添加的内容
  • removed - 删除的内容
  • diff - 添加和删除的合并差异作为数组
  • changed - 一个布尔值,表示是否有相关更改

JsonDiffResult允许以多种方式访问结果

<?php
    $diff->toArray();
    
    [
        'original' => ['...'],
        'new' => ['...'],
        'diff' => [
            'added' => ['...'],
            'removed' => ['...']
        ],
        'changed' => true // or false if nothing changed
    ];
    
    $diff->toJson(); // Json representation of toArray()
    
    // All the properties can be accessed using array or object notation;
    // original, new, added, removed, diff, changed.
    
    $diff->diff;
    $diff['diff'];

贡献

欢迎贡献,并将得到充分认可。我们将接受通过Pull Request的贡献。

  • 使用PSR-2编码规范
  • 添加测试,如果不确定如何操作,请咨询。
  • 在readme.md中记录行为变化。

测试

我们使用PHPUnit

使用PHPUnit运行测试:vendor/bin/phpunit