inspirum/arrayable
Composer 安装命令:
composer require inspirum/arrayable
包简介
PHP arrayable interface for handling casting to an array
README 文档
README
Motivation
Unfortunately PHP does not have a nice way how to typecast objects to array.
There is the __toString() magic method for \Stringable interface (since PHP 8.0) and the jsonSerialize() method for \JsonSerializable interface (since PHP 5.4), but __toArray() method is not (and will not) be supported – there are just several rejected draft RFC (object_cast_to_types, to array, ...) that suggests some kind of object to scalar type casting.
But so far (at least) there is no way to implement some (not even magic) method to be called when cast to array.
Ideally, something like this would work:
class Person { public function __construct( public string $name, protected string $username, private string $password, ) {} public function __toArray(): array { return [ 'name' => $this->name, 'email' => $this->username, ]; } } $person = new Person('John Doe', 'j.doe@example.com', 'secret_pwd'); $personArray = (array) $person; // casting triggers __toArray() /** var_dump($personArray); [ 'name' => 'John Doe' 'email' => 'j.doe@example.com' ] */
but actually it cast to array like this:
/** var_dump($personArray); [ 'name' => 'John Doe' '*username' => 'j.doe@example.com' 'Person@password' => 'secret_pwd' ] */
Usage example
All the code snippets shown here are modified for clarity, so they may not be executable.
This package implements simple \Arrayable (or \Inspirum\Arrayable\Arrayable) interface.
/** @implements \Arrayable<string, string> */ class Person implements \Arrayable { public function __construct( public string $name, protected string $username, protected string $password, ) {} /** @return array<string, string> */ public function __toArray(): array { return [ 'name' => $this->name, 'email' => $this->username, ]; } } $person = new Person('John Doe', 'j.doe@example.com', 'secret_pwd');
There is \is_arrayable() function (or \Inspirum\Arrayable\Convertor::isArrayable() method) to check if given data are able to type cast itself to array.
var_dump(\is_arrayable([1, 2, 3])); // bool(true) var_dump(\is_arrayable(new \ArrayIterator([1, 2, 3]))); // bool(true) var_dump(\is_arrayable(new \ArrayObject([4, 5, 6]))); // bool(true) var_dump(\is_arrayable((function () { yield 1; })())); // bool(true) var_dump(\is_arrayable(1)); // bool(false) var_dump(\is_arrayable(new \stdClass())); // bool(false) var_dump(\is_arrayable(new class {})); // bool(false) var_dump(\is_arrayable(new class implements \Arrayable {})); // bool(true) var_dump(\is_arrayable($person); // bool(true)
Then there is \to_array() function (or \Inspirum\Arrayable\Convertor::toArray() method) to recursively cast data to array.
$data = \to_array(new \ArrayIterator([1, $person, (object) ['a' => true]])); /** var_dump($data); [ 0 => 1 1 => [ 'name' => 'John Doe' 'email' => 'j.doe@example.com' ] 2 => [ 'a' => true ] */
There is also helper abstract classes for common use for DAO (BaseModel) and collection (BaseCollection) objects.
System requirements
Installation
Run composer require command
$ composer require inspirum/arrayable
Testing
To run unit tests, run:
$ composer test:test
To show coverage, run:
$ composer test:coverage
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email tomas.novotny@inspirum.cz instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
inspirum/arrayable 适用场景与选型建议
inspirum/arrayable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 136.5k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 07 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「arrayable」 「object-to-array」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 inspirum/arrayable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 inspirum/arrayable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 inspirum/arrayable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PHP interface defining a standard method for converting objects to arrays. Ensures consistent method naming and return type for array conversion across different classes.
A library to give objects the ability to be transformed to array data
A library that implements the php-extended/php-arrayable-interface library
统计信息
- 总下载量: 136.5k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 13
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-04