public function addFormat($alias, FormatInterface $format) { $this->formats->add($alias, $format); }
public function benchSerializardClosure() { $formats = new FormatContainer(); $formats->add('json', new JsonFormat()); $normalizers = new FallbackNormalizerContainer(); $normalizers->add(Team::class, 'team', function (Team $team) { return ['users' => $team->getUsers()]; }); $normalizers->add(User::class, 'user', function (User $user) { return ['id' => $user->getId(), 'email' => $user->getEmail(), 'tags' => $user->getTags(), 'createdAt' => $user->getCreatedAt()]; }); $normalizers->add(Tag::class, 'tag', function (Tag $tag) { return ['id' => $tag->getId(), 'name' => $tag->getName()]; }); $normalizers->add(\DateTime::class, 'date', function (\DateTime $date) { return $date->format(\DateTime::RFC3339); }); $hydrators = new FallbackHydratorContainer(); $serializard = new Serializard($formats, $normalizers, $hydrators); return $serializard->serialize($this->data, 'json'); }
public function testParent() { $userClass = 'Thunder\\Serializard\\Tests\\Fake\\FakeUser'; $user = new FakeUser(1, '*****@*****.**', new FakeTag(1, 'tag')); $formats = new FormatContainer(); $formats->add('array', new ArrayFormat()); $normalizers = new FallbackNormalizerContainer(); $hydrators = new FallbackHydratorContainer(); $serializard = new Serializard($formats, $normalizers, $hydrators); $normalizers->add($userClass . 'ParentParent', 'user', function (FakeUserParentParent $user) { return 'ancestor'; }); $this->assertSame('ancestor', $serializard->serialize($user, 'array')); $normalizers->add($userClass . 'Parent', 'user', function (FakeUserParent $user) { return 'parent'; }); $this->assertSame('parent', $serializard->serialize($user, 'array')); $normalizers->add($userClass, 'user', function (FakeUser $user) { return 'user'; }); $this->assertSame('user', $serializard->serialize($user, 'array')); }