承接 sgh/comparable-arrays 相关项目开发

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

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

sgh/comparable-arrays

最新稳定版本:v1.0.2

Composer 安装命令:

composer require sgh/comparable-arrays

包简介

Provides generic Comparators for arrays.

README 文档

README

Author Latest Version Software License Build Status

This package provides Comparators for arrays and objects that implement the SPL ArrayAccess interface. They can be used with the sorting and comparing tools in the Comparable package.

Requirements

The package requires PHP 5.4 or later and the Comparable package in version 1.0 or later.

Install

Via Composer

$ composer require sgh/comparable-arrays

Usage

The following comparators are available:

  • KeyComparator: Use array item with specific key for comparison
  • MultiKeyComparator: The same, but with the option to sort by multiple keys (i.e. fall back in case of equality)

You can use all the methods in \SGH\Comparable\SortFunctions and \SGH\Comparable\SetFunctions with the comparators.

KeyComparator Example

use SGH\Comparable\SortFunctions;
use SGH\Comparable\Comparator\StringComparator;
use SGH\Comparable\Arrays\Comparator\KeyComparator;

$arrayOfBooks = array(
    [ 'title' => 'Design Patterns', 'author' => 'Gang of Four', 'year' => 1995 ],
    [ 'title' => 'Clean Code', 'author' => 'Uncle Bob', 'year' => 2008 ],
    [ 'title' => 'Refactoring', 'author' => 'Martin Fowler', 'year' => 1999 ],
    [ 'title' => 'Patterns of Enterprise Application Architecture', 'autor' => 'Martin Fowler', 'year' => 2002 ],
);

// Sort the array of books by year:
SortFunctions::sort($arrayOfBooks, new KeyComparator('year'));

// Sort the array of books by title:
SortFunctions::sort($arrayOfBooks, new KeyComparator('title', new StringComparator));

The default comparator used to compare the array items is NumericComparator, which compares any scalar values and treats them as numbers. To sort by title we needed to specify the comparator explicitly.

If you prefer, you can also use the factory method ::callback() to retrieve a comparison callback, that can be used in any function that expects a user defined comparison callback:

usort($arrayOfBooks, KeyComparator::callback('title', new StringComparator));

MultiKeyComparator Example

use SGH\Comparable\SortFunctions;
use SGH\Comparable\Comparator\ReverseComparator;
use SGH\Comparable\Comparator\StringComparator;
use SGH\Comparable\Arrays\Comparator\MultiKeyComparator;

$arrayOfBooks = array(
    [ 'title' => 'Design Patterns', 'author' => 'Gang of Four', 'year' => 1995 ],
    [ 'title' => 'Clean Code', 'author' => 'Uncle Bob', 'year' => 2008 ],
    [ 'title' => 'Refactoring', 'author' => 'Martin Fowler', 'year' => 1999 ],
    [ 'title' => 'Patterns of Enterprise Application Architecture', 'autor' => 'Martin Fowler', 'year' => 2002 ],
);

// Sort the array of books by author, then by year, descending:
SortFunctions::sort($arrayOfBooks, new MultiKeyComparator([
	'author' => new StringComparator,
	'year'   => new ReverseComparator(new NumericComparator)
]));

Configuration

Both, KeyComparator and MultiKeyComparator accept arrays and objects that implement ArrayAccess as parameters. This default behavior can be changed such that they only accept arrays:

$comparator->setAcceptArrayAccessObject(false);

By default array keys must exist to compare their values. Both comparators have a non-strict mode, where missing items are treated as smaller than everything else. Two missing items are treated as equal.

$comparator->setStrict(false);

Testing

$ phpunit

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email fschmengler@sgh-it.eu instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-04-05