承接 lexide/clay 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

lexide/clay

最新稳定版本:v3.1.2

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);

统计信息

  • 总下载量: 10.11k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-16