Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

add transformers many2many relations support #40

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/generator/default/transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public function include<?=$relation->getCamelName()?>(<?=$transformer->dbModel->
return $this->collection($relation, $transformer, '<?=$transformer->makeResourceKey($relation->getClassKey())?>');
<?php endif;?>
}
<?php endforeach;
endif;?>
<?php endforeach;?>
<?php foreach ($transformer->many2Many as $relation):?>

public function include<?=$relation->getCamelName()?>(<?=$transformer->dbModel->getClassName()?> $model)
{
$relation = $model-><?=Inflector::variablize($relation->name)?>;
$transformer = new <?=Inflector::singularize($relation->getRelatedClassName())?>Transformer();
return $this->collection($relation, $transformer, '<?=$transformer->makeResourceKey($relation->relatedSchemaName)?>');
}
<?php endforeach;?>
<?php endif;?>
}
26 changes: 18 additions & 8 deletions src/lib/items/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
use yii\helpers\Inflector;

/**
* @property-read string $name
* @property-read array $defaultRelations
* @property-read string $modelFQN
* @property-read string $fQN
* @property-read array|\cebe\yii2openapi\lib\items\AttributeRelation[] $relations
* @property-read array $availableRelations
* @property-read string $name
* @property-read array $defaultRelations
* @property-read string $modelFQN
* @property-read string $fQN
* @property-read array|\cebe\yii2openapi\lib\items\AttributeRelation[] $relations
* @property-read \cebe\yii2openapi\lib\items\ManyToManyRelation[]|array $many2Many
* @property-read array $availableRelations
*/
class Transformer extends BaseObject
{
Expand Down Expand Up @@ -77,18 +78,27 @@ public function getRelations(): array
return $this->dbModel->relations;
}

/**
* @return array|\cebe\yii2openapi\lib\items\ManyToManyRelation[]
*/
public function getMany2Many(): array
{
return $this->dbModel->many2many;
}

public function getDefaultRelations(): array
{
return [];
}

public function getAvailableRelations(): array
{
return \array_keys($this->dbModel->relations);
return array_merge(array_keys($this->dbModel->relations), array_keys($this->dbModel->many2many));
}

public function makeResourceKey(string $value): string
{
return $this->singularResourceKey ? Inflector::singularize($value): Inflector::pluralize($value);
$value = $this->singularResourceKey ? Inflector::singularize($value): Inflector::pluralize($value);
return strtolower(Inflector::camel2id($value, '_'));
}
}
39 changes: 37 additions & 2 deletions tests/specs/many2many.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ info:
servers:
- url: http://blog.dummy.io/v2
paths:
/some:
/posts:
get:
summary: dummy path
operationId: showDummy
responses:
'200':
description: Some response
$ref: '#/components/responses/PostListResponse'
components:
schemas:
Post:
Expand Down Expand Up @@ -86,3 +86,38 @@ components:
$ref: '#/components/schemas/Photo'
post:
$ref: '#/components/schemas/Post'
_PostResource:
type: object
properties:
id:
type: integer
example: 1
type:
type: string
attributes:
$ref: '#/components/schemas/Post'
links:
type: object
relationships:
type: object
responses:
PostResponse:
description: Single post info
content:
application/vnd.api+json:
schema:
type: object
properties:
data:
$ref: '#/components/schemas/_PostResource'
PostListResponse:
description: Posts List
content:
application/vnd.api+json:
schema:
type: object
properties:
data:
type: array
items:
$ref: '#/components/schemas/_PostResource'