定制 vk-php-utils/chain 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

vk-php-utils/chain

最新稳定版本:1.0.11

Composer 安装命令:

composer require vk-php-utils/chain

包简介

PHP Chain library

README 文档

README

Build Status Latest Stable Version Coverage Status Latest Unstable Version Total Downloads License composer.lock

The library was created to make working with collections more simple in PHP

Requirements

  • PHP >=7.4 (PHP 8.1 is supported)
  • Extensions: json, mbstring

Installation

composer require vk-php-utils/chain:^1.0

Chain API

use VKPHPUtils\Chain\Chain;

// Constructors
Chain::of(...$items): Chain          // Initialize mutable chain
Chain::immutable(...$items): Chain   // Initialize immutable chain

// API
toArray(): array;   // Converts the chain to array (applies all chain-functions)
toChain(): Chain;   // Useful with mutable chain. Returns new chain with all current chain functions applied

hasKey($key): bool              // Check exists the key in current chain or no
hasValue($value): bool;         // Check existence of the value in the chain
search($value)                  // Searches position (key) of the value in current chain. Returns null if the value not found
getValue($key)                  // Get value using key
setValue($key, $value): Chain;  // Set value using key
remove($key): Chain;            // Removes element with index eq to $key
isEmpty(): bool                 // Check is the chain empty
size(): int                     // Get the chain size (number of elements)
count(): int                    // Get the chain size (number of elements)
append($any): Chain;            // Appends value into the chain
prepend($any): Chain;           // Prepend value into the chain

// map/reduce methods
filter(?callable $fn = null): Chain         // Filter the chain using function $fn
map(callable $fn): Chain;                   // Applies function $fn to each chain item and returns chain with mapped entities. Index-safe operation
flatMap(callable $fn): Chain;               // Works like {@see map()} but returns iterable set of mapped objects per each the chain's item
reduce(callable $fn, $initialValue = null)  // Calculate aggregate function result

// keys/values
keys(): Chain;                              // Get keys of the chain
values(): Chain;                            // Get chain values

// chain methods
diff(...$elements): Chain;                  // Calculates diff between current chain and passed one
merge(...$elements): Chain;                 // Merges all elements with current chain
intersect(...$elements): Chain;             // Calculates intersection of two sets (current chain and passed one). Non index-safe operation
intersectKeepIndexes(...$elements): Chain;  // Calculates intersection of two sets (current chain and passed one). Index-safe operation
frequencyAnalysis(): Chain;                 // Created histogram/frequency analysis of the chain
flip(): Chain;                              // Flip the chain
mix(): Chain;                               // Mixes the chain
reverse(bool $saveIndex = false): Chain;    // Reverse current chain
unique(bool $saveIndex = false): Chain;     // Unique items of current chain
slice(int $startIncluded, int $stopExcluded, int $step = 1, bool $saveIndex = false): Chain;                // Get chain's chunk
forEach(callable $fn): Chain;               // Just calls $fn for each chain element

// sort methods
sortKeys(?callable $sortFn = null, ?int $direction = SORT_ASC, ?int $sortFlags = null): Chain;              // Sorts chain by keys
sortValues(?callable $sortFn = null, ?int $direction = SORT_ASC, ?int $sortFlags = SORT_REGULAR): Chain;    // Sorts chain by values. Index-safe operation
sortByProperty(string $propertyName, int $direction = SORT_ASC): Chain;                                     // Sorts collection by given property

Run Tests

./vendor/bin/phpunit ./tests

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-11-16