tests/Encoder/EncoderTest.php
/**
     * Test encode Traversable (through Iterator) collection of resource(s).
     */
Test encode Traversable (through Iterator) collection of resource(s).
Test encode Traversable ( through Iterator ) collection of resource ( s ) .
public function testEncodeTraversableObjectsWithAttributesOnly(): void
    {
        $author  = Author::instance(9, 'Dan', 'Gebhardt');
        $encoder = Encoder::instance(
            [
                Author::class  => function ($factory) {
                    $schema = new AuthorSchema($factory);
                    $schema->hideDefaultLinksInRelationship(Author::LINK_COMMENTS);
                    return $schema;
                },
                Comment::class => CommentSchema::class,
            ]
        )->withUrlPrefix('http://example.com');
        $author->{Author::LINK_COMMENTS} = new ArrayIterator(
            [
                'comment1' => Comment::instance(5, 'First!'),
                'comment2' => Comment::instance(12, 'I like XML better'),
            ]
        );
        $itemSet = new ArrayIterator(['what_if_its_not_zero_based_array' => $author]);
        $actual  = $encoder->encodeData($itemSet);
        $expected = <<<EOL
        {
            "data" : [
                {
                    "type"       : "people",
                    "id"         : "9",
                    "attributes" : {
                        "first_name" : "Dan",
                        "last_name"  : "Gebhardt"
                    },
                    "relationships" : {
                        "comments" : {
                            "data" : [
                                { "type":"comments", "id":"5" },
                                { "type":"comments", "id":"12" }
                            ]
                        }
                    },
                    "links" : {
                        "self" : "http://example.com/people/9"
                    }
                }
            ]
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }
public function testEncodeTraversableObjectsWithAttributesOnly ( ) : void { $ author = Author :: instance ( 9 , 'Dan' , 'Gebhardt' ) ; $ encoder = Encoder :: instance ( [ Author :: class => function ( $ factory ) { $ schema = new AuthorSchema ( $ factory ) ; $ schema -> hideDefaultLinksInRelationship ( Author :: LINK_COMMENTS ) ; return $ schema ; } , Comment :: class => CommentSchema :: class , ] ) -> withUrlPrefix ( 'http://example.com' ) ; $ author -> { Author :: LINK_COMMENTS } = new ArrayIterator ( [ 'comment1' => Comment :: instance ( 5 , 'First!' ) , 'comment2' => Comment :: instance ( 12 , 'I like XML better' ) , ] ) ; $ itemSet = new ArrayIterator ( [ 'what_if_its_not_zero_based_array' => $ author ] ) ; $ actual = $ encoder -> encodeData ( $ itemSet ) ; $ expected = <<< EOL         {             "data" : [                 {                     "type"       : "people",                     "id"         : "9",                     "attributes" : {                         "first_name" : "Dan",                         "last_name"  : "Gebhardt"                     },                     "relationships" : {                         "comments" : {                             "data" : [                                 { "type":"comments", "id":"5" },                                 { "type":"comments", "id":"12" }                             ]                         }                     },                     "links" : {                         "self" : "http://example.com/people/9"                     }                 }             ]         } 
EOL ; self :: assertJsonStringEqualsJsonString ( $ expected , $ actual ) ; }
Test encode Traversable (through Iterator) collection of resource(s).
Test encode Traversable ( through Iterator ) collection of resource ( s ) .
// iterator here // and iterator here