zfcampus/zf-hal — pull request #21 by EvanDotPro (2014-02-11 21:14:27+00:00)
Title: Add support for JsonSerializable resources.

Since Apigility ultimately serializes things to json, one would expect
when entities/resources implement JsonSerializable, that it would be
used. This was not the case because Zf\Hal\Plugin\Hal was converting
everything to an array first before encoding to json. Now, if an
entity/resource is JsonSerializable, it will prefer the result of
jsonSerialize() rather than falling back to a given hydrator or casting
the object to an array.

It might be desireable to override or toggle this behavior, in the event
a user wants to have a JsonSerializable entity, but still have Apigility
use a specified hydrator. If anyone complains about this, we could add
an option for toggling the order, or break up getHydratorForResource()
so that we can:

1) Check if a specific hydrator is set for that entity/resource.
2) If not, check if it's JsonSerializable.
3) If it's not, check if we have a default/fallback hydrator set.
4) If none of the above work, then finally cast to array.

That would provide the best of both worlds, but currently the
getHydratorForResource() method simply returns the specific hydrator if
specified, and quietly falls back to the default hydrator, so at the
convertResourceToArray() scope, we can't run logic to do something in
one case or the other.