infocyph/arraykit
Composer 安装命令:
composer require infocyph/arraykit
包简介
A Collection of useful PHP array functions.
关键字:
README 文档
README
ArrayKit is a modern PHP 8.4+ library for elegant, high-performance array manipulation, dot notation utilities, dynamic configuration, hookable collections, and more. From shallow single arrays to deeply nested data structures — ArrayKit provides a fluent, reliable toolkit for real-world PHP projects.
Features at a Glance
- Single-Dimensional Helpers
- Multi-Dimensional Helpers
- Dot Notation Get/Set/Flatten
- Dynamic Config with Hooks
- Collection & Hooked Collection
- Unified Facade (
ArrayKit) - Traits for DTO & Hooking
- Pipeline for Collection Ops
- LazyCollection for Generator-Based Flows
- ArrayShape Validation Helper
- Compiled Config + Lazy Namespace Cache
- Namespaced Helpers + Optional Globals
Modules
Array Helpers
| Helper | Description |
|---|---|
| ArraySingle | Helpers for single-dimensional arrays (set ops, mapWithKeys, countBy, min/max, paginate, duplicates, averages). |
| ArrayMulti | Helpers for multi-dimensional arrays (flatten, collapse, depth, keyBy/indexBy, firstWhere, recursive sort/filter). |
| DotNotation | Get/set/remove values using dot keys; wildcard support; escaped literal-dot paths; flatten & expand. |
| ArrayShape | Lightweight array-shape assertions for row validation (require). |
| BaseArrayHelper | Internal shared base for consistent API across helpers. |
| ArraySharedOps | Internal shared operations used by ArraySingle and ArrayMulti (each/every/partition/skip*). |
Config System
| Class | Description |
|---|---|
| Config | Dot-access configuration loader with explicit hook-aware variants (getWithHooks, setWithHooks, fillWithHooks) plus compiled cache export/load and read memoization. |
| LazyFileConfig | First-segment lazy loader (db.host loads db.php on demand) with namespace cache files for structural reads and a flat leaf-index cache for exact scalar lookups. |
| BaseConfigTrait | Shared config logic. |
Collections
| Class | Description |
|---|---|
| Collection | OOP array wrapper implementing ArrayAccess, IteratorAggregate, Countable, JsonSerializable. |
| HookedCollection | Extends Collection with on-get/on-set hooks for real-time transformation of values. |
| Pipeline | Functional-style pipeline for chaining operations on collections. |
| LazyCollection | Generator-backed lazy operations (mapLazy, filterLazy, chunkLazy, take, takeUntil). |
| BaseCollectionTrait | Shared collection behavior. |
Traits
| Trait | Description |
|---|---|
| HookTrait | Generic hook system for on-get/on-set callbacks. Used by Config, LazyFileConfig, and HookedCollection. |
| DTOTrait | Utility trait for DTO-like behavior: populate, extract, cast arrays/objects easily. |
Helper Functions
| Helper Surface | Description |
|---|---|
Infocyph\ArrayKit\* |
Namespaced helper functions (compare, array_get, array_set, collect, chain) autoloaded by default. |
functions.php |
Optional global helper variants (manual include when needed). |
➤ Facade
| Class | Description |
|---|---|
| ArrayKit | Single entry point for arrays, dot tools, config, and collections (single(), multi(), etc.). |
Requirements
- PHP 8.4 or higher
Installation
composer require infocyph/arraykit
<?php // Namespaced helpers are autoloaded by default. use function Infocyph\ArrayKit\array_get; use function Infocyph\ArrayKit\array_set; use function Infocyph\ArrayKit\collect; use function Infocyph\ArrayKit\chain; // Optional: enable global helpers in projects that explicitly want them. require_once __DIR__ . '/vendor/infocyph/arraykit/src/functions.php';
Quick Examples
One Facade Entry Point
use Infocyph\ArrayKit\ArrayKit; $isList = ArrayKit::single()->isList([1, 2, 3]); // true $flat = ArrayKit::multi()->flatten([[1], [2, [3]]]); // [1, 2, 3] $name = ArrayKit::dot()->get(['user' => ['n' => 'A']], 'user.n'); // A $config = ArrayKit::config(['app' => ['env' => 'local']]); $env = $config->get('app.env'); // local
Single-Dimensional Helpers
use Infocyph\ArrayKit\Array\ArraySingle; $list = [1, 2, 3, 2]; // Is it a list? $isList = ArraySingle::isList($list); // true // Duplicates $dupes = ArraySingle::duplicates($list); // [2] // Contains checks $hasAll = ArraySingle::containsAll($list, [1, 2]); // true $hasAny = ArraySingle::containsAny($list, [99, 2]); // true // Pagination $page = ArraySingle::paginate($list, page:1, perPage:2); // [1, 2]
Multi-Dimensional Helpers
use Infocyph\ArrayKit\Array\ArrayMulti; $data = [ [1, 2], [3, [4, 5]] ]; // Flatten to one level $flat = ArrayMulti::flatten($data); // [1, 2, 3, 4, 5] $flatZero = ArrayMulti::flatten($data, 0); // [[1, 2], [3, [4, 5]]] $flatOne = ArrayMulti::flatten($data, 1); // [1, 2, 3, [4, 5]] // Multi-column and query helpers $sortedMany = ArrayMulti::sortByMany($rows, [ ['status', 'asc'], ['created_at', 'desc'], ]); $active = ArrayMulti::whereStartsWith($rows, 'status', 'act', false); $match = ArrayMulti::whereLike($rows, 'email', '%@example.com'); $first = ArrayMulti::firstWhereIn($rows, 'role', ['admin', 'editor']); $uniqueUsers = ArrayMulti::uniqueBy($rows, 'email'); $dupeUsers = ArrayMulti::duplicatesBy($rows, fn ($row) => strtolower((string) ($row['email'] ?? ''))); // Collapse one level $collapsed = ArrayMulti::collapse($data); // [1, 2, 3, [4, 5]] // Nesting depth $depth = ArrayMulti::depth($data); // 3 // Recursive sort $sorted = ArrayMulti::sortRecursive($data);
Dot Notation
use Infocyph\ArrayKit\Array\DotNotation; $user = [ 'profile' => ['name' => 'Alice'] ]; // Get value $name = DotNotation::get($user, 'profile.name'); // Alice $literal = DotNotation::get(['profile.name' => 'flat'], 'profile\\.name'); // flat // Set value DotNotation::set($user, 'profile.email', 'alice@example.com'); // Flatten $flat = DotNotation::flatten($user); // wildcard set DotNotation::set($user, 'users.*.active', true); // [ 'profile.name' => 'Alice', 'profile.email' => 'alice@example.com' ]
Config Hooks (Explicit)
use Infocyph\ArrayKit\Config\Config; $config = new Config(); // Load from file $config->loadFile(__DIR__.'/config.php'); // Hook: auto-hash password when set $config->onSet('auth.password', fn($v) => password_hash($v, PASSWORD_BCRYPT)); // Hook: decrypt when getting 'secure.key' $config->onGet('secure.key', fn($v) => decrypt($v)); // Use it $config->setWithHooks('auth.password', 'secret123'); $hashed = $config->getWithHooks('auth.password'); // Typed getters + state helpers $port = $config->getInt('db.port', 3306); $config->snapshot('before-runtime'); $config->merge(['app' => ['env' => 'production']]); $changed = $config->changed('before-runtime'); $config->restore('before-runtime'); // Compiled cache export / load $config->exportCache(__DIR__ . '/bootstrap/cache/config.php'); $cached = new Config(); $cached->loadCache(__DIR__ . '/bootstrap/cache/config.php');
Hooked Collection
use Infocyph\ArrayKit\Collection\HookedCollection; $collection = new HookedCollection(['name' => 'alice']); // Hook on-get: uppercase $collection->onGet('name', fn($v) => strtoupper($v)); // Hook on-set: prefix $collection->onSet('role', fn($v) => "Role: $v"); echo $collection['name']; // ALICE $collection['role'] = 'admin'; echo $collection['role']; // Role: admin
🔹 DTO Trait Example
use Infocyph\ArrayKit\traits\DTOTrait; class UserDTO { use DTOTrait; public string $name; public string $email; } $user = new UserDTO(); $user->fromArray(['name' => 'Alice', 'email' => 'alice@example.com']); $array = $user->toArray(); // Advanced hydration / export $user->hydrate(['name' => 'Alice'], mapping: ['name' => 'full_name']); $deep = $user->toArrayDeep();
Lazy + Shape + Cache
use Infocyph\ArrayKit\Array\ArrayShape; use Infocyph\ArrayKit\ArrayKit; use Infocyph\ArrayKit\Config\LazyFileConfig; $lazy = ArrayKit::lazyCollection(range(1, 10)) ->filterLazy(fn ($v) => $v % 2 === 0) ->take(3) ->all(); // [2, 4, 6] $row = ArrayShape::require( ['id' => 1, 'email' => 'a@example.com', 'roles' => ['admin']], ['id' => 'int', 'email' => 'string', 'roles' => 'list<string>'], ); $config = new LazyFileConfig(__DIR__ . '/config', namespaceCacheDirectory: __DIR__ . '/bootstrap/cache/config'); $config->warmNamespaceCache(['db', 'cache']); // Exact scalar leaf reads can hit bootstrap/cache/config/__flat.php first. $host = $config->get('db.host');
Behavior Notes
ArrayMulti::flatten($array, 0)keeps top-level values unchanged;1flattens one level;INFfully flattens.ArraySingle::avg(),sum(),isPositive(), andisNegative()only consider numeric values (non-numeric values are ignored).ArraySingle::paginate()requirespage >= 1andperPage >= 1(throwsInvalidArgumentExceptionotherwise).- Callback-based row helpers (
ArrayMulti::sortBy(),sum(),maxBy(),minBy()) support($row, $key). DotNotationtreats existingnullkeys/properties as present (does not fall back to defaults).DotNotation::hasWildcard(),paths(),matches(),rename(), andmove()are available for wildcard/path operations.- For untrusted/deep payloads, use bounded traversal variants:
DotNotation::getSafe(),ArrayMulti::depthGuarded(),flattenGuarded(), andsortRecursiveGuarded(). LazyFileConfignamespace cache writes one cache file per namespace plus a shared__flat.phpfile containing only final scalar/null leaf values for exact-key fast paths.
Security
Protected by PHPForge — an automated quality and security gate for PHP projects.
Made with ❤️ for the PHP communityMIT Licensed
Documentation • Security • Code of Conduct • Contributing • Report Bug • Request Feature
infocyph/arraykit 适用场景与选型建议
infocyph/arraykit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.14k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2025 年 01 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「config」 「array」 「collection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 infocyph/arraykit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 infocyph/arraykit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 infocyph/arraykit 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
A Zend Framework module to quickly and easily set PHP settings.
This package provides type-safe extension of the laravel collection, forcing a single type of object.
Trait providing methods to set class properties with an array.
Traits to build collections of specific objects
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
统计信息
- 总下载量: 5.14k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 19
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-29