简体   繁体   中英

Pass options to ClassMethodsHydrator in zend-expressive-hal config

I use zend-expressive-hal (v3) and have the following config written for the delivery of my User class:

return [
    [
        '__class__' => RouteBasedResourceMetadata::class,
        'resource_class' => Handler\User::class,
        'route' => 'users',
        'extractor' => ClassMethodsHydrator::class,
    ],
];

This works without any problems. What I have noticed, however, is that the keys are stored in the generated JSON with underscores, while in my User class the methods are written camel case. How can I supplement my above configuration to pass options to the ClassMethodsHydrator class, eg underscoreSeparatedKeys = false?

Aparently, I am not using the latest version of zend hydrator, so I don't have the Zend\Hydrator\ClassMethodsHydrator class. I built my own hydrator (I know for sure that the objects have getters and setters for each property):

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM