serlo-org / athene2-hydrator
此包已被弃用,不再维护。未建议替代包。
Zend Framework 2 模块,为 Athene2 提供一个 hydrator
dev-master
2014-08-10 17:41 UTC
Requires
- php: >=5.4
- doctrine/common: ~2.4
- doctrine/doctrine-module: 0.*
- zendframework/zend-servicemanager: ~2.3
- zendframework/zend-stdlib: ~2.3
Requires (Dev)
- phpunit/phpunit: ~3.7
- satooshi/php-coveralls: ~0.6
- squizlabs/php_codesniffer: 1.4.*
- zendframework/zendframework: ~2.2
This package is not auto-updated.
Last update: 2020-01-20 08:56:39 UTC
README
注意,此模块尚未完全测试
安装
athene2-versioning 仅通过 Composer 官方支持安装。有关 Composer 文档,请参阅 getcomposer.org。
安装模块
$ php composer.phar require serlo-org/athene2-hydrator:dev-master
hydrators?
此 Zend Framework 2 模块旨在为您提供带有 ZF2 和 Doctrine 模块的 hydrators 的先进 PluginAware Hydrators。以下 hydrators 已修改以用于插件
- DoctrineObject
ArraySerializableClassMethodsObjectPropertyReflection
插件?
对象水合有时可能变得困难。某些水合步骤可能需要高级逻辑。让我们以一个现实世界的例子来说明
鲍勃更新了网页上音乐曲目条目中艺术家的名字。新的艺术家名字不在数据库中,需要先创建然后与曲目条目相关联。插件可以轻松完成此操作
public function hydrate(array $data, $object) { $data['artist'] = $this->myArtistService->createIfNotExists($data['artist']); return $data; }
用法
要创建一个新的插件,只需实现 Athene2\Hydrator\Plugin\HydratorPluginInterface。要将该插件添加到 HydratorPluginManager,请将以下内容添加到您的 module.config.php
return [ 'hydrator_plugins' => [ 'invokables' => [ 'myPlugin' => 'MyNamespace\Hydrator\Plugin\MyPlugin' ] ] ];
HydratorPluginManager 是 ZF2 PluginManager,因此您还可以使用 factory 和 alias 键。
现在让我们创建一个 hydrator!
$pluginManager = $serviceManager->get('Athene2\Hydrator\Plugin\HydratorPluginManager'); $hydrator = new PluginAwareClassMethods($pluginManager); // Let's add the plugin 'myPlugin' to this hydrator $hydrator->addPlugin('myPlugin'); // The plugin will be called before hydration takes place. // What you do in the plugin does not matter. You can inject stuff or just modify the hydration data. // Be aware however, that every key value pair you return will be hydrated by the ClassMethod hydrator. // Make sure to remove key value pairs, which should be ignored by ClassMethod (or any other hydrator for that matter). $hydrator->hydrate($myObject, $myData); // The plugin will be called after extraction takes place. $data = $hydrator->extract($myObject);