Skip to content

Commit

Permalink
Added possibility to only load relationship data when the relationshi…
Browse files Browse the repository at this point in the history
…p itself is included
  • Loading branch information
kocsismate committed Nov 24, 2015
1 parent b0e5827 commit 6c8f328
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
ADDED:

- Possibility to pass additional meta information for documents when fetching the response
- Possibility to only load relationship data when the relationship itself is included

CHANGED:

- Renamed `getDefaultRelationships()` to `getDefaultIncludedRelationships()` in transformers to better reflect its meaning
- The "data" key of relationships won't be present in the response when it is empty

REMOVED:

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ manifestation of our vision.
* [Exceptions](#exceptions)
* [JsonApi class](#jsonapi-class)
* [Advanced Usage](#advanced-usage)
* [Loading relationship data efficiently](#loading-relationship-data-efficiently)
* [Content negotiation](#content-negotiation)
* [Request/response validation](#request-response-validation)
* [Middlewares](#middlewares)
Expand Down Expand Up @@ -677,6 +678,8 @@ functionality of Woohoo Labs. Yin, it is highly recommended to utilize this clas

This section guides you through the advanced features of Yin.

#### Loading relationship data efficiently

#### Content negotiation

#### Request/response validation
Expand Down
5 changes: 4 additions & 1 deletion examples/User/JsonApi/Resource/UserResourceTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ public function getRelationships($user)
"contacts" => function(array $user) {
return
ToManyRelationship::create()
->setData($user["contacts"], $this->contactTransformer)
->setDataAsCallable(function() use ($user) {
return $user["contacts"];
}, $this->contactTransformer)
->omitWhenNotIncluded()
;
}
];
Expand Down
61 changes: 58 additions & 3 deletions src/JsonApi/Schema/Relationship/AbstractRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ abstract class AbstractRelationship
/**
* @var mixed
*/
protected $data;
private $data;

/**
* @var bool
*/
protected $isCallableData;

/**
* @var bool
*/
protected $omitDataWhenNotIncluded;

/**
* @var \WoohooLabs\Yin\JsonApi\Transformer\ResourceTransformerInterface
Expand Down Expand Up @@ -49,6 +59,8 @@ public function __construct(
$this->meta = $meta;
$this->links = $links;
$this->data = $data;
$this->isCallableData = false;
$this->omitDataWhenNotIncluded = false;
$this->resourceTransformer = $resourceTransformer;
}

Expand All @@ -60,11 +72,33 @@ public function __construct(
public function setData($data, ResourceTransformerInterface $resourceTransformer)
{
$this->data = $data;
$this->isCallableData = false;
$this->resourceTransformer = $resourceTransformer;

return $this;
}

/**
* @param mixed $data
* @param \WoohooLabs\Yin\JsonApi\Transformer\ResourceTransformerInterface $resourceTransformer
* @return $this
*/
public function setDataAsCallable(callable $data, ResourceTransformerInterface $resourceTransformer)
{
$this->data = $data;
$this->isCallableData = true;
$this->resourceTransformer = $resourceTransformer;

return $this;
}

public function omitWhenNotIncluded()
{
$this->omitDataWhenNotIncluded = true;

return $this;
}

/**
* @param \WoohooLabs\Yin\JsonApi\Transformer\Transformation $transformation
* @param string $resourceType
Expand All @@ -81,7 +115,18 @@ public function transform(
array $additionalMeta = []
) {
$relationship = null;
$transformedData = $this->transformData($transformation, $relationshipName, $defaultRelationships);

if ($this->omitDataWhenNotIncluded === false ||
$transformation->request->isIncludedRelationship(
$transformation->basePath,
$relationshipName,
$defaultRelationships
)
) {
$transformedData = $this->transformData($transformation, $relationshipName, $defaultRelationships);
} else {
$transformedData = null;
}

if ($transformation->request->isIncludedField($resourceType, $relationshipName)) {
$relationship = [];
Expand All @@ -98,7 +143,9 @@ public function transform(
}

// DATA
$relationship["data"] = $transformedData;
if (isset($transformedData) === true) {
$relationship["data"] = $transformedData;
}
}

return $relationship;
Expand Down Expand Up @@ -129,4 +176,12 @@ protected function transformResource(

return $this->resourceTransformer->transformToResourceIdentifier($domainObject);
}

/**
* @return mixed
*/
protected function retrieveData()
{
return $this->isCallableData ? call_user_func($this->data, $this) : $this->data;
}
}
7 changes: 4 additions & 3 deletions src/JsonApi/Schema/Relationship/ToManyRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ protected function transformData(
$relationshipName,
array $defaultRelationships
) {
if (empty($this->data) || $this->resourceTransformer === null) {
return [];
$data = $this->retrieveData();
if (empty($data) || $this->resourceTransformer === null) {
return null;
}

$content = [];
foreach ($this->data as $item) {
foreach ($data as $item) {
$content[] = $this->transformResource($transformation, $item, $relationshipName, $defaultRelationships);
}

Expand Down
5 changes: 3 additions & 2 deletions src/JsonApi/Schema/Relationship/ToOneRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ protected function transformData(
$relationshipName,
array $defaultRelationships
) {
if ($this->data === null || $this->resourceTransformer === null) {
$data = $this->retrieveData();
if (isset($data) === false || $this->resourceTransformer === null) {
return null;
}

return $this->transformResource(
$transformation,
$this->data,
$data,
$relationshipName,
$defaultRelationships
);
Expand Down
4 changes: 2 additions & 2 deletions tests/JsonApi/Transformer/AbstractResourceTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function testTransformToResourceWithDefaultRelationship()
$this->assertInstanceOf(RequestInterface::class, $request);

$relationship = new ToOneRelationship();
$relationship->setData([], new StubResourceTransformer("user", "2"));
$relationship->setData(["Father Vader"], new StubResourceTransformer("user", "2"));
return $relationship;
}
];
Expand Down Expand Up @@ -208,7 +208,7 @@ public function testTransformToRelationship()
$relationships = [
"father" => function () {
$relationship = new ToOneRelationship();
$relationship->setData([], new StubResourceTransformer("user", "2"));
$relationship->setData(["Father Vader"], new StubResourceTransformer("user", "2"));
return $relationship;
}
];
Expand Down

0 comments on commit 6c8f328

Please sign in to comment.