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

Support for SkipStepException and handling class inheritance #175

Closed
wants to merge 18 commits into from

Conversation

shiroyuki
Copy link

(This PR is based on #174.)

This PR is to add the support to allow the handler to tell the graph navigator to skip itself and proceed with the normal procedure. For instance,

use PhpOption\None;

class MyHandler implements SubscribingHandlerInterface
{
    public function serializeProxy(JsonSerializationVisitor $visitor, $entity, array $type, Context $context)
    {
        if ($visitor->getRoot() === null) {
            throw new SkipStepException('Skip This');
        }

        return array(/* ... */);
    }
}

Also, this PR supports the class inheritance such that we can specify a parent class in getSubscribingMethods. For instance,

class MyHandler implements SubscribingHandlerInterface
{
    public static function getSubscribingMethods()
    {
        return array(
            array(
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
                'format' => 'json',
                'type' => 'EntityParentClassOrInterface',
                'method' => 'serializeProxy',
            ),
        );
    }

    public function serializeProxy(JsonSerializationVisitor $visitor, $entity, array $type, Context $context)
    {
        /* ... */
    }
}

cabello and others added 18 commits January 4, 2013 16:41
Conflicts:
	src/JMS/Serializer/GraphNavigator.php
…rent handlers if the known handler is not found for the particulartype
Once a handler throws a SkipStepException, this handler will be ignored and the GraphNavigation will try to reach an another one.
Ex: if the user implements a JSON Handler to some special operation, and want to disable in somepoint, just need to throw a SkipStepException and the GraphNavigator will reach the default JSON handler

use \PhpOption\None;

class IcJsonHandler implements SubscribingHandlerInterface
{
    public static function getSubscribingMethods()
    {
        return array(
            array(
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
                'format' => 'json',
                'type' => 'My\Interface',
                'method' => 'serializeProxy',
            ),
        );
    }

    public function serializeProxy(JsonSerializationVisitor $visitor, $entity, array $type, Context $context)
    {
        $skipHandler = $context->attributes->get('my_special_skip_attribute');

        if ($visitor->getRoot() === null || ($skipHandler instanceof None === false) {
            throw new SkipStepException('Skip This');
        }

        return array(/*MySpecial Operation*/);
    }
}
@schmittjoh
Copy link
Owner

Could you rebase this feature on master and exclude the commits related to the circular reference event?

You can do this via git rebase -i origin/master.

@shiroyuki
Copy link
Author

Rebasing is impossible. This is what I have:

error: could not apply f8acf1c... Updated the lazy handler registry to seek for the first-discovered parent handlers if the known handler is not found for the particulartype

I'm going to resubmit the PR in 20 minutes.

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.

5 participants