Options

The $options argument of Hydrator::hydrate() can contain:

  • parent object: On a nested call, this is a reference to the object containing the current object. This allows the application to link the current object to the parent if required.
  • source string: The form of the data in the $config argument. If missing, "json" is assumed. Accepted values are json, object, and yaml. If the option is "object" then the $config argument will be treated as the result of decoding the source data.
  • strict bool: Throw errors on any failure, otherwise log for getErrors(). Defaults to true.

The application can pass any additional options by prefixing the index with an underscore. These will be passed through unchanged.

Using $options['parent]' and a custom option:

class A implements Hydratable
{
    private $customData;
    private ?B $parent = null;

    private static Hydrator $hydrator;

    public function hydrate($json, $options = []): bool
    {
        $this->parent = $options['parent'] ?? null;
        $this->customData = $options['_custom'] ?? null;
        return self::$hydrator->hydrate($this, $json, $options);
    }

}

Options and Nested Classes

Classes that only have their hydration method called by a parent class can assume that their $config argument has been decoded.

Classes that are both accessed directly and from another class without transforming the data can simply let Hydrator::hydrate() decide what to do. However, if the class is expanding a simplified form of the data it is advisable to ensure it has been decoded by first calling the Hydrator::decode() method. See simplification for an example of this.

Option Access in Properties

Most closures defined in a Property will receive a reference to the Property as an argument. Applications can retrieve the option context by calling the Property::getOptions() method.