![](/img/trans.png)
[英]pass parameter from pipeline middleware to factory class in zend expressive 2
[英]Pass options to ClassMethodsHydrator in zend-expressive-hal config
我使用 zend-expressive-hal (v3) 并为交付我的用户 class 编写了以下配置:
return [
[
'__class__' => RouteBasedResourceMetadata::class,
'resource_class' => Handler\User::class,
'route' => 'users',
'extractor' => ClassMethodsHydrator::class,
],
];
这工作没有任何问题。 然而,我注意到的是,密钥存储在生成的 JSON 中并带有下划线,而在我的用户 class 中,这些方法是用驼峰写成的。 如何补充我的上述配置以将选项传递给 ClassMethodsHydrator class,例如 underscoreSeparatedKeys = false?
显然,我没有使用最新版本的 zend hydrator,所以我没有 Zend\Hydrator\ClassMethodsHydrator class。 我构建了自己的水合器(我确定对象的每个属性都有 getter 和 setter):
class ObjectWithGetterAndSetterHydrator extends AbstractHydrator
{
public function extract($object)
{
if (!$object instanceof ApiEntityInterface) {
throw new \RuntimeException('Could not extract object. Object must be instance of ' . ApiEntityInterface::class);
}
/** @var ApiEntityInterface $object */
$properties = $object::getExportableProperties();
$data = [];
foreach ($properties as $property) {
$data[$property] = method_exists($object, 'get' . ucfirst($property)) ? $object->{'get' . ucfirst($property)}() : $object->{'is' . ucfirst($property)}();
}
return $data;
}
public function hydrate(array $data, $object)
{
foreach ($data as $key => $value) {
$object->{'set' . ucfirst($key)}($value);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.