yin: Don't ignore attribute in hydrator if not present in the request

The Hydrator ignores attribute if they are not present in the request, the callable is never call.

This is annoying if an attribute is required for example. I think it should be great to execute all the callable and let the developer decide what it should do, ignore the attribute, set it to null, throw an exception, etc.

I made a commit on my repo https://github.com/lunika/yin/commit/d6955813bc7bc1839794ff445a75d222902e94d1

This is yet again a BC break. An example how ti use it :

    protected function getAttributeHydrator($user)
    {
        return [
            'firstname' => function ($user, $attributeValue, $data, $attribute, $isMissing) {
                if (true === $isMissing) {
                    throw new MissingMandatoryAttributeException($attribute);
                }

                $user['firstname'] = $attributeValue;

                return $user;
            },
            'lastname' => function ($user, $attributeValue, $data, $attribute, $isMissing) {
                if (false === $isMissing) {
                    $user['lastname'] = $attributeValue;
                }

                return $user;
            }
        ];
    }

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (13 by maintainers)

Most upvoted comments

They all got a notification about this conversation 😃 if someone was interested, they could have created an issue in the past one month. And I think we are good for this major release because it contains quite a few BC breaks and I don’t really want to have more.

All in all, I became impatient to release v2.0, so I’ll do it today.

Yes it’s a good idea to separate the validation and the hydration like @leclairmael suggest it, you can add more validation than simply test if a field is present or not. I’m testing this library to do that https://github.com/chanmix51/ParameterJuicer