irfantoor/collection
Composer 安装命令:
composer require irfantoor/collection
包简介
Collection implementing ArrayAccess, Countable and IteratorAggregate
README 文档
README
Collection implementing ArrayAccess, Countable and IteratorAggregate
The identifiers can use dot notation to access an identifier down a hierarchical
level, e.g. to access $config['debug']['log']['file'] you can code in
doted notation as: $config['debug.log.file']
Initializing
You can initialize by passing an array of key value pairs while creating a new instance.
<?php use IrfanTOOR\Collection; $init = [ 'app' => [ 'name' => 'My App', 'version' => '1.1', ], 'debug' => [ 'level' => 1, 'log' => [ 'enabled' => 0, 'channel' => 'DEBUG', 'file' => '/tmp/debug.log', ], ] ]; $app = new IrfanTOOR\Collection($init);
Setting
You can by set an identifier in the collection by using the method 'set':
use IrfanTOOR\Collection; $app = new IrfanTOOR\Collection(); # setting hello => world $app->set('hello', 'world'); # using an array notation $app['hello'] = 'world'; # defining multiple $app->setMultiple([ 'hello' => 'world', 'box' => 'empty', 'something' => null, 'array' => [ 'action' => 'go', 'step' => 1, ], ]); # defining sub values $app->set('app.name', 'Another App'); $app->set('debug.level', 2);
or by using array access mechanism:
$app['hello'] = 'world'; $app['debug.log.file'] = '/my/debug/log/file.log';
Getting
You can get the stored value in the collection by its identifier using the method 'get':
$debug_level = $app->get('debug.level'); # returns the value stored against this identifier or returns 0 if the identifier # is not present in the collection $debug_log_level = $app->get('debug.log.level', 0);
you can also use the array access:
$debug_level = $app['debug.level']; # returns the value stored against this identifier or returns 0 if the identifier # is not present in the collection $debug_log_level = $app['debug.log.level'] ?? 0;
Checking if a value is present in the collection
You can use the method 'has' to check if the collection has an entry identified with the identifier id:
if ($app->has('debug.level')) { # this will be executed even if the given identifier has the value NULL, 0 # or false echo 'debug.level is present in the collection' }
using the array access the above code will become:
if (isset($app['debug.level']) { # this will be executed even if the given identifier has the value NULL, 0 # or false echo 'debug.level is present in the collection' }
Removing an entry
You can use the method 'remove' or unset on the element:
# using method remove $app->remove('debug.level'); # using unset unset($app['debug.level']);
Collection to Array
The method 'toArray' can be used to convert the collection into an array:
$array = $app->toArray();
Array of identifiers
The array of identifiers can be retrieved by using the method 'keys':
$ids_array = $app->keys();
Count
The number of items present in the collection can be retrieved using the method 'count'. Note that it will return the count of the items at base level.
# will return 2 for the Collection defined in initialization section $count = $app->count();
Iteration
The collection can directly be used in a foreach loop or wherever an iterator is used. for example the code:
foreach($app->toArray() as $k => $v) { print_r([$k => $v]); }
can be simplified as:
foreach($app as $k => $v) { print_r([$k => $v]); }
Lock
A collection can be made read only when once it is initialised by using the lock function, the values can not be added, modified, or removed from the collection afterwards.
Use case for a readonly collection is configuration of an application.
e.g.
$config = new IrfanTOOR\Collection([ 'app' => [ 'name' => 'My App', 'version' => '1.1', ], 'debug' => [ 'level' => 1, 'log' => [ 'enabled' => 0, 'channel' => 'DEBUG', 'file' => '/tmp/debug.log', ], ] ]); $config->lock(); # will not modify the values $config->set('debug.level', 0); $config->set('app.version', '1.2'); # will not remove the value $config->remove('debug.log');
Utility fuctions
filter
Returns the collection of the elements which return true
NOTE: $callback function must uses parameters in the following order for the provided callback function: param_1 $value Value of the current element param_2 $key Key of the current element
$c = new Collection(['10', 1, 2, 3, 4]); $callback = function ($key, $value) { return is_int($value); }; $int = $c->filter($callback); # 1, 2, 3, 4
map
Returns a collection with the callback applied to the element values of this collection:
$callback = function ($key, $value) { return $value * $value; }; # int is the collection from previous example $int2 = $int->map($callback); # 1, 4, 9, 16
reduce
Reduces the array to a result, by applying the function to all of its elements
NOTE: $callback function must uses parameters in the following order: param_1 $carry Result of callback operation on the previous element param_2 $value Value of the current element param_3 $key Key of the current element
$callback = function ($key, $value, $carry) { return $carry + $value; }; $sum = $int2->reduce($callback); # 30
irfantoor/collection 适用场景与选型建议
irfantoor/collection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 265 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 12 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「iterator」 「array」 「serializable」 「collection」 「dot-notation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 irfantoor/collection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 irfantoor/collection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 irfantoor/collection 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
collection php library
Kinikit - PHP Application development framework MVC component
PHP PDO Mysql select statement iterator implemented as multiple queries using LIMIT clauses
Iteration tools for PHP
Sequential way to run Symfony Processes in parallel
统计信息
- 总下载量: 265
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-12-24