tests/Http/Query/BaseQueryParserTest.php
/**
     * Shows how to integrate base query parser with EncodingParameters.
     *
     * @return void
     *
     * @see https://github.com/neomerx/json-api/issues/198
     */
Shows how to integrate base query parser with EncodingParameters.
@return void


Shows how to integrate base query parser with EncodingParameters . @return void
public function testIntegrationWithEncodingParameters(): void
    {
        $profileUrl1 = 'http:example1.com/foo';
        $profileUrl2 = 'http:example2.com/boo';
        $queryParameters = [
            BaseQueryParser::PARAM_FIELDS  => [
                'comments' => Comment::LINK_AUTHOR . ',     ' . Comment::ATTRIBUTE_BODY . '      ',
                'people'   => Author::ATTRIBUTE_FIRST_NAME,
            ],
            BaseQueryParser::PARAM_SORT    => '-created,title,+updated',
            BaseQueryParser::PARAM_INCLUDE => Comment::LINK_AUTHOR . ',   ' .
                Comment::LINK_AUTHOR . '.' . Author::LINK_COMMENTS,
            BaseQueryParser::PARAM_PROFILE => urlencode(implode(' ', [$profileUrl1, $profileUrl2])),
        ];
        $parser = new class ($queryParameters) extends BaseQueryParser
        {
            private $fields = null;
            private $sorts = null;
            private $includes = null;
            private $profile = null;
            public function getFields(): array
            {
                if ($this->fields === null) {
                    $this->fields = $this->iterableToArray(parent::getFields());
                }
                return $this->fields;
            }
            public function getSorts(): array
            {
                if ($this->sorts === null) {
                    $this->sorts = $this->iterableToArray(parent::getSorts());
                }
                return $this->sorts;
            }
            public function getProfileUrls(): array
            {
                if ($this->profile === null) {
                    $this->profile = $this->iterableToArray(parent::getProfileUrls());
                }
                return $this->profile;
            }
            public function getIncludes(): array
            {
                if ($this->includes === null) {
                    $this->includes = array_keys($this->iterableToArray(parent::getIncludes()));
                }
                return $this->includes;
            }
            private function iterableToArray(iterable $iterable): array
            {
                $result = [];
                foreach ($iterable as $key => $value) {
                    $result[$key] = $value instanceof Generator ? $this->iterableToArray($value) : $value;
                }
                return $result;
            }
        };
        self::assertSame(
            [
                'comments' => [Comment::LINK_AUTHOR, Comment::ATTRIBUTE_BODY],
                'people'   => [Author::ATTRIBUTE_FIRST_NAME],
            ],
            $parser->getFields()
        );
        self::assertSame(
            [
                'created' => false,
                'title'   => true,
                'updated' => true,
            ],
            $parser->getSorts()
        );
        self::assertSame(
            [
                Comment::LINK_AUTHOR,
                Comment::LINK_AUTHOR . '.' . Author::LINK_COMMENTS,
            ],
            $parser->getIncludes()
        );
        self::assertSame(
            [
                $profileUrl1,
                $profileUrl2,
            ],
            $parser->getProfileUrls()
        );
         Now the main purpose of the test. Will it work with EncodingParameters?
         firstly setup some data
        $author                          = Author::instance(9, 'Dan', 'Gebhardt');
        $comments                        = [
            Comment::instance(5, 'First!', $author),
            Comment::instance(12, 'I like XML better', $author),
        ];
        $author->{Author::LINK_COMMENTS} = $comments;
         and encode with params taken from the parser
        $actual = Encoder::instance(
            [
                Author::class  => AuthorSchema::class,
                Comment::class => CommentSchema::class,
            ]
        )
            ->withIncludedPaths($parser->getIncludes())
            ->withFieldSets($parser->getFields())
            ->encodeData($comments);
        $expected = <<<EOL
        {
          "data": [
            {
              "type": "comments",
              "id": "5",
              "attributes": {
                "body": "First!"
              },
              "relationships": {
                "author": {
                  "links": {
                    "self"    : "/comments/5/relationships/author",
                    "related" : "/comments/5/author"
                  },
                  "data": { "type": "people", "id": "9" }
                }
              },
              "links": {
                "self": "/comments/5"
              }
            },
            {
              "type": "comments",
              "id": "12",
              "attributes": {
                "body": "I like XML better"
              },
              "relationships": {
                "author": {
                  "links": {
                    "self"    : "/comments/12/relationships/author",
                    "related" : "/comments/12/author"
                  },
                  "data": { "type": "people", "id": "9" }
                }
              },
              "links": {
                "self": "/comments/12"
              }
            }
          ],
          "included": [
            {
              "type": "people",
              "id": "9",
              "attributes":{
                "first_name":"Dan"
            },
            "links": {
                "self": "/people/9"
              }
            }
          ]
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }
public function testIntegrationWithEncodingParameters ( ) : void { $ profileUrl1 = 'http://example1.com/foo' ; $ profileUrl2 = 'http://example2.com/boo' ; $ queryParameters = [ BaseQueryParser :: PARAM_FIELDS => [ 'comments' => Comment :: LINK_AUTHOR . ',     ' . Comment :: ATTRIBUTE_BODY . '      ' , 'people' => Author :: ATTRIBUTE_FIRST_NAME , ] , BaseQueryParser :: PARAM_SORT => '-created,title,+updated' , BaseQueryParser :: PARAM_INCLUDE => Comment :: LINK_AUTHOR . ',   ' . Comment :: LINK_AUTHOR . '.' . Author :: LINK_COMMENTS , BaseQueryParser :: PARAM_PROFILE => urlencode ( implode ( ' ' , [ $ profileUrl1 , $ profileUrl2 ] ) ) , ] ; $ parser = new class ( $ queryParameters ) extends BaseQueryParser { private $ fields = null ; private $ sorts = null ; private $ includes = null ; private $ profile = null ; public function getFields ( ) : array { if ( $ this -> fields === null ) { $ this -> fields = $ this -> iterableToArray ( parent :: getFields ( ) ) ; } return $ this -> fields ; } public function getSorts ( ) : array { if ( $ this -> sorts === null ) { $ this -> sorts = $ this -> iterableToArray ( parent :: getSorts ( ) ) ; } return $ this -> sorts ; } public function getProfileUrls ( ) : array { if ( $ this -> profile === null ) { $ this -> profile = $ this -> iterableToArray ( parent :: getProfileUrls ( ) ) ; } return $ this -> profile ; } public function getIncludes ( ) : array { if ( $ this -> includes === null ) { $ this -> includes = array_keys ( $ this -> iterableToArray ( parent :: getIncludes ( ) ) ) ; } return $ this -> includes ; } private function iterableToArray ( iterable $ iterable ) : array { $ result = [ ] ; foreach ( $ iterable as $ key => $ value ) { $ result [ $ key ] = $ value instanceof Generator ? $ this -> iterableToArray ( $ value ) : $ value ; } return $ result ; } } ; self :: assertSame ( [ 'comments' => [ Comment :: LINK_AUTHOR , Comment :: ATTRIBUTE_BODY ] , 'people' => [ Author :: ATTRIBUTE_FIRST_NAME ] , ] , $ parser -> getFields ( ) ) ; self :: assertSame ( [ 'created' => false , 'title' => true , 'updated' => true , ] , $ parser -> getSorts ( ) ) ; self :: assertSame ( [ Comment :: LINK_AUTHOR , Comment :: LINK_AUTHOR . '.' . Author :: LINK_COMMENTS , ] , $ parser -> getIncludes ( ) ) ; self :: assertSame ( [ $ profileUrl1 , $ profileUrl2 , ] , $ parser -> getProfileUrls ( ) ) ; $ author = Author :: instance ( 9 , 'Dan' , 'Gebhardt' ) ; $ comments = [ Comment :: instance ( 5 , 'First!' , $ author ) , Comment :: instance ( 12 , 'I like XML better' , $ author ) , ] ; $ author -> { Author :: LINK_COMMENTS } = $ comments ; $ actual = Encoder :: instance ( [ Author :: class => AuthorSchema :: class , Comment :: class => CommentSchema :: class , ] ) -> withIncludedPaths ( $ parser -> getIncludes ( ) ) -> withFieldSets ( $ parser -> getFields ( ) ) -> encodeData ( $ comments ) ; $ expected = <<< EOL         {           "data": [             {               "type": "comments",               "id": "5",               "attributes": {                 "body": "First!"               },               "relationships": {                 "author": {                   "links": {                     "self"    : "/comments/5/relationships/author",                     "related" : "/comments/5/author"                   },                   "data": { "type": "people", "id": "9" }                 }               },               "links": {                 "self": "/comments/5"               }             },             {               "type": "comments",               "id": "12",               "attributes": {                 "body": "I like XML better"               },               "relationships": {                 "author": {                   "links": {                     "self"    : "/comments/12/relationships/author",                     "related" : "/comments/12/author"                   },                   "data": { "type": "people", "id": "9" }                 }               },               "links": {                 "self": "/comments/12"               }             }           ],           "included": [             {               "type": "people",               "id": "9",               "attributes":{                 "first_name":"Dan"             },             "links": {                 "self": "/people/9"               }             }           ]         } 
EOL ; self :: assertJsonStringEqualsJsonString ( $ expected , $ actual ) ; }
Shows how to integrate base query parser with EncodingParameters.
Shows how to integrate base query parser with EncodingParameters .
// It is expected that classes that encapsulate/extend BaseQueryParser would add features // such filters/pagination parsing, validation, etc. Though for simplicity we omit adding // them here and check how it integrates with EncodingParameters. /**
             * @var null|array
             */ /**
             * @var null|array
             */ /**
             * @var null|array
             */ /**
             * @var null|array
             */ /**
             * @return array
             */ /**
             * @return array
             */ /**
             * @return array
             */ /**
             * @return array
             */ /**
             * @param iterable $iterable
             *
             * @return array
             */ // Check parsing works fine // // Now the main purpose of the test. Will it work with EncodingParameters?
 // // firstly setup some data // and encode with params taken from the parser