定制 rkgrep/attributable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

rkgrep/attributable

最新稳定版本:v1.1

Composer 安装命令:

composer require rkgrep/attributable

包简介

Fast an elegant way to make dynamic objects

README 文档

README

Build Status Latest Stable Version Latest Unstable Version License SensioLabsInsight

Note: Original idea by Taylor Otwell in Laravel Framework.

The package includes traits which allow fluent and elegant way to work with internal object property arrays.

Installation

Install the package with composer.

composer require rkgrep/attributable

Apply the trait to any class you need.

class Foo {
    
    use rkgrep\Attributable;
    
}
class Bar {
    
    use rkgrep\Fillable;
    
}

Usage

Attributable trait

Attributable provides different access and assignment ways.

Assign internal variables via property or method call

$foo->var1 = 1;
$foo->var2(2);

Access variables via property

echo $foo->var1;
echo $foo->var2;

Provide fallback value via get method

$foo->get('var4', 'fallback');
$foo->get('var5', function() { return 'closure result'; });

Get all internal variables via getAttributes method

$all = $foo->getAttributes();

Method call without arguments assigns true

$foo->viewable();
true == $foo->viewable;

Chain methods for fast assignment

$user->first_name('John')->last_name('Doe')->admin();

Fillable trait

Fillable provides chaining assignment of variables or groups of variables.

Mass assign atributes with fill method

$user->fill(['name' => 'Admin', 'email' => 'admin@example.com']);

Overwrite or reassign control via second parameter

$user->fill(['name' => 'John Doe']); // Name changed, email remains untouched
$user->fill(['email' => 'other@example.com'], false); // Disabled merging - old values are dropped

Fill specific properties with with method

$user->with('password', md5('password'));

Prevent overriding with third parameter

$user->with('password', '', false); // Password remains untouched

Assign multiple variables

$user->with(['friends' => ['Mike', 'Dave'], 'girlfriend' => 'Jane']);
$user->with(['siblings' => [], 'girlfriend' => 'Mary'], null, false); // Overriding disabled - only siblings are touched

Chain method calls

$post->fill(['title' => 'Lorem Ipsum'])->with('views', 5)->with('likes', 3);

Interfaces

Any class with Attributable trait applied implements ArrayAccess and JsonSerializable. If you use illuminate/support package you can also apply Arrayable and Jsonable interfaces.

class Bar implements ArrayAccess, JsonSerializable, Arrayable, Jsonable {

    use rkgrep\Attributable;

}

$bar = new Bar();
$bar->value('test');

$arrayValue = $bar['value'];
$bar['value'] = 'new';
$json = json_encode($bar); // returns '{"value": "new"}'

$array = $bar->toArray();
$json = $bar->toJson();

License

Attributable package is open-sourced package licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固