Skip to content

Commit 3d4e93c

Browse files
authored
Merge pull request #97 from Insolita/fix-non-db-model-transformers
support non-db relations for transformers
2 parents ae3155b + a00aa1c commit 3d4e93c

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

src/generator/default/transformer.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,31 @@ public function include<?=$relation->getCamelName()?>(<?=$transformer->dbModel->
6363
<?php endif;?>
6464
}
6565
<?php endforeach;?>
66+
<?php foreach ($transformer->nonDbRelations as $relation):?>
67+
68+
public function include<?=$relation->getCamelName()?>(<?=$transformer->dbModel->getClassName()?> $model)
69+
{
70+
$relation = $model-><?=Inflector::variablize($relation->getName())?>;
71+
<?php if ($relation->isHasOne()):?>
72+
if ($relation === null) {
73+
return $this->null();
74+
}
75+
<?php if ($relation->getClassName() === $transformer->dbModel->getClassName()):?>
76+
$transformer = new static();
77+
<?php else:?>
78+
$transformer = new <?=Inflector::singularize($relation->getClassName())?>Transformer();
79+
<?php endif;?>
80+
return $this->item($relation, $transformer, '<?=$transformer->makeResourceKey($relation->getClassKey())?>');
81+
<?php else:?>
82+
<?php if ($relation->getClassName() === $transformer->dbModel->getClassName()):?>
83+
$transformer = new static();
84+
<?php else:?>
85+
$transformer = new <?=Inflector::singularize($relation->getClassName())?>Transformer();
86+
<?php endif;?>
87+
return $this->collection($relation, $transformer, '<?=$transformer->makeResourceKey($relation->getClassKey())?>');
88+
<?php endif;?>
89+
}
90+
<?php endforeach;?>
6691
<?php foreach ($transformer->many2Many as $relation):?>
6792

6893
public function include<?=$relation->getCamelName()?>(<?=$transformer->dbModel->getClassName()?> $model)

src/lib/items/NonDbRelation.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace cebe\yii2openapi\lib\items;
99

10+
use yii\helpers\Inflector;
11+
1012
class NonDbRelation
1113
{
1214
public const HAS_ONE = 'hasOne';
@@ -83,6 +85,16 @@ public function getClassName():?string
8385
return $this->className;
8486
}
8587

88+
public function getCamelName():string
89+
{
90+
return Inflector::camelize($this->name);
91+
}
92+
93+
public function getClassKey():string
94+
{
95+
return Inflector::camel2id($this->getClassName());
96+
}
97+
8698
/**
8799
* @return string
88100
*/

src/lib/items/Transformer.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* @property-read string $fQN
2222
* @property-read array|\cebe\yii2openapi\lib\items\AttributeRelation[] $relations
2323
* @property-read \cebe\yii2openapi\lib\items\ManyToManyRelation[]|array $many2Many
24+
* @property-read \cebe\yii2openapi\lib\items\NonDbRelation[]|array $nonDbRelations
2425
* @property-read array $availableRelations
2526
*/
2627
class Transformer extends BaseObject
@@ -87,7 +88,11 @@ public function getUniqueTransformerClasses(): array
8788
return Inflector::singularize($relation->getRelatedClassName()).'Transformer';
8889
}, $this->dbModel->many2many);
8990

90-
return array_unique(array_merge($relations, $relationsMany));
91+
$relationsNonDb = array_map(static function (NonDbRelation $relation) {
92+
return Inflector::singularize($relation->getClassName()).'Transformer';
93+
}, $this->dbModel->nonDbRelations);
94+
95+
return array_unique(array_merge($relations, $relationsMany, $relationsNonDb));
9196
}
9297

9398
/**
@@ -97,7 +102,13 @@ public function getRelations(): array
97102
{
98103
return $this->dbModel->relations;
99104
}
100-
105+
/**
106+
* @return array|\cebe\yii2openapi\lib\items\NonDbRelation[]
107+
*/
108+
public function getNonDbRelations(): array
109+
{
110+
return $this->dbModel->nonDbRelations;
111+
}
101112
/**
102113
* @return array|\cebe\yii2openapi\lib\items\ManyToManyRelation[]
103114
*/
@@ -121,7 +132,11 @@ public function getAvailableRelations(): array
121132
return Inflector::variablize($relation->name);
122133
}, $this->dbModel->many2many);
123134

124-
return array_merge($relations, $relationsMany);
135+
$relationsNonDb = array_map(static function (NonDbRelation $relation) {
136+
return Inflector::variablize($relation->getName());
137+
}, $this->dbModel->nonDbRelations);
138+
139+
return array_merge($relations, $relationsMany, $relationsNonDb);
125140
}
126141

127142
public function makeResourceKey(string $value): string

tests/specs/petstore_jsonapi/transformers/PetStatisticTransformer.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class PetStatisticTransformer extends TransformerAbstract
88
{
9-
protected $availableIncludes = ['parentPet', 'favoritePets'];
9+
protected $availableIncludes = ['parentPet', 'favoritePets', 'topDoctors'];
1010
protected $defaultIncludes = [];
1111

1212
public function transform(PetStatistic $model)
@@ -30,4 +30,11 @@ public function includeFavoritePets(PetStatistic $model)
3030
$transformer = new PetTransformer();
3131
return $this->collection($relation, $transformer, 'pets');
3232
}
33+
34+
public function includeTopDoctors(PetStatistic $model)
35+
{
36+
$relation = $model->topDoctors;
37+
$transformer = new DoctorTransformer();
38+
return $this->collection($relation, $transformer, 'doctors');
39+
}
3340
}

tests/specs/petstore_jsonapi/transformers/PetTransformer.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class PetTransformer extends TransformerAbstract
88
{
9-
protected $availableIncludes = ['duplicates'];
9+
protected $availableIncludes = ['duplicates', 'doctor'];
1010
protected $defaultIncludes = [];
1111

1212
public function transform(Pet $model)
@@ -20,4 +20,14 @@ public function includeDuplicates(Pet $model)
2020
$transformer = new static();
2121
return $this->collection($relation, $transformer, 'pets');
2222
}
23+
24+
public function includeDoctor(Pet $model)
25+
{
26+
$relation = $model->doctor;
27+
if ($relation === null) {
28+
return $this->null();
29+
}
30+
$transformer = new DoctorTransformer();
31+
return $this->item($relation, $transformer, 'doctors');
32+
}
2333
}

0 commit comments

Comments
 (0)