0

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, e.g. underscoreSeparatedKeys = false?

Ghostff
  • 1,407
  • 3
  • 18
  • 30
altralaser
  • 2,035
  • 5
  • 36
  • 55

1 Answers1

0

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