Skip to content

Commit

Permalink
Merge pull request #43 from thanh-taro/add_relationships_data
Browse files Browse the repository at this point in the history
Update code
  • Loading branch information
thanh-taro authored Jun 22, 2016
2 parents b635abf + 628d848 commit 483f876
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 65 deletions.
6 changes: 5 additions & 1 deletion src/JsonObjects/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ public function add($field, $value, $key = null)
if ($key === null) {
$this->_params[$field][] = $add;
} else {
$this->_params[$field][$key] = $add;
if ($this->_params[$field] instanceof self) {
$this->_params[$field]->set($key, $add);
} elseif (is_array($this->_params[$field])) {
$this->_params[$field][$key] = $add;
}
}
}
}
Expand Down
154 changes: 90 additions & 64 deletions src/JsonObjects/V1_0/TopLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,32 @@ public function addModel($model, $getRelationships = true)
return $this;
}

public function setPagination(AbstractPaginator $collections)
public function setPagination(AbstractPaginator $collections, $isRelationship = false, $path = null)
{
list($resources, $links, $meta, $includes) = $this->parsePagination($collections);
list($resources, $links, $meta, $includes) = $this->parsePagination($collections, $isRelationship, $path);
$this->set('data', $resources);
if (!empty($links)) {
$this->set('links', $links);
$oldLinks = $this->links;
if (empty($oldLinks)) {
$this->set('links', $links);
} else {
$links = ($links instanceof Links) ? $links->toArray() : $links;
if ($oldLinks instanceof Links) {
foreach ($links as $key => $link) {
$oldLinks->set($key, $link);
}
} elseif ($oldLinks instanceof Link || !is_array($oldLinks)) {
$oldLinks = [$oldLinks];
foreach ($links as $key => $link) {
$oldLinks[$key] = $link;
}
} else {
foreach ($links as $key => $link) {
$oldLinks[$key] = $link;
}
}
$this->set('links', $oldLinks);
}
}
if (!empty($meta)) {
$this->set('meta', $meta);
Expand Down Expand Up @@ -186,14 +206,39 @@ public function parseModel($model, $getRelationships = true)
$relationshipTopLevel = new static();
if (!is_array($value)) {
$data = $value;
} elseif (!empty($value['data'])) {
$data = $value['data'];
} else {
$data = null;
if (!empty($value['data'])) {
$data = $value['data'];
} else {
$data = null;
}
if (!empty($value['links'])) {
if ($value['links'] instanceof Link) {
$links = $value['links']->getParams(['self', 'related']);
} else {
$links = $value['links'];
}
if (!empty($links['self'])) {
$relationshipTopLevel->add('links', $links['self'], 'self');
}
if (!empty($links['related'])) {
$relationshipTopLevel->add('links', $links['related'], 'related');
}
}
}
if ($data !== null) {
if ($data instanceof AbstractPaginator) {
$relationshipTopLevel->setPagination($data);
$links = $relationshipTopLevel->links;
if (empty($links)) {
$path = null;
} elseif (is_array($links) && isset($links['self'])) {
$path = $links['self'];
} elseif ($links instanceof Links) {
$path = $links->self;
} else {
$path = null;
}
$relationshipTopLevel->setPagination($data, true, $path);
$isList = true;
} else {
if ($data instanceof Collection) {
Expand All @@ -203,19 +248,6 @@ public function parseModel($model, $getRelationships = true)
$relationshipTopLevel->setModel($data, false);
}
}
if (!empty($value['links'])) {
if ($value['links'] instanceof Link) {
$links = $value['links']->getParams(['self', 'related']);
} else {
$links = $value['links'];
}
if (!empty($links['self'])) {
$relationshipTopLevel->add('links', $links['self'], 'self');
}
if (!empty($links['related'])) {
$relationshipTopLevel->add('links', $links['related'], 'related');
}
}
}
$relationshipTopLevel->delete('jsonapi');
$data = $relationshipTopLevel->data;
Expand All @@ -228,11 +260,8 @@ public function parseModel($model, $getRelationships = true)
if (!empty($relationshipArray['meta'])) {
$relationship['meta'] = $relationshipArray['meta'];
}
if (!empty($relationshipArray['links']['self'])) {
$relationship['links']['self'] = $relationshipArray['links']['self'];
}
if (!empty($relationshipArray['links']['related'])) {
$relationship['links']['related'] = $relationshipArray['links']['related'];
if (!empty($relationshipArray['links'])) {
$relationship['links'] = $relationshipArray['links'];
}
if (!empty($relationshipArray['data'])) {
if ($relationshipTopLevel->data instanceof Resource) {
Expand All @@ -246,7 +275,13 @@ public function parseModel($model, $getRelationships = true)
}
}
if (!empty($relationshipArray['data'])) {
$includes[] = new Resource($relationshipArray['data']);
if ($isList && !empty($data)) {
foreach ($data as $include) {
$includes[] = ($include instanceof Resource) ? $include : new Resource($include);
}
} else {
$includes[] = new Resource($relationshipArray['data']);
}
}
if (empty($relationship) || empty($relationship['data'])) {
if ($isList) {
Expand All @@ -263,55 +298,46 @@ public function parseModel($model, $getRelationships = true)
return [$resource, $includes];
}

public function parsePagination(AbstractPaginator $collections)
public function parsePagination(AbstractPaginator $collections, $isRelationship = false, $path = null)
{
$resources = [];
$includes = [];
foreach ($collections->items() as $item) {
/*
$resource = new Resource([
'id' => $item->getResourceId(),
'type' => $item->getResourceType(),
]);
$itemAttributes = $item->getResourceAttributes();
if (!empty($itemAttributes)) {
$resource->set('attributes', $itemAttributes);
}
$itemLinks = $item->getResourceLinks();
if (!empty($itemLinks)) {
$resource->set('links', $itemLinks);
}
$resources[] = $resource;
*/
list($resource, $included) = $this->parseModel($item);
$resources[] = $resource;
$includes = array_merge($includes, $included);
}

$linksArr = [
'first' => $collections->url(1),
'prev' => $collections->previousPageUrl(),
'next' => $collections->nextPageUrl(),
'last' => method_exists($collections, 'lastPage') ? $collections->url($collections->lastPage()) : null,
];
$currentQuery = parse_url(url()->full(), PHP_URL_QUERY);
if (!empty($currentQuery)) {
parse_str($currentQuery, $queryArr);
foreach ($linksArr as $key => $value) {
if (!empty($value)) {
$urlQuery = parse_url($value, PHP_URL_QUERY);
if (!empty($urlQuery)) {
parse_str($urlQuery, $urlArr);
foreach ($urlArr as $name => $val) {
if (isset($queryArr[$name]) && is_array($queryArr[$name]) && is_array($val)) {
foreach ($val as $k => $v) {
$queryArr[$name][$k] = $v;
if ($isRelationship && $path === null) {
$linksArr = [];
} else {
if ($path !== null) {
$collections->setPath($path);
}
$linksArr = [
'first' => $collections->url(1),
'prev' => $collections->previousPageUrl(),
'next' => $collections->nextPageUrl(),
'last' => method_exists($collections, 'lastPage') ? $collections->url($collections->lastPage()) : null,
];
$currentQuery = parse_url(url()->full(), PHP_URL_QUERY);
if (!empty($currentQuery)) {
parse_str($currentQuery, $queryArr);
foreach ($linksArr as $key => $value) {
if (!empty($value)) {
$urlQuery = parse_url($value, PHP_URL_QUERY);
if (!empty($urlQuery)) {
parse_str($urlQuery, $urlArr);
foreach ($urlArr as $name => $val) {
if (isset($queryArr[$name]) && is_array($queryArr[$name]) && is_array($val)) {
foreach ($val as $k => $v) {
$queryArr[$name][$k] = $v;
}
} else {
$queryArr[$name] = $val;
}
} else {
$queryArr[$name] = $val;
}
$linksArr[$key] = url()->current().'?'.http_build_query($queryArr);
}
$linksArr[$key] = url()->current().'?'.http_build_query($queryArr);
}
}
}
Expand Down

0 comments on commit 483f876

Please sign in to comment.