Skip to content

Commit 5848d4b

Browse files
author
Mads Møller
authored
Merge pull request #16 from Napp/feature/fixes_for_transforming_relationships
Feature/fixes for transforming relationships
2 parents dff8d57 + f9a6005 commit 5848d4b

9 files changed

+118
-14
lines changed

src/Transformers/ApiTransformer.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,27 +127,31 @@ protected function transformRelationships(array $output, Model $data): array
127127
/** @var Model $data */
128128
$relationships = $data->getRelations();
129129
foreach ($relationships as $relationshipName => $relationship) {
130-
if (null === $relationship) {
131-
$output[$relationshipName] = $this->isMapped($relationshipName) ? $this->convertValueType($relationshipName, null) : null;
130+
if (true === $this->strict && ! $this->isMapped($relationshipName)) {
131+
continue;
132132
}
133-
else if (true === $relationship instanceof Collection) {
133+
134+
$outputKey = $this->findNewKey($relationshipName);
135+
136+
if (null === $relationship) {
137+
$output[$outputKey] = $this->convertValueType($relationshipName, null);
138+
} elseif (true === $relationship instanceof Collection) {
134139
if ($relationship->isEmpty()) {
135-
$output[$relationshipName] = $this->isMapped($relationshipName) ? $this->convertValueType($relationshipName, null) : null;
140+
$output[$outputKey] = $this->convertValueType($relationshipName, null);
136141
continue;
137142
}
138143

139144
if ($this->isTransformAware($relationship->first())) {
140-
$output[$relationshipName] = $relationship->first()->getTransformer()->transformOutput($relationship);
145+
$output[$outputKey] = $relationship->first()->getTransformer()->transformOutput($relationship);
141146
} else {
142-
$output[$relationshipName] = $relationship->toArray();
147+
$output[$outputKey] = $relationship->toArray();
143148
}
144-
}
145-
else {
149+
} else {
146150
// model
147151
if ($this->isTransformAware($relationship)) {
148-
$output[$relationshipName] = $relationship->getTransformer()->transformOutput($relationship);
152+
$output[$outputKey] = $relationship->getTransformer()->transformOutput($relationship);
149153
} else {
150-
$output[$relationshipName] = $relationship->getAttributes();
154+
$output[$outputKey] = $relationship->getAttributes();
151155
}
152156
}
153157
}

tests/ApiPaginatedTransformerTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Pagination\LengthAwarePaginator;
77
use Illuminate\Pagination\Paginator;
88
use Napp\Core\Api\Tests\Models\Category;
9+
use Napp\Core\Api\Tests\Transformers\CategoryStrictTransformer;
910
use Napp\Core\Api\Transformers\ApiTransformer;
1011
use Napp\Core\Api\Tests\TestCase;
1112

@@ -146,4 +147,43 @@ public function test_transform_length_aware_paginated_with_relationships()
146147
$this->assertEquals('Windows', $transformedOutput['data'][1]['products'][1]['title']);
147148
}
148149

150+
public function test_transform_length_aware_paginated_with_relationships_with_strict_mode_on()
151+
{
152+
$category = Category::create(['title' => 'Electronics']);
153+
$category->products()->create(['name' => 'iPhone', 'price'=> 100.0]);
154+
$category->products()->create(['name' => 'Google Pixel', 'price'=> 80.0]);
155+
$category->products()->create(['name' => 'Samsung Galaxy 9', 'price'=> 110.0]);
156+
157+
$category2 = Category::create(['title' => 'Computers']);
158+
$category2->products()->create(['name' => 'Mac', 'price'=> 28860.0]);
159+
$category2->products()->create(['name' => 'Windows', 'price'=> 11000.0]);
160+
161+
$input = Category::with('products')->get();
162+
163+
$paginatedInput = new LengthAwarePaginator($input, count($input) * 4, count($input));
164+
165+
$transformedOutput = (new CategoryStrictTransformer())->transformOutput($paginatedInput);
166+
167+
$this->assertArrayHasKey('current_page', $transformedOutput);
168+
$this->assertArrayHasKey('data', $transformedOutput);
169+
$this->assertArrayHasKey('first_page_url', $transformedOutput);
170+
$this->assertArrayHasKey('from', $transformedOutput);
171+
$this->assertArrayHasKey('last_page', $transformedOutput);
172+
$this->assertArrayHasKey('last_page_url', $transformedOutput);
173+
$this->assertArrayHasKey('next_page_url', $transformedOutput);
174+
$this->assertArrayHasKey('path', $transformedOutput);
175+
$this->assertArrayHasKey('per_page', $transformedOutput);
176+
$this->assertArrayHasKey('prev_page_url', $transformedOutput);
177+
$this->assertArrayHasKey('to', $transformedOutput);
178+
$this->assertArrayHasKey('total', $transformedOutput);
179+
180+
$this->assertArrayNotHasKey('products', $transformedOutput['data'][0]);
181+
$this->assertArrayNotHasKey('products', $transformedOutput['data'][1]);
182+
$this->assertArrayNotHasKey('title', $transformedOutput['data'][0]);
183+
$this->assertArrayNotHasKey('title', $transformedOutput['data'][1]);
184+
185+
$this->assertEquals(1, $transformedOutput['data'][0]['id']);
186+
$this->assertEquals(2, $transformedOutput['data'][1]['id']);
187+
}
188+
149189
}

tests/ApiTransformerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Napp\Core\Api\Tests\Models\Category;
66
use Napp\Core\Api\Tests\Models\Post;
77
use Napp\Core\Api\Tests\Models\Product;
8+
use Napp\Core\Api\Tests\Transformers\CategoryStrictTransformer;
9+
use Napp\Core\Api\Tests\Transformers\CategoryTransformerWithDifferentOutputKey;
810
use Napp\Core\Api\Tests\Transformers\PostTransformer;
911
use Napp\Core\Api\Tests\Transformers\ProductTransformer;
1012
use Napp\Core\Api\Transformers\ApiTransformer;
@@ -314,4 +316,29 @@ public function test_transform_not_strict_model()
314316
$this->assertEquals(3, count($result['tags']));
315317
$this->assertNull($result['otherTags']);
316318
}
319+
320+
public function test_transform_model_relations_is_exluded_if_not_found_in_transform_map_and_strict_mode_is_enabled()
321+
{
322+
/** @var Category $category */
323+
$category = Category::create(['title' => 'Electronics']);
324+
$category->products()->create(['name' => 'iPhone', 'price'=> 100.0]);
325+
$category->load('products');
326+
327+
$result = (new CategoryStrictTransformer)->transformOutput($category);
328+
329+
$this->assertArrayNotHasKey('products', $result);
330+
}
331+
332+
public function test_transform_model_relations_with_different_output_key()
333+
{
334+
/** @var Category $category */
335+
$category = Category::create(['title' => 'Electronics']);
336+
$category->products()->create(['name' => 'iPhone', 'price'=> 100.0]);
337+
$category->load('products');
338+
339+
$result = (new CategoryTransformerWithDifferentOutputKey)->transformOutput($category);
340+
341+
$this->assertArrayNotHasKey('products', $result);
342+
$this->assertArrayHasKey('indexes', $result);
343+
}
317344
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Napp\Core\Api\Tests\Transformers;
4+
5+
use Napp\Core\Api\Transformers\ApiTransformer;
6+
7+
class CategoryStrictTransformer extends ApiTransformer
8+
{
9+
protected $strict = true;
10+
11+
public function __construct()
12+
{
13+
$this->setApiMapping([
14+
'id' => ['newName' => 'id', 'dataType' => 'int'],
15+
]);
16+
}
17+
}

tests/Transformers/CategoryTransformer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
*/
1212
class CategoryTransformer extends ApiTransformer
1313
{
14-
protected $strict = true;
1514
/**
1615
* @param Category $category
1716
*/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Napp\Core\Api\Tests\Transformers;
4+
5+
use Napp\Core\Api\Transformers\ApiTransformer;
6+
7+
class CategoryTransformerWithDifferentOutputKey extends ApiTransformer
8+
{
9+
public function __construct()
10+
{
11+
$this->setApiMapping([
12+
'id' => ['newName' => 'id', 'dataType' => 'int'],
13+
'title' => ['newName' => 'name', 'dataType' => 'string'],
14+
'products' => ['newName' => 'indexes', 'dataType' => 'array'],
15+
]);
16+
}
17+
}

tests/Transformers/PostTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
class PostTransformer extends ApiTransformer
1313
{
14-
protected $strict = false;
14+
1515
/**
1616
* @param Post $post
1717
*/

tests/Transformers/ProductTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
class ProductTransformer extends ApiTransformer
1313
{
14-
protected $strict = true;
14+
1515
/**
1616
* @param Product $product
1717
*/

tests/Transformers/VariantTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212
class VariantTransformer extends ApiTransformer
1313
{
14-
protected $strict = true;
14+
1515
/**
1616
* @param Variant $variant
1717
*/

0 commit comments

Comments
 (0)