我使用zend-expressive-hal(v3),并为交付User类编写了以下配置:
return [
[
'__class__' => RouteBasedResourceMetadata::class,
'resource_class' => Handler\User::class,
'route' => 'users',
'extractor' => ClassMethodsHydrator::class,
],
];
此方法没有任何问题。但是,我注意到的是,密钥存储在生成的带有下划线的JSON中,而在我的User类中,方法是驼峰写的。我如何补充我的上述配置以将选项传递给ClassMethodsHydrator类,例如underscoreSeparatedKeys = false?
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);
}
}
}