![](/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.