Commit 8abab8fd authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Refactoring massif.

parent 150feea3
......@@ -25,7 +25,7 @@ use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
/**
* Class Context.
*/
class Context
final class Context
{
/**
* @var string
......@@ -250,6 +250,10 @@ class Context
*/
public function getSuffix(): string
{
if (empty($this->groups)) {
return $this->isNormalization() ? 'Response' : 'Request';
}
return implode('', $this->groups);
}
......@@ -258,7 +262,7 @@ class Context
*/
public function getKeySuffix(): string
{
return sha1($this->getSuffix());
return sha1($this->getOperationName() . $this->getOperationType() . ($this->isNormalization() ? 'Response' : 'Request'));
}
/**
......
<?php declare(strict_types=1);
/**
* Copyright (C) 2018 IRSTEA
* All rights reserved.
* @copyright 2018 IRSTEA
* @author guillaume.perreal
*/
namespace Irstea\NgModelGeneratorBundle\Models;
/**
* Interface Named
*/
interface Named
{
/**
* @return string
*/
public function getName(): string;
}
......@@ -48,7 +48,7 @@ trait NamedTrait
/**
* @return bool
*/
public function getReference(): string
public function getUsage(): string
{
return $this->getName();
}
......
......@@ -21,12 +21,15 @@ namespace Irstea\NgModelGeneratorBundle\Models;
use ApiPlatform\Core\Api\OperationType;
use Doctrine\Common\Inflector\Inflector;
use Irstea\NgModelGeneratorBundle\Models\Types\Type;
/**
* Class Operation.
*/
class Operation
final class Operation implements Walkable
{
use WalkableTrait;
/**
* @var string
*/
......@@ -77,8 +80,14 @@ class Operation
* @param array $pathParameters
* @param array $optionalParameters
*/
public function __construct(string $name, string $type, string $method, string $path, array $pathParameters = [], array $optionalParameters = [])
{
public function __construct(
string $name,
string $type,
string $method,
string $path,
array $pathParameters = [],
array $optionalParameters = []
) {
$this->name = $name;
$this->type = $type;
$this->method = $method;
......@@ -215,7 +224,7 @@ class Operation
if ($this->responseBody === null) {
return 'HttpResponseBase';
}
$type = $this->responseBody->getReference();
$type = $this->responseBody->getUsage();
if ($this->isCollectionOperation()) {
return "HydraCollection<$type>";
}
......@@ -336,4 +345,23 @@ class Operation
return null;
}
/**
* {@inheritdoc}
*/
protected function doWalk(callable $walker, array &$seen = []): void
{
if ($this->requestBody) {
$this->requestBody->walk($walker, $seen);
}
if ($this->responseBody) {
$this->responseBody->walk($walker, $seen);
}
foreach ($this->pathParameters as $param) {
$param->walk($walker, $seen);
}
foreach ($this->optionalParameters as $param) {
$param->walk($walker, $seen);
}
}
}
......@@ -19,12 +19,15 @@
namespace Irstea\NgModelGeneratorBundle\Models;
use Irstea\NgModelGeneratorBundle\Models\Types\Type;
/**
* Class Parameter.
*/
class Parameter implements Referencable, Declarable
class Parameter implements Walkable
{
use NamedTrait;
use WalkableTrait;
/**
* @var Type
......@@ -41,6 +44,7 @@ class Parameter implements Referencable, Declarable
*
* @param string $name
* @param Type $type
* @param bool $optional
*/
public function __construct(string $name, Type $type, bool $optional = false)
{
......@@ -50,6 +54,7 @@ class Parameter implements Referencable, Declarable
/**
* Get type.
*
* @return Type
*/
public function getType(): Type
......@@ -62,11 +67,12 @@ class Parameter implements Referencable, Declarable
*/
public function getDeclaration(): string
{
return sprintf('%s%s: %s', $this->getEscapedName(), $this->optional ? '?' : '', $this->type->getReference());
return sprintf('%s%s: %s', $this->getEscapedName(), $this->optional ? '?' : '', $this->type->getUsage());
}
/**
* Get optional.
*
* @return bool
*/
public function isOptional(): bool
......@@ -75,11 +81,10 @@ class Parameter implements Referencable, Declarable
}
/**
* @inheritDoc
* {@inheritdoc}
*/
public function getDescription(): string
protected function doWalk(callable $walker, array &$seen = []): void
{
return '';
$this->type->walk($walker, $seen);
}
}
......@@ -19,17 +19,20 @@
namespace Irstea\NgModelGeneratorBundle\Models;
use Irstea\NgModelGeneratorBundle\Models\Types\Type;
/**
* Class Property.
*/
class Property implements Referencable, Declarable
final class Property implements Walkable
{
use NamedTrait;
use WalkableTrait;
/** @var string */
private $description;
/** @var Type|null */
/** @var Type */
private $type;
/** @var bool */
......@@ -54,7 +57,7 @@ class Property implements Referencable, Declarable
public function __construct(
string $name,
string $description,
Type $type = null,
Type $type,
bool $isIdentifier = false,
bool $isNullable = false,
bool $isReadonly = false
......@@ -70,23 +73,13 @@ class Property implements Referencable, Declarable
/**
* Get type.
*
* @return Type|null
* @return Type
*/
public function getType(): ?Type
public function getType(): Type
{
return $this->type;
}
/**
* Set type.
*
* @param Type|null $type
*/
public function setType(Type $type): void
{
$this->type = $type;
}
/**
* Get description.
*
......@@ -147,7 +140,7 @@ class Property implements Referencable, Declarable
$this->isReadonly ? 'readonly ' : '',
$this->getEscapedName(),
$this->isNullable ? '?' : '',
$this->type->getReference()
$this->type->getUsage()
);
}
......@@ -189,4 +182,12 @@ class Property implements Referencable, Declarable
return $a->name > $b->name;
}
/**
* {@inheritdoc}
*/
protected function doWalk(callable $walker, array &$seen = []): void
{
$this->type->walk($walker, $seen);
}
}
<?php declare(strict_types=1);
/*
* irstea/ng-model-generator-bundle generates Typescript interfaces for Angular using api-platform metadata.
* Copyright (C) 2018 IRSTEA
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\NgModelGeneratorBundle\Models;
use Irstea\NgModelGeneratorBundle\TypeFactoryInterface;
/**
* Class Ref.
*/
class Reference extends Type implements Declarable
{
/**
* @var Type|null
*/
private $target = null;
/**
* @var string
*/
private $key;
/**
* Reference constructor.
*
* @param Type|null $target
* @param string $key
*/
public function __construct(string $key, Type $target = null)
{
$this->key = $key;
$this->target = $target;
}
/**
* {@inheritdoc}
*/
public function getReference(): string
{
return $this->target ? $this->target->getReference() : 'never';
}
/**
* {@inheritdoc}
*/
public function hasSimpleReference(): bool
{
return $this->target !== null && $this->target->hasSimpleReference();
}
/**
* {@inheritdoc}
*/
public function getComment(): string
{
return $this->target ? $this->target->getComment() : 'unresolved reference';
}
/**
* Get target.
* @return Type|null
*/
public function getTarget(): ?Type
{
return $this->target;
}
/**
* Set target.
*
* @param Type $target
*/
public function resolve(Type $target): void
{
$this->target = $target;
}
/**
* {@inheritDoc}
*/
public function optimized(TypeFactoryInterface $factory): Type
{
if ($this->target) {
$this->target = $this->target->optimized($factory);
}
return parent::optimized($factory);
}
/**
* @inheritDoc
*/
public function getDeclaration(): string
{
if ($this->target instanceof Declarable) {
return $this->target->getDeclaration();
}
if ($this->target instanceof Referencable) {
return $this->target->getReference();
}
return 'never;';
}
/**
* @inheritDoc
*/
public function getDescription(): string
{
if ($this->target instanceof Declarable) {
return $this->target->getDescription();
}
if ($this->target) {
return sprintf('reference to undeclarable type %s (%s)', $this->target->getReference(), $this->key);
}
return sprintf('unresolved reference (%s)', $this->key);
}
}
......@@ -22,8 +22,10 @@ namespace Irstea\NgModelGeneratorBundle\Models;
/**
* Class Repository.
*/
class Repository
final class Repository implements Walkable
{
use WalkableTrait;
/**
* @var string
*/
......@@ -72,6 +74,16 @@ class Repository
$this->operations[] = $operation;
}
/**
* {@inheritdoc}
*/
protected function doWalk(callable $walker, array &$seen = []): void
{
foreach ($this->operations as $op) {
$op->walk($walker, $seen);
}
}
/**
* @param self $a
* @param self $b
......
......@@ -19,15 +19,18 @@
namespace Irstea\NgModelGeneratorBundle\Models;
use Irstea\NgModelGeneratorBundle\Models\Types\Object;
use Irstea\NgModelGeneratorBundle\Models\Types\Type;
/**
* Class TransformedParameter.
*/
class TransformedParameter extends Parameter
final class TransformedParameter extends Parameter
{
/**
* {@inheritdoc}
*/
public function __construct(string $name, AnonymousObject $type, bool $optional = false)
public function __construct(string $name, Type $type, bool $optional = false)
{
parent::__construct($name, $type, $optional);
}
......@@ -39,7 +42,7 @@ class TransformedParameter extends Parameter
{
$components = [];
/** @var AnonymousObject $type */
/** @var object $type */
$type = $this->getType();
$paramName = $this->getName();
......@@ -48,7 +51,7 @@ class TransformedParameter extends Parameter
$rhs = $propertyName !== $property->getName() ? "${paramName}[${propertyName}]" : "${paramName}.${propertyName}";
if ($property->getType()->getReference() !== 'string') {
if ($property->getType()->getUsage() !== 'string') {
$rhs = "''+${rhs}";
}
......
<?php declare(strict_types=1);
/**
* Copyright (C) 2018 IRSTEA
* All rights reserved.
* @copyright 2018 IRSTEA
* @author guillaume.perreal
*/
namespace Irstea\NgModelGeneratorBundle\Models;
/**
* Class TypeAlias
*/
class TypeAlias extends NamedType implements Declarable
{
/**
* @var Type
*/
private $target;
/**
* @var string
*/
private $description;
/**
* TypeAlias constructor.
*
* @param string $name
* @param Type $target
* @param string $description
*/
public function __construct(string $name, Type $target, string $description = '')
{
parent::__construct($name);
$this->target = $target;
$this->description = $description;
}
/**
* @inheritDoc
*/
public function getDeclaration(): string
{
return sprintf('type %s = %s;', $this->getName(), $this->target ? $this->target->getReference() : 'never');
}
/**
* @inheritDoc
*/
public function getDescription(): string
{
return $this->description;
}
}
......@@ -17,13 +17,35 @@
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\NgModelGeneratorBundle\Models;
namespace Irstea\NgModelGeneratorBundle\Models\Types;
use Irstea\NgModelGeneratorBundle\Models\NamedTrait;
/**
* Class BuiltinType.
*/
class BuiltinType extends NamedType
final class BuiltinType extends Type
{
use NamedTrait;
/**
* BuiltinType constructor.
*
* @param string $name
*/
public function __construct(string $name)
{
$this->setName($name);
}
/**
* {@inheritdoc}
*/
public function getUsage(): string
{
return $this->getName();
}
/**
* {@inheritdoc}
*/
......
......@@ -17,56 +17,31 @@
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\NgModelGeneratorBundle\Models;
namespace Irstea\NgModelGeneratorBundle\Models\Types;
/**
* Class Collection.
*/
class Collection extends Type
final class Collection extends Type
{
/** @var Type */
private $itemType;
use DecoratorTrait;
/**
* Collection constructor.
*
* @param Type $itemType
* @param Type $item
*/
public function __construct(Type $itemType)
public function __construct(Type $item)
{
$this->itemType = $itemType;
$this->setDecorated($item);
}
/**
* {@inheritdoc}
*/
public function getReference(): string
public function getUsage(): string
{
return sprintf($this->itemType->hasSimpleReference() ? '%s[]' : 'Array<%s>', $this->itemType->getReference());
}
/**
* {@inheritdoc}
*/
public function hasSimpleReference(): bool
{
return false;
}
/**
* {@inheritdoc}
*/
public function getComment(): string
{
return $this->itemType->getComment();
}
/**
* @return Type
*/
public function getItemType(): Type
{
return $this->itemType;
return sprintf('Array<%s>', $this->getDecorated()->getUsage());
}
/**
......@@ -77,7 +52,7 @@ class Collection extends Type
public function mergeWith(Type $t): Type
{
if ($t instanceof self) {
return new self($this->itemType->mergeWith($t->itemType));
return new self($this->getDecorated()->mergeWith($t->getDecorated()));
}
return parent::mergeWith($t);
......