alexrili/vephar
Composer 安装命令:
composer require alexrili/vephar
包简介
Small abstraction of api requests and transform responses in collections
README 文档
README
vephar vephar is a simple library that transform you arrays in collections/resources(objects).
Install
Via Composer
composer require alexrili/vephar
Start vephar
Vephar provides you a simple method called response_to_object out of the box.
$yourArray = []; // simple way, just pass your array data, and vephar will make the magic :) $response = response_to_object($yourArray); // passing your own custom contract classes $response = response_to_object($yourArray, YourCustomContractClass::class); // passing your own custom contract classes and your custom collection classes $response = response_to_object($yourArray, YourCustomContractClass::class, YourCustomContractCollection::class);
OR you can call like this
use Hell\Vephar\Response; $yourArray = []; $vephar = new Response(); $response = $vephar->make($yourArray); $response = $vephar->make($yourArray, YourCustomContractClass::class); $response = $vephar->make($yourArray, YourCustomContractClass::class, YourCustomContractCollection::class);
OR by calling static method called toObject
use Hell\Vephar\Response; $response = Response::toObject($yourArray); $response = Response::toObject($yourArray, YourCustomContractClass::class); $response = Response::toObject($yourArray, YourCustomContractClass::class, YourCustomContractCollection::class);
Usage (Automatic way)
The vephar will transform your arrays(and the nested as well) in collections of resources automatically. Every index of your array will be a new attribute of your new collection/resource(object).
use Hell\Vephar\Response; #can be a response from api request $array = [ "title" => "my title", "EMAIL" => "my@email.com", "nested_Array" => [ "address" => "street 10", "postal_code" => 1234 ], "true_array" => [ 123, 10, 15 ] ] # Instantiating (recommended) $response = response_to_object($array)
#Return for collection will be
class Hell\Vephar\Collection#73 (1) {
protected $items =>
array(1) {
[0] =>
class Hell\Vephar\Resource#71 (4) {
public $title =>
string(8) "my title"
public $email =>
string(12) "my@email.com"
public $nestedArray =>
class Hell\Vephar\Resource#72 (2) {
public $address =>
string(9) "street 10"
public $postalCode =>
int(1234)
}
public $trueArray =>
array(3) {
[0] =>
int(123)
[1] =>
int(10)
[2] =>
int(15)
}
}
}
}
#return for a single resource will be
class Hell\Vephar\Resource#74 (4) {
public $title =>
string(8) "my title"
public $email =>
string(12) "my@email.com"
public $nestedArray =>
class Hell\Vephar\Resource#72 (2) {
public $address =>
string(9) "street 10"
public $postalCode =>
int(1234)
}
public $trueArray =>
array(3) {
[0] =>
int(123)
[1] =>
int(10)
[2] =>
int(15)
}
}
Usage (Custom way)
The vephar also allows you to assign your own collection and resource contracts to it.
Important:When you use your own contracts you need to explicitly say to if vephar should or not make somthings like going deeper or not into your nested arrays or change the pattern of you attribute names to camelCase etc.
namespace Hell\Vephar\Fake; use Hell\Vephar\Contracts\CollectionContract; class CustomCollection extends CollectionContract { // This will tell the vephar to goo deeper or not. // The default is false means should not go. protected $keepDigging = false; // This will tell the vephar to change your attributes names to camelCase. // The default is false means will respect whatever you write protected $toCamelCase = false; // This will tell the vephar that you will set the attributes by your self. // The default is false means will the vephar will put values automatically // intou your attributes if they existes on the input data. protected $setters = false; }
namespace Hell\Vephar\Fake; use Hell\Vephar\Contracts\ResourceContract; class CustomResource extends ResourceContract { /** * @bool */ protected $setters = true; /** * @bool */ protected $keepDigging = true; /** * @var */ public $name; /** * @var */ public $email; /** * @param mixed $email */ public function setName($name): void { $this->name = $name; } /** * @param mixed $email */ public function setEmail($email): void { $this->email = $email; } }
#can be a response from api request $array = [ "title" => "my title", "EMAIL" => "my@email.com", "nested_Array" => [ "address" => "street 10", "postal_code" => 1234 ], "true_array" => [ 123, 10, 15 ] ] $vephar = response_to_object($array, CustomResourceClass::class, CustomCollectionClass:class);
In this case the response will be your own custom classes
#Return for collection will be
class Hell\Vephar\CustomCollection#73 (1) {
protected $items =>
array(1) {
[0] =>
class Hell\Vephar\Resource#71 (4) {
public $name =>
null(0) null
public $email =>
string(12) "my@email.com"
}
}
}
#return for a single resource will be
class Hell\Vephar\CustomResource#74 (4) {
public $name =>
null(0) null
public $email =>
string(12) "my@email.com"
}
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email alexrili instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
alexrili/vephar 适用场景与选型建议
alexrili/vephar 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.78k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「alexrili」 「vephar」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alexrili/vephar 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alexrili/vephar 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-02-19