Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PASS_THRU extraction #149

Merged
merged 2 commits into from
Apr 22, 2014
Merged

Add PASS_THRU extraction #149

merged 2 commits into from
Apr 22, 2014

Conversation

bakura10
Copy link
Member

Hi,

This add another extraction strategy (I think we all have them now), that I called PASS_THRU. Basically, it allows a parent hydrator to decide how to extract child resources, and is a hint to a renderer to use this representation.

For instance, let's say you have a User with a roles ManyToMany associations. If you use the "ID" strategy, you'll have a payload that looks like this:

{"username": "Toto", "roles": [4, 2]}

If you use "EMBED", it will use the hydrator attached to the Role entity, and payload will look like this:

{"username": "Toto", "roles": [{"id": 4, "name": "member"}, {"id":2, "name": "admin"]}

However for those cases, what we prefer is to have a simpler representation as array:

{"username": "Toto", "roles": ["member", "admin"]}

This PASS_THRU extraction policy is made for that: you can decide that for this specification association, it's up to the parent hydrator (in this case, the UserHydrator) to decide how to represent this nested resource. For instance, you can write a UserHydrator that builds itself how roles are represented:

class UserWithRoleHydrator extends ClassMethods
{
    public function extract($object)
    {
        $payload = parent::extract($object);
        $roles   = $object->getRoles();

        $payload['roles'] = []; // Clear automatic extraction from ClassMethods

        foreach ($roles as $role) {
            $payload['roles'][] = $role->getName();
        }

        return $payload;
    }
}

And here is how you can configure the association:

/**
 * @REST\Association(routable=false, extraction="PASS_THRU")
 */
protected $roles;

Let me know if you like the idea :)

ping @danizord @Ocramius

@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) when pulling 55943a6 on pass-thru-extraction into 335a424 on master.

@bakura10
Copy link
Member Author

Anyone? I'll merge that otherwise :).

@@ -39,8 +39,8 @@ Otherwise, this URI will returns a 404. By default, this is set to false for bet
works if `routable` is set to true.
* `extraction`: defines how an association is rendered by the resource renderer. This can take the values `NONE` (the
association is not rendered and completely removes from the payload), `EMBED` (the association is recursively extracted
using the bound hydrator in the associated resource metadata) and `ID` (only the identifier(s) is/are rendered). The
default value is `ID`.
using the bound hydrator in the associated resource metadata), `ID` (only the identifier(s) is/are rendered) and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unclear wording

@bakura10
Copy link
Member Author

Thanks.

bakura10 added a commit that referenced this pull request Apr 22, 2014
@bakura10 bakura10 merged commit 99f6e3d into master Apr 22, 2014
@bakura10 bakura10 deleted the pass-thru-extraction branch April 22, 2014 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants