touki / populator
简单的数组到对象库
Requires
- php: >=5.3.2
- doctrine/annotations: ~1.1.2
This package is auto-updated.
Last update: 2024-09-07 23:49:13 UTC
README
PHP Populator 是一个简单的数组到对象的库,通过注释进行少量扩展,将数组转换为指定对象。
这不是一个反序列化器。如果您正在寻找序列化器/反序列化器,我建议您使用 jms/serializer。
这个库的简单目标是使用轻量级(仍然是面向对象的)解决方案来从给定的数组中填充对象,而不需要使用完整的序列化器。
此外,行为可以轻松修改(解耦),并且已经进行了单元测试!
## 文档摘要
安装
使用 composer 安装
使用此库的最简单方法是通过 Composer。只需将以下行添加到您的 composer.json
文件中
{ "require": { "touki/populator": "~1.0.0" } }
然后运行
composer update
设置
基本设置
此库使用 PSR-0 自动加载机制。如果您使用 composer,则无需执行任何操作,类应该已经自动加载。
为了开始使用 populator,您只需要创建其实例。
<?php use Touki\Populator\Populator; $populator = new Populator; ?>
但是,如果您遇到错误
[语义错误] 注释 "..." 不存在,或无法自动加载。
您应该在应用程序顶部添加以下行。
这允许 doctrine 通过其自己的自动加载器了解如何导入您的注释。
<?php $loader = require '/path/to/app/vendor/autoload.php'; // Composer autoloader use Doctrine\Common\Annotations\AnnotationRegistry; AnnotationRegistry::registerLoader(array($loader, 'loadClass')); ?>
就这样!您现在可以开始使用库了。有关更多信息,请参阅用法。
高级设置
如果没有给出任何实例,populator 在其 构造函数 中会神奇地创建两个实例。
它需要一个 HydratorInterface
实例(将在给定的上下文中填充对象)和一个 HydratorContextFactoryInterface
实例(将根据给定对象的反射创建上下文)
为了重现默认行为,您可以这样做
<?php use Touki\Populator\Populator; use Touki\Populator\Hydrator; use Touki\Populator\HydratorContextFactory; use Doctrine\Common\Annotations\AnnotationReader; /** * You can instanciate your own as long as it implements HydratorInterface */ $hydrator = new Hydrator; /** * You can instanciate your own as long as it implements HydratorContextFactoryInterface * The default one accepts Any Doctrine's Annotation Reader (Like FileCacheReader) * * @see https://github.com/doctrine/annotations/tree/master/lib/Doctrine/Common/Annotations */ $factory = new HydratorContextFactory(new AnnotationReader); $populator = new Populator($hydrator, $factory); ?>
用法
简单用法
假设我们有一个 Foo
类
<?php namespace Acme\Model\Foo; class Foo { protected $bar; public $public; public $publicWithSetter; public function setBar($bar) { $this->bar = $bar; } public function getBar() { return $this->bar; } public function setPublicWithSetter($var) { $this->publicWithSetter = $var; } } $data = array( 'bar' => 'Foobaz!', 'public' => 'Public!' 'publicWithSetter' => 'BySetter' ); /** * You can give either classname or an instance */ $foo = new Acme\Model\Foo; $foo = 'Acme\Model\Foo'; $newFoo = $populator->populate($data, $foo); echo $newFoo->getBar(); // Foobaz! echo $newFoo->public; // Public! echo $newFoo->publicWithSetter; // BySetter ?>
注释
在以下示例中,我们假设每个受保护的属性都有其自己的设置器和获取器
@Populator\Ignore
此注释跳过属性的设置
<?php use Touki\Populator\Annotation as Populator; class Foo { /** * @Populator\Ignore */ protected $bar; } $data = array( 'bar' => 'Foobaz!' ); $newFoo = $populator->populate($data, $foo); echo $newFoo->getBar(); // NULL ?>
@Populator\Setter
此注释允许属性定义其自己的类的设置器
<?php use Touki\Populator\Annotation as Populator; class Foo { /** * @Populator\Setter("mySetter") */ protected $bar; public function mySetter($value) { $this->bar = $value; } } $data = array( 'bar' => 'Foobaz!' ); $newFoo = $populator->populate($data, $foo); echo $newFoo->getBar(); // Foobaz! ?>
@Populator\Alias
此注释添加一个别名以匹配属性
<?php use Touki\Populator\Annotation as Populator; class Foo { /** * @Populator\Alias("bar") * @Populator\Alias("another") */ protected $foo; public function setFoo($value) { $this->foo = $value; } } $data = array( 'bar' => 'Foobaz!' ); $newFoo = $populator->populate($data, $foo); echo $newFoo->getFoo(); // Foobaz! $data = array( 'another' => 'Foobaz!' ); $newFoo = $populator->populate($data, $foo); echo $newFoo->getFoo(); // Foobaz! ?>
@Populator\Aliases
此注释设置和替换别名
<?php use Touki\Populator\Annotation as Populator; class Foo { /** * @Populator\Aliases({"bar", "another"}) */ protected $foo; public function setFoo($value) { $this->foo = $value; } } $data = array( 'bar' => 'Foobaz!' ); $newFoo = $populator->populate($data, $foo); echo $newFoo->getFoo(); // Foobaz! ?>
### @Populator\Deep
此注释允许您拥有更深层级的对象
<?php use Touki\Populator\Annotation as Populator; class Foo { /** * @Populator\Deep("Bar") */ protected $bar; public function setBar(Bar $bar) { $this->bar = $bar; } } class Bar { protected $baz; public function setBaz($bar) { $this->baz = $baz; } } $data = array( 'bar' => array( 'baz' => 'DeepBaz!' ) ); $newFoo = $populator->populate($data, $foo); echo $newFoo->getBar()->getBaz(); // DeepBaz! ?>