protected function createResource($uri = null) { $resource = new Resource(); if ($uri) { $resource->setURI($uri); } return $resource; }
private function doTransformResource(&$data, &$embedded, $rel, Resource $resource) { if (!$resource->getUri()) { $data[$rel][] = $this->resourceToArray($resource); } else { $embedded[$rel][] = $this->resourceToArray($resource); } }
protected function transformResources(array &$data, Resource $resource) { foreach ($resource->getAllResources() as $rel => $resources) { if (!is_array($resources)) { $resources = [$resources]; } foreach ($resources as &$resource) { $data['entities'][] = $this->doTransformResource($data, $rel, $resource); } } }
protected function transformEmbedded(array $data) { $resource = new Resource(); isset($data['attributes'][self::LINK_KEY_HREF]) && $resource->setURI($data['attributes'][self::LINK_KEY_HREF]); isset($data['attributes'][self::LINK_KEY_TITLE]) && $resource->setTitle($data['attributes'][self::LINK_KEY_TITLE]); $embeddeds = []; $links = []; $data2 = []; foreach ($data['values'] as &$node) { if ($this->nodeIsResource($node)) { $embeddeds[$node['attributes']['rel']][] = $this->transformEmbedded($node); } elseif ($this->nodeIsLink($node)) { $links[$node['attributes']['rel']][] = $this->transformLink($node); } else { if (is_array($node['values'])) { $toSave = []; foreach ($node['values'] as &$value) { $toSave[$value['name']][] = $value['values']; } } else { $toSave = $node['values']; } $data2[$node['name']][] = $toSave; } } $resource->setData($data2); foreach ($links as $rel => $link) { $resource->setLinks($rel, $link); } foreach ($embeddeds as $rel => $embedded) { $resource->addResources($rel, $embedded); } return $resource; }
protected function doTransformEmbedded(array $data) { $resource = new Resource(); foreach ($data as $key => $value) { if (substr($key, 0, 1) !== '_') { $resource->addData($key, $value); } } $this->transformMetadata($resource, $data); return $resource; }
public function testExpandLinkedResourcesTreeThreeLevelLinked() { $linkedLevel1Resource = new Resource($this->repository); $linkedLevel1Resource->setURI('foo'); $linkedLevel2Resource = new Resource($this->repository); $linkedLevel2Resource->setURI('foo'); $linkedLevel2Resource->linkResource('bar', $linkedLevel1Resource); $linkedPhpHalResource = new Resource($this->repository); $linkedPhpHalResource->setURI('foo'); $linkedPhpHalResource->linkResource('qux', $linkedLevel2Resource); $this->resource->linkResource('foo', $linkedPhpHalResource); $this->assertCount(0, $linkedLevel2Resource->getAllResources()); $this->resource->expandLinkedResourcesTree(['foo', 'qux', 'bar']); $this->assertSame($linkedLevel1Resource, $linkedLevel2Resource->getResources('bar')); }