Skip to content

Commit

Permalink
v0.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
neomerx committed Jun 25, 2015
2 parents 6e00891 + a0e7fb6 commit 4a25d2d
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 58 deletions.
22 changes: 14 additions & 8 deletions src/Contracts/Document/DocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,21 +143,27 @@ public function addRelationshipToData(
* Add an empty relationship to resource in 'data' section.
*
* @param ResourceObjectInterface $parent
* @param RelationshipObjectInterface $current
* @param RelationshipObjectInterface $relationship
*
* @return void
*/
public function addEmptyRelationshipToData(ResourceObjectInterface $parent, RelationshipObjectInterface $current);
public function addEmptyRelationshipToData(
ResourceObjectInterface $parent,
RelationshipObjectInterface $relationship
);

/**
* Add a null relationship to resource in 'data' section.
*
* @param ResourceObjectInterface $parent
* @param RelationshipObjectInterface $current
* @param RelationshipObjectInterface $relationship
*
* @return void
*/
public function addNullRelationshipToData(ResourceObjectInterface $parent, RelationshipObjectInterface $current);
public function addNullRelationshipToData(
ResourceObjectInterface $parent,
RelationshipObjectInterface $relationship
);

/**
* Add resource to 'included' section.
Expand Down Expand Up @@ -187,26 +193,26 @@ public function addRelationshipToIncluded(
* Add an empty relationship to resource in 'included' section.
*
* @param ResourceObjectInterface $parent
* @param RelationshipObjectInterface $current
* @param RelationshipObjectInterface $relationship
*
* @return void
*/
public function addEmptyRelationshipToIncluded(
ResourceObjectInterface $parent,
RelationshipObjectInterface $current
RelationshipObjectInterface $relationship
);

/**
* Add a null relationship to resource in 'included' section.
*
* @param ResourceObjectInterface $parent
* @param RelationshipObjectInterface $current
* @param RelationshipObjectInterface $relationship
*
* @return void
*/
public function addNullRelationshipToIncluded(
ResourceObjectInterface $parent,
RelationshipObjectInterface $current
RelationshipObjectInterface $relationship
);

/**
Expand Down
28 changes: 17 additions & 11 deletions src/Document/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,35 +192,41 @@ public function addRelationshipToIncluded(
/**
* @inheritdoc
*/
public function addEmptyRelationshipToData(ResourceObjectInterface $parent, RelationshipObjectInterface $current)
{
$this->presenter->setRelationshipTo($this->bufferForData, $parent, $current, []);
public function addEmptyRelationshipToData(
ResourceObjectInterface $parent,
RelationshipObjectInterface $relationship
) {
$this->presenter->setRelationshipTo($this->bufferForData, $parent, $relationship, []);
}

/**
* @inheritdoc
*/
public function addNullRelationshipToData(ResourceObjectInterface $parent, RelationshipObjectInterface $current)
{
$this->presenter->setRelationshipTo($this->bufferForData, $parent, $current, null);
public function addNullRelationshipToData(
ResourceObjectInterface $parent,
RelationshipObjectInterface $relationship
) {
$this->presenter->setRelationshipTo($this->bufferForData, $parent, $relationship, null);
}

/**
* @inheritdoc
*/
public function addEmptyRelationshipToIncluded(
ResourceObjectInterface $parent,
RelationshipObjectInterface $current
RelationshipObjectInterface $relationship
) {
$this->presenter->setRelationshipTo($this->bufferForIncluded, $parent, $current, []);
$this->presenter->setRelationshipTo($this->bufferForIncluded, $parent, $relationship, []);
}

/**
* @inheritdoc
*/
public function addNullRelationshipToIncluded(ResourceObjectInterface $parent, RelationshipObjectInterface $current)
{
$this->presenter->setRelationshipTo($this->bufferForIncluded, $parent, $current, null);
public function addNullRelationshipToIncluded(
ResourceObjectInterface $parent,
RelationshipObjectInterface $relationship
) {
$this->presenter->setRelationshipTo($this->bufferForIncluded, $parent, $relationship, null);
}

/**
Expand Down
64 changes: 39 additions & 25 deletions src/Document/Presenters/ElementPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,50 @@ public function __construct(Document $document)
/**
* @param array $target
* @param ResourceObjectInterface $parent
* @param RelationshipObjectInterface $current
* @param mixed $url
* @param RelationshipObjectInterface $relation
* @param mixed $value
*
* @return void
*/
public function setRelationshipTo(
array &$target,
ResourceObjectInterface $parent,
RelationshipObjectInterface $current,
$url
RelationshipObjectInterface $relation,
$value
) {
$parentId = $parent->getId();
$parentType = $parent->getType();
$name = $current->getName();
$name = $relation->getName();
$parentExists = isset($target[$parentType][$parentId]);

assert('$parentExists === true');
assert('isset($target[$parentType][$parentId][\''.Document::KEYWORD_RELATIONSHIPS.'\'][$name]) === false');

if ($parentExists === true) {
$target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name] = $url;
$representation = [];

if ($relation->isShowData() === true) {
$representation[Document::KEYWORD_LINKAGE_DATA][] = $value;
}

$representation += $this->getRelationRepresentation($parent, $relation);

$target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name] = $representation;
}
}

/**
* @param array $target
* @param ResourceObjectInterface $parent
* @param RelationshipObjectInterface $relationship
* @param RelationshipObjectInterface $relation
* @param ResourceObjectInterface $resource
*
* @return void
*/
public function addRelationshipTo(
array &$target,
ResourceObjectInterface $parent,
RelationshipObjectInterface $relationship,
RelationshipObjectInterface $relation,
ResourceObjectInterface $resource
) {
$parentId = $parent->getId();
Expand All @@ -88,16 +96,28 @@ public function addRelationshipTo(

// parent might be already added to included to it won't be in 'target' buffer
if ($parentExists === true) {
$name = $relationship->getName();
$alreadyGotData = isset($target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name]);
if ($alreadyGotData === false) {
// ... add the first one
$target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS][$name] =
$this->getRelationRepresentation($parent, $relationship, $resource);
} else {
// ... or add another relation
$target[$parentType][$parentId][Document::KEYWORD_RELATIONSHIPS]
[$name][Document::KEYWORD_LINKAGE_DATA][] = $this->getLinkageRepresentation($resource);
$parentAlias = &$target[$parentType][$parentId];

$name = $relation->getName();
$alreadyGotRelation = isset($parentAlias[Document::KEYWORD_RELATIONSHIPS][$name]);

$linkage = null;
if ($relation->isShowData() === true) {
$linkage = $this->getLinkageRepresentation($resource);
}

if ($alreadyGotRelation === false) {
// ... add the first linkage
$representation = [];
if ($linkage !== null) {
$representation[Document::KEYWORD_LINKAGE_DATA][] = $linkage;
}
$representation += $this->getRelationRepresentation($parent, $relation);

$parentAlias[Document::KEYWORD_RELATIONSHIPS][$name] = $representation;
} elseif ($alreadyGotRelation === true && $linkage !== null) {
// ... or add another linkage
$parentAlias[Document::KEYWORD_RELATIONSHIPS][$name][Document::KEYWORD_LINKAGE_DATA][] = $linkage;
}
}
}
Expand Down Expand Up @@ -235,14 +255,12 @@ private function getLinkRepresentation($prefix = null, LinkInterface $link = nul
/**
* @param ResourceObjectInterface $parent
* @param RelationshipObjectInterface $relation
* @param ResourceObjectInterface $resource
*
* @return array
*/
private function getRelationRepresentation(
ResourceObjectInterface $parent,
RelationshipObjectInterface $relation,
ResourceObjectInterface $resource
RelationshipObjectInterface $relation
) {
assert(
'$relation->getName() !== \''.Document::KEYWORD_SELF.'\'',
Expand All @@ -251,10 +269,6 @@ private function getRelationRepresentation(

$representation = [];

if ($relation->isShowData() === true) {
$representation[Document::KEYWORD_LINKAGE_DATA][] = $this->getLinkageRepresentation($resource);
}

if (($meta = $relation->getMeta()) !== null) {
$representation[Document::KEYWORD_META] = $meta;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Encoder/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private function getEncodingParameters($data, EncodingParametersInterface $param
} elseif ($parameters !== null && $parameters->getIncludePaths() !== null) {
return $parameters;
} else {
$schema = $this->container->getSchema(is_array($data) ? $data[0] : $data);
$schema = $this->container->getSchema(is_array($data) ? reset($data) : $data);
$includePaths = $schema->getIncludePaths();
$fieldSets = $parameters === null ? null : $parameters->getFieldSets();

Expand Down
2 changes: 1 addition & 1 deletion src/Encoder/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private function parseData($data)
} else {
if (is_array($data) === true) {
$isOriginallyArrayed = true;
$schema = $this->container->getSchema($data[0]);
$schema = $this->container->getSchema(reset($data));
} else {
$isOriginallyArrayed = false;
$schema = $this->container->getSchema($data);
Expand Down
27 changes: 20 additions & 7 deletions tests/Document/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ public function testAddEmptyLinkToData()
'relationship-name',
new stdClass(), // in reality it will be a Comment class instance where $resource properties were taken from
[], // links
['this meta' => 'wont be shown'], // relationship meta
['some' => 'relationship meta'], // relationship meta
true // show data
);

Expand All @@ -494,7 +494,10 @@ public function testAddEmptyLinkToData()
"lastName" : "Dow"
},
"relationships" : {
"relationship-name" : []
"relationship-name" : {
"data" : [],
"meta" : { "some" : "relationship meta"}
}
}
}
}
Expand Down Expand Up @@ -537,7 +540,9 @@ public function testAddNullLinkToData()
"lastName" : "Dow"
},
"relationships" : {
"relationship-name" : null
"relationship-name" : {
"data" : null
}
}
}
}
Expand Down Expand Up @@ -805,7 +810,9 @@ public function testAddEmptyLinkToIncluded()
"lastName" : "Dow"
},
"relationships" : {
"comments-relationship" : []
"comments-relationship" : {
"data" : []
}
},
"links" : {
"self" : "peopleSelfUrl/"
Expand Down Expand Up @@ -858,7 +865,9 @@ public function testAddNullLinkToIncluded()
"lastName" : "Dow"
},
"relationships" : {
"comments-relationship" : null
"comments-relationship" : {
"data" : null
}
},
"links" : {
"self" : "peopleSelfUrl/"
Expand Down Expand Up @@ -1024,7 +1033,9 @@ public function testRelationshipsPrimaryMeta()
"lastName" : "Dow"
},
"relationships" : {
"relationship-name" : null,
"relationship-name" : {
"data" : null
},
"meta" : { "some" : "relationships meta" }
}
}
Expand Down Expand Up @@ -1077,7 +1088,9 @@ public function testRelationshipsInclusionMeta()
"lastName" : "Dow"
},
"relationships" : {
"relationship-name" : null,
"relationship-name" : {
"data" : null
},
"meta" : { "some" : "relationships meta" }
}
}]
Expand Down
6 changes: 3 additions & 3 deletions tests/Encoder/EncodeIncludedObjectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ public function testEncodeWithNullAndEmptyLinks()
"body" : "Outside every fat man there was an even fatter man trying to close in"
},
"relationships" : {
"author" : null,
"comments" : []
"author" : {"data" : null},
"comments" : {"data" : []}
}
}]
}
Expand Down Expand Up @@ -335,7 +335,7 @@ public function testEncodeDuplicatesWithCyclicDeps()
"author" : {
"data" : { "type" : "posts", "id" : "1" }
},
"comments" : []
"comments" : {"data" : []}
}
}]
}
Expand Down
Loading

0 comments on commit 4a25d2d

Please sign in to comment.