honeycomb / method-sort
框架/库无关的杂项工具类和对象
3.0.3
2022-04-05 01:57 UTC
Requires
- php: >=5.6.0
Requires (Dev)
- codeception/codeception: ^2.4
This package is auto-updated.
Last update: 2024-09-21 22:07:21 UTC
README
Honeycomb 是一组无框架/库约束的有用工具对象。每个类都旨在完成一项任务,并且做得很好。
MethodSort
MethodSort
类是一个状态对象,实现了 __invoke
方法,因此该对象的实例可以用作标准排序的预配置比较函数。方法排序允许通过调用方法的返回结果来排序对象。
例如。假设你有一个 Person
class Person { public function __construct($firstname, $lastname) { $this->firstname = $firstname; $this->lastname = $lastname; } public function getFirstName() { return $this->firstname; } public function getLastName() { return $this->lastname; } }
现在假设你有一组人
$objects = [ 'ms' => new Person('Matthew', 'Sahagian'), 'mj' => new Person('Matthew', 'Jones'), 'bt' => new Person('Brian', 'Tam') ];
你可以使用 MethodSort
按姓名排序,然后按姓氏排序
$method_sort = new Honeycomb\MethodSort(); $method_sort->setOrder([ 'getFirstName' => 'asc', 'getLastName' => 'asc' ]); uasort($objects, $method_sort);
对象现在包含
array ( 'bt' => Person::__set_state(array( 'firstname' => 'Brian', 'lastname' => 'Tam', )), 'mj' => Person::__set_state(array( 'firstname' => 'Matthew', 'lastname' => 'Jones', )), 'ms' => Person::__set_state(array( 'firstname' => 'Matthew', 'lastname' => 'Sahagian', )), )