neomerx / json-api

Framework agnostic JSON API (jsonapi.org) implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Creating Link objects in SchemaProvider classes

luniki opened this issue · comments

My SchemaProvider class "PostingSchema" returns its relationships like this:

    public function getRelationships($posting, $isPrimary, array $includeList)
    {
        $user = $posting->getUser();

        return [
            'author' => [
                self::LINKS => [
                    Link::RELATED => new Link('/users/'.$user)
                ]
            ]
        ];
    }

Is there a way to call the getSelfSubLink method of the $user's schema class instead? Otherwise my code becomes very brittle as changing the UserSchema::resourceType would result in a broken link of the "author" relationship in the Posting.

As far as I understand you would be happy if the following was possible

    public function getRelationships($posting, $isPrimary, array $includeList)
    {
        $user = $posting->getUser();

        return [
            'author' => [
                self::LINKS => [
                    Link::RELATED => $this->getSchemaContainer()->getSchemaByType(User:class)->getSelfSubLink($user),
                ]
            ]
        ];
    }

So the question is 'How to add getSchemaContainer method?'

I can see it as following

  • Have CustomSchemaProvider that adds get/set for Schema Container and all your schemas extend from this class.
  • Have custom Schema Container that knows it works only with CustomSchemaProvider instances so it can override createSchemaFromCallable and createSchemaFromClassName methods and set $this as a container.
  • The last step make encoder to use that custom container. An example of that could be found in Tests/Extensions/Issue154 folder.

I'm sorry it would take some code to inject custom provider and provider container though you will be using documented and tested way of extending the library.

Don't hesitate to ask any questions if you need some help with implementation.

Related wiki

I've added it as an idea for next version of the lib https://github.com/neomerx/json-api/projects/1

@luniki I haven't heard any feedback from you for some time and have a feeling your question has been answered. If you need any further assistance please don't hesitate to contact.

I will try your proposed solution and get back to you if there are more obstacles ;-)