Skip to content

Commit f9a6005

Browse files
author
Michael Lundbøl
committed
Allow relationships to be mapped into another output key
1 parent b2593e0 commit f9a6005

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

src/Transformers/ApiTransformer.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,25 +131,27 @@ protected function transformRelationships(array $output, Model $data): array
131131
continue;
132132
}
133133

134+
$outputKey = $this->findNewKey($relationshipName);
135+
134136
if (null === $relationship) {
135-
$output[$relationshipName] = $this->convertValueType($relationshipName, null);
137+
$output[$outputKey] = $this->convertValueType($relationshipName, null);
136138
} elseif (true === $relationship instanceof Collection) {
137139
if ($relationship->isEmpty()) {
138-
$output[$relationshipName] = $this->convertValueType($relationshipName, null);
140+
$output[$outputKey] = $this->convertValueType($relationshipName, null);
139141
continue;
140142
}
141143

142144
if ($this->isTransformAware($relationship->first())) {
143-
$output[$relationshipName] = $relationship->first()->getTransformer()->transformOutput($relationship);
145+
$output[$outputKey] = $relationship->first()->getTransformer()->transformOutput($relationship);
144146
} else {
145-
$output[$relationshipName] = $relationship->toArray();
147+
$output[$outputKey] = $relationship->toArray();
146148
}
147149
} else {
148150
// model
149151
if ($this->isTransformAware($relationship)) {
150-
$output[$relationshipName] = $relationship->getTransformer()->transformOutput($relationship);
152+
$output[$outputKey] = $relationship->getTransformer()->transformOutput($relationship);
151153
} else {
152-
$output[$relationshipName] = $relationship->getAttributes();
154+
$output[$outputKey] = $relationship->getAttributes();
153155
}
154156
}
155157
}

tests/ApiTransformerTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Napp\Core\Api\Tests\Models\Post;
77
use Napp\Core\Api\Tests\Models\Product;
88
use Napp\Core\Api\Tests\Transformers\CategoryStrictTransformer;
9+
use Napp\Core\Api\Tests\Transformers\CategoryTransformerWithDifferentOutputKey;
910
use Napp\Core\Api\Tests\Transformers\PostTransformer;
1011
use Napp\Core\Api\Tests\Transformers\ProductTransformer;
1112
use Napp\Core\Api\Transformers\ApiTransformer;
@@ -323,8 +324,21 @@ public function test_transform_model_relations_is_exluded_if_not_found_in_transf
323324
$category->products()->create(['name' => 'iPhone', 'price'=> 100.0]);
324325
$category->load('products');
325326

326-
$result = (new CategoryStrictTransformer())->transformOutput($category);
327+
$result = (new CategoryStrictTransformer)->transformOutput($category);
327328

328329
$this->assertArrayNotHasKey('products', $result);
329330
}
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+
}
330344
}
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+
}

0 commit comments

Comments
 (0)