Skip to content

Commit

Permalink
Merge pull request #16 from Napp/feature/fixes_for_transforming_relat…
Browse files Browse the repository at this point in the history
…ionships

Feature/fixes for transforming relationships
  • Loading branch information
Mads Møller authored Oct 25, 2018
2 parents dff8d57 + f9a6005 commit 5848d4b
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 14 deletions.
24 changes: 14 additions & 10 deletions src/Transformers/ApiTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,31 @@ protected function transformRelationships(array $output, Model $data): array
/** @var Model $data */
$relationships = $data->getRelations();
foreach ($relationships as $relationshipName => $relationship) {
if (null === $relationship) {
$output[$relationshipName] = $this->isMapped($relationshipName) ? $this->convertValueType($relationshipName, null) : null;
if (true === $this->strict && ! $this->isMapped($relationshipName)) {
continue;
}
else if (true === $relationship instanceof Collection) {

$outputKey = $this->findNewKey($relationshipName);

if (null === $relationship) {
$output[$outputKey] = $this->convertValueType($relationshipName, null);
} elseif (true === $relationship instanceof Collection) {
if ($relationship->isEmpty()) {
$output[$relationshipName] = $this->isMapped($relationshipName) ? $this->convertValueType($relationshipName, null) : null;
$output[$outputKey] = $this->convertValueType($relationshipName, null);
continue;
}

if ($this->isTransformAware($relationship->first())) {
$output[$relationshipName] = $relationship->first()->getTransformer()->transformOutput($relationship);
$output[$outputKey] = $relationship->first()->getTransformer()->transformOutput($relationship);
} else {
$output[$relationshipName] = $relationship->toArray();
$output[$outputKey] = $relationship->toArray();
}
}
else {
} else {
// model
if ($this->isTransformAware($relationship)) {
$output[$relationshipName] = $relationship->getTransformer()->transformOutput($relationship);
$output[$outputKey] = $relationship->getTransformer()->transformOutput($relationship);
} else {
$output[$relationshipName] = $relationship->getAttributes();
$output[$outputKey] = $relationship->getAttributes();
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions tests/ApiPaginatedTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Napp\Core\Api\Tests\Models\Category;
use Napp\Core\Api\Tests\Transformers\CategoryStrictTransformer;
use Napp\Core\Api\Transformers\ApiTransformer;
use Napp\Core\Api\Tests\TestCase;

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

public function test_transform_length_aware_paginated_with_relationships_with_strict_mode_on()
{
$category = Category::create(['title' => 'Electronics']);
$category->products()->create(['name' => 'iPhone', 'price'=> 100.0]);
$category->products()->create(['name' => 'Google Pixel', 'price'=> 80.0]);
$category->products()->create(['name' => 'Samsung Galaxy 9', 'price'=> 110.0]);

$category2 = Category::create(['title' => 'Computers']);
$category2->products()->create(['name' => 'Mac', 'price'=> 28860.0]);
$category2->products()->create(['name' => 'Windows', 'price'=> 11000.0]);

$input = Category::with('products')->get();

$paginatedInput = new LengthAwarePaginator($input, count($input) * 4, count($input));

$transformedOutput = (new CategoryStrictTransformer())->transformOutput($paginatedInput);

$this->assertArrayHasKey('current_page', $transformedOutput);
$this->assertArrayHasKey('data', $transformedOutput);
$this->assertArrayHasKey('first_page_url', $transformedOutput);
$this->assertArrayHasKey('from', $transformedOutput);
$this->assertArrayHasKey('last_page', $transformedOutput);
$this->assertArrayHasKey('last_page_url', $transformedOutput);
$this->assertArrayHasKey('next_page_url', $transformedOutput);
$this->assertArrayHasKey('path', $transformedOutput);
$this->assertArrayHasKey('per_page', $transformedOutput);
$this->assertArrayHasKey('prev_page_url', $transformedOutput);
$this->assertArrayHasKey('to', $transformedOutput);
$this->assertArrayHasKey('total', $transformedOutput);

$this->assertArrayNotHasKey('products', $transformedOutput['data'][0]);
$this->assertArrayNotHasKey('products', $transformedOutput['data'][1]);
$this->assertArrayNotHasKey('title', $transformedOutput['data'][0]);
$this->assertArrayNotHasKey('title', $transformedOutput['data'][1]);

$this->assertEquals(1, $transformedOutput['data'][0]['id']);
$this->assertEquals(2, $transformedOutput['data'][1]['id']);
}

}
27 changes: 27 additions & 0 deletions tests/ApiTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Napp\Core\Api\Tests\Models\Category;
use Napp\Core\Api\Tests\Models\Post;
use Napp\Core\Api\Tests\Models\Product;
use Napp\Core\Api\Tests\Transformers\CategoryStrictTransformer;
use Napp\Core\Api\Tests\Transformers\CategoryTransformerWithDifferentOutputKey;
use Napp\Core\Api\Tests\Transformers\PostTransformer;
use Napp\Core\Api\Tests\Transformers\ProductTransformer;
use Napp\Core\Api\Transformers\ApiTransformer;
Expand Down Expand Up @@ -314,4 +316,29 @@ public function test_transform_not_strict_model()
$this->assertEquals(3, count($result['tags']));
$this->assertNull($result['otherTags']);
}

public function test_transform_model_relations_is_exluded_if_not_found_in_transform_map_and_strict_mode_is_enabled()
{
/** @var Category $category */
$category = Category::create(['title' => 'Electronics']);
$category->products()->create(['name' => 'iPhone', 'price'=> 100.0]);
$category->load('products');

$result = (new CategoryStrictTransformer)->transformOutput($category);

$this->assertArrayNotHasKey('products', $result);
}

public function test_transform_model_relations_with_different_output_key()
{
/** @var Category $category */
$category = Category::create(['title' => 'Electronics']);
$category->products()->create(['name' => 'iPhone', 'price'=> 100.0]);
$category->load('products');

$result = (new CategoryTransformerWithDifferentOutputKey)->transformOutput($category);

$this->assertArrayNotHasKey('products', $result);
$this->assertArrayHasKey('indexes', $result);
}
}
17 changes: 17 additions & 0 deletions tests/Transformers/CategoryStrictTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Napp\Core\Api\Tests\Transformers;

use Napp\Core\Api\Transformers\ApiTransformer;

class CategoryStrictTransformer extends ApiTransformer
{
protected $strict = true;

public function __construct()
{
$this->setApiMapping([
'id' => ['newName' => 'id', 'dataType' => 'int'],
]);
}
}
1 change: 0 additions & 1 deletion tests/Transformers/CategoryTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
class CategoryTransformer extends ApiTransformer
{
protected $strict = true;
/**
* @param Category $category
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/Transformers/CategoryTransformerWithDifferentOutputKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Napp\Core\Api\Tests\Transformers;

use Napp\Core\Api\Transformers\ApiTransformer;

class CategoryTransformerWithDifferentOutputKey extends ApiTransformer
{
public function __construct()
{
$this->setApiMapping([
'id' => ['newName' => 'id', 'dataType' => 'int'],
'title' => ['newName' => 'name', 'dataType' => 'string'],
'products' => ['newName' => 'indexes', 'dataType' => 'array'],
]);
}
}
2 changes: 1 addition & 1 deletion tests/Transformers/PostTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
class PostTransformer extends ApiTransformer
{
protected $strict = false;

/**
* @param Post $post
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Transformers/ProductTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
class ProductTransformer extends ApiTransformer
{
protected $strict = true;

/**
* @param Product $product
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Transformers/VariantTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
class VariantTransformer extends ApiTransformer
{
protected $strict = true;

/**
* @param Variant $variant
*/
Expand Down

0 comments on commit 5848d4b

Please sign in to comment.