php-kit/power-primitives
Composer 安装命令:
composer require php-kit/power-primitives
包简介
PHP's arrays and strings as chainable, composable, object-oriented constructs
README 文档
README
PHP arrays and strings with the fluency of modern collection APIs
Why Power Primitives?
Plain PHP arrays and strings are wonderfully flexible, yet they quickly turn into walls of procedural helper calls. Loop counters, temporary variables and manual checks for missing keys clutter the happy path of your application.
The PowerArray and PowerString classes wrap native values in a lightweight object-oriented facade that embraces PHP 8 and makes common transformations readable, chainable and safe.
<?php use function PA; $emails = PA($users) ->filter(fn ($user) => $user['isActive']) ->orderBy('lastLogin', SORT_DESC) ->map(fn ($user) => $user['email']) ->all();
No temporary arrays, no custom utility library scattered throughout your project — just fluent, discoverable methods that compose naturally.
Installation
composer require php-kit/power-primitives
Requires PHP 8.1 or newer (tested up to 8.4-dev). The package ships with a self-contained test runner, so no external dependencies are required to verify your installation.
A tour of PowerArray
Create an instance with PowerArray::of() (copying data) or asPA() for a
zero-cost wrapper around an existing array. From there you gain a toolbox of
methods grouped into categories:
Construction & inspection
of,on,cast, and helpersPA,asPA,toPAfor flexible wrapping.all,first,last,count,getIteratorexpose the underlying data.- Native
ArrayAccesssupport lets you interact using$array[$key]syntax.
Transformation & chaining
append,prepend,merge,stripFirst,stripLast,splice,slice,reindexandsortreshape the structure fluently.map,mapColumns,reduce,repeat,join,toClass,hidrate,toLowerCase(viaPowerString) andjoinRecordshelp you build pipelines without leaving the method chain.
Filtering & searching
filter,find,findAll,missing,indexOf,binarySearch,orderBycover common lookup patterns with optional strict comparisons.pruneremovesnullvalues whileprune_emptyremovesnulland empty strings—perfect for tidying user input.
Field level helpers
extract,fields,getColumn,getColumns,mapColumns,iterateColumnstarget nested keys in arrays or objects without verbose loops.group,indexBy,joinRecords, andhidratehelp you reshape datasets into trees, keyed maps or domain objects in a single expression.
Serialization & compatibility
serialize,unserialize,__serialize,__unserializeensure forward- compatible persistence.joinconverts arbitrarily nested values into aPowerString, stringifying objects and arrays in a readable JSON form.
The entire API is designed for method chaining so you can describe your intent
at a high level and only drop down to native arrays when you call all().
A tour of PowerString
PowerString takes the same philosophy to text processing. Wrap your string via
PowerString::of() or the PS() helper and unlock a Unicode-aware toolkit:
Creation & mutation
append,prepend,concat,repeat,replace,substr,substring,slice,trim,trimLeft,trimRight,toLowerCase,toUpperCaselet you manipulate strings without juggling offsets.
Inspection & navigation
charAt,charCodeAt,length,count,includes,startsWith,endsWith,indexOf,lastIndexOf,indexOfPattern,searchprovide familiar ergonomics with multibyte safety.- Pattern helpers (
match,splitByPattern) transparently upgrade your regexes to Unicode mode and optionally capture global matches.
Interoperability
splitreturns aPowerArrayfor downstream transformations.- Array-style indexing works (
$string[0],isset($string[5]),unset($string[1])). normalize(if the intl extension is available) unifies Unicode sequences.
Convenience helpers
Short global functions live in src/globals.php:
PA($array)/asPA(&$array)/toPA(&$var)for array wrappers.PS($string)/asPS(&$string)/toPS(&$var)for string wrappers.
They make quick scripting or prototyping a breeze:
<?php use function PS; echo PS(' Laravel ') ->trim() ->toUpperCase(); // outputs LARAVEL
Comparing to procedural PHP
Traditional helpers often look like this:
$emails = []; foreach ($users as $user) { if (!empty($user['active']) && isset($user['email'])) { $emails[] = strtolower($user['email']); } }
With Power Primitives the same intent reads like a story:
$emails = PA($users) ->filter(fn ($user) => !empty($user['active'])) ->getColumn('email') ->map('strtolower') ->all();
Each method describes what you want, not the low-level mechanics of how to obtain it. Tests in this repository guard every method, so you can chain with confidence.
Running the tests
The project ships with a deterministic, dependency-free test suite covering the full API surface.
php tests/run.php
The runner prints a friendly checklist so you immediately know the health of
your installation. (CI environments can simply check for an exit code of 0.)
Contributing
Issues and pull requests are welcome! The codebase embraces strict types, PHP 8
features and thoughtful documentation. Please run php tests/run.php before
submitting patches.
License
Power Primitives is open-source software licensed under the MIT license.
php-kit/power-primitives 适用场景与选型建议
php-kit/power-primitives 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.31k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 09 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「helpers」 「utility」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 php-kit/power-primitives 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 php-kit/power-primitives 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 php-kit/power-primitives 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A collection of enhancements and utilities for the Silverstripe UserForms module
Extends TYPO3 EXT:seo hreflang functionality
PHP7 utility classes
HTML and form generation
GHT D-Tools Bundle
Falsy helps you manage half-truths with PHP
统计信息
- 总下载量: 2.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-18