lgse / php-object-builder
PHP 7.1+ 对象构建器
v1.9
2019-11-07 18:47 UTC
Requires
- php: >=7.1
Requires (Dev)
- mockery/mockery: ^0.9.8
- phpunit/phpunit: ^7.0
README
https://github.com/lgse/php-object-builder
PHP 7+ 递归对象构建器
关于
PHP 对象构建器 通过传递值数组,递归验证并实例化所有复杂的对象。
特性
- 验证参数类型
- 按正确顺序将参数传递给构造函数
- 实例化参数类
- 闪电般快速
- 易于使用
安装
composer require php-object-builder
使用方法
use PHPOB\Model; use PHPOB\ObjectBuilder; /** * Example Object Class To Instantiate * Extending our `Model` class will add the `getInstance` static method * to your object so you don't have to create an object builder every * time you want to instantiate a class. */ class Customer extends Model { public function __construct( int $id, string $name, Address $address ) { $this->id = $id; $this->name = $name; $this->address = $address; } ... } class Address { public function __construct( string $street, string $city, string $state, int $zip ) { $this->street = $street; $this->city = $city; $this->state = $state; $this->zip = $zip; } ... } /** * Example instantiation using the `getInstance` static method */ $customer = Customer::getInstance([ 'id' => '0e2c0f21-2c46-4cf9-ad7e-2beeadb9282b', 'name' => 'Microsoft', 'address' => [ 'street' => '1 Microsoft Way', 'city' => 'Redmond', 'state' => 'WA', 'zip' => 98052, ] ]); /** * Example instantiation using the object builder * Note: You can pass in instantiated parameters that will be automatically passed through * to the object's constructor */ $builder = new ObjectBuilder(Customer::class); $customer = $builder->getObject([ 'id' => '0e2c0f21-2c46-4cf9-ad7e-2beeadb9282b', 'name' => 'Microsoft', 'address' => new Address([ 'street' => '1 Microsoft Way', 'city' => 'Redmond', 'state' => 'WA', 'zip' => 98052, ]) ]);
API
模型可用方法
static getInstance(array|object $arguments)
自动创建构建器对象并返回扩展类的实例。
ObjectBuilder 可用方法
__construct(string $className)
为提供的类名实例化构建器对象。getObject(array $arguments)
使用参数数组返回实例化的对象。