myclabs/array-comparator
Composer 安装命令:
composer require myclabs/array-comparator
包简介
Array comparator
关键字:
README 文档
README
Array comparison helper.
Principle
Here is the default behavior:
| Array 1 | Array 2 | Method called |
|---|---|---|
| foo => Foo | foo => Foo | whenEqual |
| bar => Bar | bar => Foo | whenDifferent |
| baz => Baz | whenMissingRight | |
| bam => Bam | whenMissingLeft |
By default, array keys are compared. This behavior can be customized.
Usage
$comparator = new ArrayComparator(); $comparator->whenEqual(function ($item1, $item2) { // Do your stuff ! }) ->whenDifferent(function ($item1, $item2) { // Do your stuff ! }) ->whenMissingRight(function ($item1) { // Do your stuff ! }) ->whenMissingLeft(function ($item2) { // Do your stuff ! }); $comparator->compare($array1, $array2);
Advanced example for Doctrine entities for example:
$comparator = new ArrayComparator(); // Set that items are considered the same if they have the same id // Array keys are ignored in this example $comparator->setItemIdentityComparator(function ($key1, $key2, $item1, $item2) { return $item1->id === $item2->id; }); // Items have differences if their name differ $comparator->setItemComparator(function ($item1, $item2) { return $item1->name === $item2->name; }); $comparator->whenEqual(function ($item1, $item2) { // Do your stuff ! }) ->whenDifferent(function ($item1, $item2) { // Do your stuff ! }) ->whenMissingRight(function ($item1) { // Do your stuff ! }) ->whenMissingLeft(function ($item2) { // Do your stuff ! }); $comparator->compare($array1, $array2);
Note that you can also use any PHP callable format instead of inline functions, for example:
$comparator->whenDifferent(array($this, 'whenDifferent'));
Documentation
whenEqual- Called when 2 items are found in both arrays, and are equal
$comparator->whenEqual(function ($item1, $item2) { });
whenDifferent- Called when 2 items are found in both arrays, but are different
$comparator->whenDifferent(function ($item1, $item2) { });
whenMissingRight- Called when an item is in the first array (left array) but not in the second (right array)
$comparator->whenMissingRight(function ($item1) { });
whenMissingLeft- Called when an item is in the second array (right array) but not in the first (left array)
$comparator->whenMissingLeft(function ($item2) { });
setItemIdentityComparator- Overrides the default identity comparator which determine if 2 items represent the same thing
Can be used for example to compare the id of the items.
The default behavior compares the array keys using ===.
$comparator->setItemIdentityComparator(function ($key1, $key2, $item1, $item2) { // return true or false });
setItemComparator- Overrides the default item comparator to determine if 2 items (representing the same thing) have differences
Can be used for example to compare specific attributes of the items. The function should return "is equal", i.e. true if items have no differences (then nothing is done because all is good), or false if they have differences (then whenDifferent is called).
The default behavior compares the items using ==.
$comparator->setItemComparator(function ($item1, $item2) { // return true or false });
Custom comparator
There is an alternative to using setItemIdentityComparator and setItemComparator by writing your own comparator class:
class CustomComparator extends ArrayComparator { protected function areSame($key1, $key2, $item1, $item2) { // Your stuff return $item1->id === $item2->id; } protected function areEqual($item1, $item2) { // Your stuff return $item1->name === $item2->name; } }
Installation
Edit your composer.json to add the dependency:
{
"require": {
"myclabs/array-comparator": "1.0.*"
}
}
ArrayComparator is tested with PHP 5.3 and greater.
Changelog
- 1.0: Stable version after testing and use in production (no change from 0.3)
- 0.3: PHP 5.3 compatibility and support all PHP callable types
- 0.2: Allowed to extend the
ArrayComparatorclass and write custom comparators - 0.1: First version
Contribute
Install dependencies with Composer:
composer install
TODO:
- Optimize the array traversals
- Improve documentation ?
myclabs/array-comparator 适用场景与选型建议
myclabs/array-comparator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 197.63k 次下载、GitHub Stars 达 19, 最近一次更新时间为 2013 年 06 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「array」 「comparator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 myclabs/array-comparator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 myclabs/array-comparator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 myclabs/array-comparator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Provides Comparable and Comparator interfaces and methods to sort and compare objects based on these.
PHP Semantic Versioning Parser and Comparator
Trait providing methods to set class properties with an array.
Traits to build collections of specific objects
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
统计信息
- 总下载量: 197.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2013-06-03