/** * @param Mapping $mapping * @param string $className * * @throws MappingException */ protected static function setProperties(Mapping $mapping, string $className) { $mapping->setProperties(static::getClassProperties($className)); }
/** * */ public function testItWillSerializeToJsonApiAnArrayOfObjects() { $postArray = [new SimplePost(1, 'post title 1', 'post body 1', 4), new SimplePost(2, 'post title 2', 'post body 2', 5)]; $postMapping = new Mapping(SimplePost::class, '/post/{postId}', ['postId']); $postMapping->setProperties(['postId', 'title', 'body', 'authorId', 'comments']); $postMapping->setFilterKeys(['body', 'title']); $mapper = new Mapper(); $mapper->setClassMap([$postMapping->getClassName() => $postMapping]); $jsonApiJsonApiSerializer = new JsonApiTransformer($mapper); $expected = <<<JSON { "data":[ { "type":"post", "id":"1", "attributes":{ "title":"post title 1", "body":"post body 1" }, "links":{ "self":{ "href":"/post/1" } } }, { "type":"post", "id":"2", "attributes":{ "title":"post title 2", "body":"post body 2" }, "links":{ "self":{ "href":"/post/2" } } } ], "jsonapi":{ "version":"1.0" } } JSON; $this->assertEquals(\json_decode($expected, true), \json_decode((new JsonApiSerializer($jsonApiJsonApiSerializer))->serialize($postArray), true)); }