cryonighter/object-column
Composer 安装命令:
composer require cryonighter/object-column
包简介
This package provides the object_column() function, which works similarly to the array_column() function of the standard library, but having the ability to work with objects
README 文档
README
This package provides the object_column() function, which works similarly to the array_column() function of the standard library, but having the ability to work with objects.
To search for columns in objects, both the public properties of these objects and the methods getColumnName() / hasColumnName() / isColumnName() / columnName() (in that exact order), as well as by the methods of the ArrayAccess interface.
Function object_column() is fully backward compatible with array_column() and can work with regular arrays the same way.
In addition, the object_column() function supports call chainings.
Highlights
System Requirements
You need:
- PHP >= 8.1.0 but the latest stable version of PHP is recommended
Install
Via Composer
$ composer require cryonighter/object-column
Usage
The function works as follows:
Example №1: simple access to public properties
use function Cryonighter\ObjectColumn\object_column; $objects = [ new class { public $foo = '123'; public $bar = '456'; public $baz = '789'; }, new class { public $foo = 'qwe'; public $bar = 'asd'; public $baz = 'zxc'; }, ]; $result = object_column($objects, 'foo', 'bar');
Example №2: chain of calls to getters
use function Cryonighter\ObjectColumn\object_column; $objects = [ new class { public function getFoo(): object { return new class { public function baz(): string { return '123'; } }; } public function getBar(): object { return new class { public function buz(): string { return '456'; } }; } }, new class { public function getFoo(): object { return new class { public function baz(): string { return 'qwe'; } }; } public function getBar(): object { return new class { public function buz(): string { return 'asd'; } }; } }, ]; $result = object_column($objects, 'foo.baz', 'bar.buz');
Example №3: chain of calls to ArrayAccess objects
use function Cryonighter\ObjectColumn\object_column; $objects = [ new ArrayObject([ 'foo' => new ArrayObject(['baz' => '123']), 'bar' => new ArrayObject(['buz' => '456']), ]), new ArrayObject([ 'foo' => new ArrayObject(['baz' => 'qwe']), 'bar' => new ArrayObject(['buz' => 'asd']), ]), ]; $result = object_column($objects, 'foo.baz', 'bar.buz');
Result
In all cases, the result will be the same
[456 => '123', 'asd' => 'qwe']
Callable arguments
For more complex cases, you can pass your own handlers, which will be applied to all objects of the first nesting level. You will have to implement the handle of the following nesting levels yourself in your callback functions.
Example №4: callable arguments
use function Cryonighter\ObjectColumn\object_column; $objects = [ new class { public function getFoo(): string { return '123'; } public function getBar(): string { return '456'; } public function getBaz(): string { return '789'; } }, new class { public function getFoo(): string { return 'qwe'; } public function getBar(): string { return 'asd'; } public function getBaz(): string { return 'zxc'; } }, ]; $result = object_column( $objects, fn(object $object): string => $object->getFoo() . '-' . $object->getBar(), fn(object $object): string => $object->getBar() . '-' . $object->getBaz(), );
Result
[
'456-789' => '123-456',
'asd-zxc' => 'qwe-asd',
];
Array indexing
Also, the function can be used to index an array, for this it is enough not to pass the first argument
Example №5: array indexing
use function Cryonighter\ObjectColumn\object_column; $objects = [ [ 'foo' => ['baz' => '123'], 'bar' => ['buz' => '456'], ], [ 'foo' => ['baz' => 'qwe'], 'bar' => ['buz' => 'asd'], ], ]; $result = object_column($objects, null, 'bar.buz'); // or $result = object_column($objects, indexKey: 'bar.buz');
Result
[
'456' => [
'foo' => ['baz' => '123'],
'bar' => ['buz' => '456'],
],
'asd' => [
'foo' => ['baz' => 'qwe'],
'bar' => ['buz' => 'asd'],
],
];
For clarity, an example with a simple array is given, but it will work for all the cases listed above.
If no second or third argument is passed to the function, the original array will be returned. This operation is meaningless.
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ php vendor/phpunit/phpunit/phpunit tests
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email cryonighter@yandex.ru instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
cryonighter/object-column 适用场景与选型建议
cryonighter/object-column 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.19k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 05 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「object」 「column」 「array_column」 「iterable」 「object_column」 「iterable_column」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cryonighter/object-column 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cryonighter/object-column 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cryonighter/object-column 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides functionality for array_column() to projects using PHP earlier than version 5.5.
A set of useful PHP classes.
Replacement for ramsey's array_column package: array_column() for projects using PHP earlier than version 5.5.
Runn Me! Value Objects Library
Display multiple images as a stack in your Filament tables
Laravel SDK mapping data in object
统计信息
- 总下载量: 5.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-10