承接 icecave/parity 相关项目开发

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

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

icecave/parity

Composer 安装命令:

composer require icecave/parity

包简介

A customizable deep comparison library.

README 文档

README

Build Status Code Coverage Latest Version

Parity is a deep comparison library for PHP.

composer require icecave/parity

Rationale

PHP does not provide a way to reliably and strictly compare values of heterogeneous types. Most of the built-in comparison operators perform often undesired type-juggling. The one exception is the strict equality operator, which has the further caveat that it can only compare objects by their identity. No type-strict mechanism is provided for comparing objects by their properties; nor are there any type-strict versions of the relative comparison operators (less-than, greater-than, etc).

Parity aims to fill the void by providing a comparison engine with the following features:

  • Type-strict comparison of arrays and objects by their elements
  • Recursion-safe comparison of objects
  • Natural, type-strict comparison semantics for built-in types
  • Powerful mechanisms for classes to customize comparison behavior

Example

The Parity comparison engine is accessed via static methods on the Parity facade class. These methods accept any types and are guaranteed to produce a deterministic comparison result1. Some basic examples are shown below using integers.

use Icecave\Parity\Parity;

// The compare() method provides a strcmp-style comparison, and hence can be
// used as a sorting function for operations such as usort()
assert(Parity::compare(1, 2) < 0);

// The following methods are convenience methods, implemented on top of compare().
assert(Parity::isEqualTo(1, 2) === false);
assert(Parity::isNotEqualTo(1, 2) === true);
assert(Parity::isNotEqualTo(1, 2) === true);
assert(Parity::isLessThan(1, 2) === true);
assert(Parity::isLessThanOrEqualTo(1, 2) === true);
assert(Parity::isGreaterThan(1, 2) === false);
assert(Parity::isGreaterThanOrEqualTo(1, 2) === false);

Concepts

Comparable

The core concept of Parity is the comparable. A comparable is any object that provides its own behavior for comparison with other values. The following refinements of the comparable concept are supported by the comparison engine:

  • Restricted Comparable: A comparable that can be queried as to which values it may be compared to.
  • Self Comparable: A comparable that may only be compared to other objects of exactly the same type.
  • Sub Class Comparable: A comparable that may only be compared to other objects of the same or a derived type.
  • Any Comparable: A comparable that may be freely compared to values of any other type.

Comparator

A Comparator defines comparison behavior for values other than itself. Parity provides the following comparator implementations:

Algorithm Resolution

The following process is used by Parity::compare($A, $B) to determine which comparison algorithm to use:

Use $A->compare($B) if:

  1. $A is Any Comparable; or
  2. $A is Restricted Comparable and $A->canCompare($B) returns true; or
  3. $A is Self Comparable and $A is exactly the same type as $B; or
  4. $A is Sub Class Comparable and $B is an instance of the class where $A->compare() is implemented

If none of the conditions above are true, the comparison is tried in reverse with $A on the right hand side and $B on the left; the result is also inverted. If still no comparison is possible, Parity falls back to a strictly-typed deep comparison.

When comparing scalar types, integers and doubles (PHP's only true numeric types) are treated as though they were the same type, such that the expression 3 < 3.5 < 4 holds true. Numeric strings are not compared in this way.

Caveats

  1. Comparison of recursive objects is not a truly deterministic operation as objects are compared by their object hash where deeper comparison would otherwise result in infinite recursion.

icecave/parity 适用场景与选型建议

icecave/parity 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.23M 次下载、GitHub Stars 达 49, 最近一次更新时间为 2013 年 05 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「less」 「sort」 「equality」 「comparison」 「compare」 「sorting」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 icecave/parity 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 icecave/parity 我们能提供哪些服务?
定制开发 / 二次开发

基于 icecave/parity 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 7.23M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 51
  • 点击次数: 29
  • 依赖项目数: 12
  • 推荐数: 0

GitHub 信息

  • Stars: 49
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-05-29