czim/laravel-dataobject 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

czim/laravel-dataobject

Composer 安装命令:

composer require czim/laravel-dataobject

包简介

Basic validatable standardized data object.

README 文档

README

Latest Version on Packagist Software License Build Status Coverage Status Latest Stable Version SensioLabsInsight

Basic framework for making standardized but flexible data objects. This provides a class (and interfaces) to use to standardize your dependencies or return values with a generic dataobject.

All this really is, is a data storage class with magic getters and setters. It is Arrayable, Jsonable and validatable.

Also provided are the means to make nested validation possible for data objects (containing futher data objects).

Version Compatibility

Laravel Package PHP
5.1 1.0
5.2 1.0
5.3 1.3
5.4 1.4
5.5 and up 1.4, 2.0
9 and up 3.0 8.0 and up

Install

Via Composer

$ composer require czim/laravel-dataobject

Usage

Simply create your own extension of the base dataobject, and:

Optionally add your own getters and setters

Basic stuff of course, but if you want your IDE to know what your objects contain, you can simply write getters and setters like this:

class TestDataObject extends \Czim\DataObject\AbstractDataObject
{
    public function getName($value)
    {
        $this->getAttribute('name');
    }

    public function setName($value)
    {
        $this->setAttribute('name', $value);
    }
}

Additionally, if you want to block any type of magic assignment (meaning all assignment would have to be done through the setAttribute() or setAttributes() methods, or your own setters), you can disable magic assignment as follows:

class TestDataObject extends \Czim\DataObject\AbstractDataObject
{
    protected $magicAssignment = false;

    ...

Attempts to set attributes on the DataObject by magic or array access will then throw an UnassignableAttributeException.

Optionally add validation rules for attributes

class YourDataObject extends \Czim\DataObject\AbstractDataObject
{
    protected $rules = [
        'name' => 'required|string',
        'list' => 'array|min:1',
    ];
}

Validating the data can be done as follows:

    $dataObject = new YourDataObject();

    // validate() returns a boolean, false if it does not follow the rules
    if ( ! $dataObject->validate()) {

        $messages = $dataObject->messages();

        // messages() returns a standard Laravel MessageBag
        dd( $messages->first() );
    }

Messages are a MessageBag object generated by the Validator (whatever is behind the Laravel Facade Validator).

Validation

To use the extra Validation features for nested DataObject validation rules and better array validation, load the ServiceProvider for this package. This is the only reason to load the ServiceProvider; this package does not itself require the Provider to function.

Add this line of code to the providers array located in your config/app.php file:

    Czim\DataObject\DataObjectServiceProvider::class,

Note that this will rebind the Validator facade, so if you have done this yourself, you may instead want to use the provided validation Traits to add to your own extended validator class.

Read more information about the validation (traits) here.

Castable Data Object

You may opt to extend the Czim\DataObject\CastableDataObject. Besides the standard features, this also includes the possibility of casting its properties to scalar values or (nested) data objects. This works similarly to Eloquent's $casts property (with some minor differences).

By overriding a protected casts() method, it is possible to set a cast type per attribute key:

<?php
protected function casts()
{
    return [
        'check' => 'boolean',
        'count' => 'integer',
        'price' => 'float',
    ];
}

This has the effect of casting each property to its set type before returning it.

<?php
$object = new YourDataObject([
    'check' => 'truthy value',
    'price' => 45,
]);

$object->check;     // true
$object['price'];   // 45.0
$object->count;     // 0 (instead of null)

toArray Casting

Cast types are also applied for the toArray() method of the data object. This means that unset properties will be present in the array as their default value (false for boolean, 0.0 for float, etc.).

To disable this behaviour, set $castToArray to false. This will entirely disable casting values on toArray().

Nested Object Casting

More useful than scalar-casting, is object casting. This will allow you to create a tree of nested objects that, if set, can be invoked fluently.

Given casts that are set as follows:

<?php
class RootDataObject extends \Czim\DataObject\CastableDataObject
{
    protected function casts()
    {
        return [
            'some_object' => YourDataObject::class,
            'object_list' => YourDataObject::class . '[]',
        ];
    }
}

And with the following data example, you can access the data by property:

<?php
$data = [
    'some_object' => [
        'type' => 'peaches',
    ],
    'object_list' => [
        ['type' => 'cucumbers'],
        ['type' => 'sherry'],
    ],
];

$object = new RootDataobject($data);

$object->some_object;           // instance of YourDataObject
$object->some_object->type;     // peaches
$object->object_list[1]->type;  // sherry

Note that unset or null values will return null, not an empty data object. Non-array values will cause exceptions to be thrown on being interpreted as data objects.

This behaviour can be changed by setting the $castUnsetObjects property to true: unset attributes with an object cast will then be cast as an empty instance of that object class.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

czim/laravel-dataobject 适用场景与选型建议

czim/laravel-dataobject 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 143.33k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2015 年 09 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「validation」 「data object」 「validate」 「standardization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 czim/laravel-dataobject 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 czim/laravel-dataobject 我们能提供哪些服务?
定制开发 / 二次开发

基于 czim/laravel-dataobject 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 143.33k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 21
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-26