lexide/clay
Composer 安装命令:
composer require lexide/clay
包简介
Create lightweight models and apply complex data structures to them
关键字:
README 文档
README
Clay allows you to populate a model object with an array of data (from a database or other data store) without having to manually apply the values by hand. Data is applied to the model based on the name of each property, either directly or via setters if they are available. Clay supports nested array data and properties which contain other objects (in a multi layered data structure) as well as automatically converting keys to camel case when applying data to the model.
installation
Install via composer
composer require lexide/clay
Simple Example
To use Clay, a model should use the ModelTrait trait and implement a method which calls loadData()
class Model
{
use Lexide\Clay\Model\ModelTrait;
public $property;
...
public function __construct(array $data = [])
{
$this->loadData($data);
}
}
The model can then be instantiated, passing data to the constructor
$data = ["property" => "value", ...];
$model = new Model($data);
echo $model->property; // outputs "value"
Data with underscored or spaced keys will be applied to their camel case counterparts:
$data = [
"long_field_name" => ... // will apply to the property "longFieldName"
"property name with spaces" => ... // will apply to "propertyNameWithspaces"
];
Object instantiation
If you want a subset of the data to be contained within a second class, you simply need to type-hint the supplied argument in a setter
class Address
{
use Lexide\Clay\Model\ModelTrait;
public $street;
public $city;
public function __construct(array $data = [])
{
$this->loadData($data);
}
}
class Customer
{
use Lexide\Clay\Model\ModelTrait;
protected $address;
public function __construct(array $data = [])
{
$this->loadData($data);
}
public function setAddress(Address $address)
{
$this->address = $address;
}
}
$data = [
"address": [
"street" => "1 test street",
"city" => "exampleton"
]
]
$customer = new Customer($data);
echo $customer->getAddress()->city; // outputs "exampleton"
Object collections
The same can be done for an array or collection of objects. Clay looks for an "add" method for that property to determine the class to instantiate
class Customer
{
use Lexide\Clay\Model\ModelTrait;
protected $addresses = [];
public function __construct(array $data = [])
{
$this->loadData($data);
}
public function setAddresses(array $addresses)
{
$this->addresses = [];
foreach ($addresses as $address) {
$this->addAddresses($address));
}
}
public function addAddresses(Address $address)
{
$this->addresses[] = $address;
}
}
$data = [
"addresses": [
[
"street" => "1 Test street",
"city" => "Exampleton"
],
[
"street" => "22 Sample row",
"city" => "Testville"
]
]
]
$customer = new Customer($data);
lexide/clay 适用场景与选型建议
lexide/clay 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.12k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「lightweight」 「model」 「models」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lexide/clay 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lexide/clay 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lexide/clay 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Wordpress Query Builder class library for custom models and data querying.
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
A Laravel package for managing model drafts.
A package to better handle database operations via Eloquent Models.
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 13.12k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 6
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-16