/** * @param array $mappedClass * * @throws MappingException * * @return Mapping */ public static function fromArray(array &$mappedClass) : Mapping { $className = static::getClass($mappedClass); $resourceUrl = static::getSelfUrl($mappedClass); $idProperties = static::getIdProperties($mappedClass); $mapping = new Mapping($className, $resourceUrl, $idProperties); $mapping->setClassAlias(empty($mappedClass[static::ALIAS_KEY]) ? $className : $mappedClass[static::ALIAS_KEY]); static::setAliasedProperties($mappedClass, $mapping, $className); static::setHideProperties($mappedClass, $mapping, $className); static::setRelationships($mappedClass, $mapping, $className); static::setCuries($mappedClass, $mapping); static::setProperties($mapping, $className); $otherUrls = static::getOtherUrls($mappedClass); if (!empty($otherUrls)) { $mapping->setUrls($otherUrls); } return $mapping; }
public function testTypeValueIsChangedByClassAlias() { $post = HelperFactory::simplePost(); $postMapping = new Mapping(SimplePost::class, '/post/{postId}', ['postId']); $postMapping->setClassAlias('Message'); $mapper = new Mapper(); $mapper->setClassMap([$postMapping->getClassName() => $postMapping]); $jsonApiJsonApiSerializer = new JsonApiTransformer($mapper); $expected = <<<JSON { "data": { "type": "message", "id": "1", "attributes": { "post_id": 1, "title": "post title", "body": "post body", "author_id": 2, "comments": [ { "comment_id": 10, "comment": "I am writing comment no. 1", "user_id": "User 5", "created_at": "2015-07-19T12:48:00+02:00" }, { "comment_id": 20, "comment": "I am writing comment no. 2", "user_id": "User 10", "created_at": "2015-07-20T12:48:00+02:00" }, { "comment_id": 30, "comment": "I am writing comment no. 3", "user_id": "User 15", "created_at": "2015-07-21T12:48:00+02:00" }, { "comment_id": 40, "comment": "I am writing comment no. 4", "user_id": "User 20", "created_at": "2015-07-22T12:48:00+02:00" }, { "comment_id": 50, "comment": "I am writing comment no. 5", "user_id": "User 25", "created_at": "2015-07-23T12:48:00+02:00" } ] }, "links": { "self": { "href": "/post/1"} } }, "links": { "self": { "href": "/post/1" } }, "jsonapi": { "version": "1.0" } } JSON; $this->assertEquals(\json_decode($expected, true), \json_decode((new JsonApiSerializer($jsonApiJsonApiSerializer))->serialize($post), true)); }
public function testClassAlias() { $this->mapping->setClassAlias('NewMessage'); $this->assertEquals('new_message', $this->mapping->getClassAlias()); }