public function testToArrayIncludesTheResourcesRepresentation() { $post = (object) ['id' => 1, 'foo' => 'bar']; $resource = new Resource($post, new PostSerializer2()); $document = new Document($resource); $this->assertEquals(['data' => $resource->toArray()], $document->toArray()); }
/** * @param Request $request * @param Created $payload */ public function created(Request $request, Created $payload) { $serializer = PerformanceStatistic::getSerializer(); $resource = new Resource($payload->getModel(), $serializer); $document = new Document($resource); return new JsonResponse($document->toArray(), 201, ['Location' => $resource->getLinks()['self']]); }
public function testToIdentifierReturnsArrayOfResourceIdentifiers() { $serializer = new PostSerializer3(); $post1 = (object) ['id' => 1]; $post2 = (object) ['id' => 2]; $collection = new Collection([$post1, $post2], $serializer); $resource1 = new Resource($post1, $serializer); $resource2 = new Resource($post2, $serializer); $this->assertEquals([$resource1->toIdentifier(), $resource2->toIdentifier()], $collection->toIdentifier()); }
public function testCanMergeWithAnotherResource() { $post1 = (object) ['id' => '123', 'foo' => 'bar', 'comments' => [1]]; $post2 = (object) ['id' => '123', 'baz' => 'qux', 'comments' => [1, 2]]; $resource1 = new Resource($post1, new PostSerializer4()); $resource2 = new Resource($post2, new PostSerializer4()); $resource1->with(['comments']); $resource2->with(['comments']); $resource1->merge($resource2); $this->assertEquals(['type' => 'posts', 'id' => '123', 'attributes' => ['baz' => 'qux', 'foo' => 'bar'], 'relationships' => ['comments' => ['data' => [['type' => 'comments', 'id' => '1'], ['type' => 'comments', 'id' => '2']]]]], $resource1->toArray()); }
/** * @param Resource[] $resources * @param Resource $newResource * @return Resource[] */ protected function mergeResource(array $resources, Resource $newResource) { $type = $newResource->getType(); $id = $newResource->getId(); if (isset($resources[$type][$id])) { $resources[$type][$id]->merge($newResource); } else { $resources[$type][$id] = $newResource; } return $resources; }
/** * @param Resource[] $resources * @param Resource $newResource * @return Resource[] */ protected function mergeResource(array $resources, Resource $newResource) { $type = $newResource->getType(); $id = $newResource->getId(); foreach ($resources as $resource) { if ($resource->getType() === $type && $resource->getId() === $id) { $resource->merge($newResource); return $resources; } } $resources[] = $newResource; return $resources; }
/** * Get the result of an API request to show the current user. * * @return array */ protected function getUserDocument(Request $request) { $actor = $request->getAttribute('actor'); $this->userSerializer->setActor($actor); $resource = new Resource($actor, $this->userSerializer); $document = new Document($resource->with('groups')); return $document->toArray(); }