Commit 55216e94 authored by Guillaume Perréal's avatar Guillaume Perréal

Amélioration de la validation des resources.

parent 41217307
......@@ -54,7 +54,7 @@ abstract class AbstractType implements Type
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
public function checkType(string $expr, bool $explicit = false): string
{
throw new DomainException('cannot type-check a ' . \get_class($this));
}
......
......@@ -64,9 +64,9 @@ final class Alias extends AbstractType implements Declaration
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
public function checkType(string $expr, bool $explicit = false): string
{
return $this->target->checkType($expr);
return $this->target->checkType($expr, $explicit);
}
/**
......
......@@ -56,8 +56,13 @@ class ArrayType extends AbstractCollection
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
public function checkType(string $expr, bool $explicit = false): string
{
return sprintf('Array.isArray(%s)', $expr);
return sprintf(
'(Array.isArray(%s) && %s.findIndex(x => !%s) === -1)',
$expr,
$expr,
$this->valueType->checkType('x', $explicit)
);
}
}
......@@ -64,7 +64,7 @@ class BuiltinType extends AbstractType
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
public function checkType(string $expr, bool $explicit = false): string
{
return sprintf('(typeof %s === %s)', $expr, TypescriptHelper::quoteString($this->name));
}
......
......@@ -102,9 +102,22 @@ class AnonymousObject extends AbstractType
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
public function checkType(string $expr, bool $explicit = false): string
{
return BuiltinType::get('object')->checkType($expr);
$tests = [BuiltinType::get('object')->checkType($expr, false)];
/** @var Property $property */
foreach ($this->properties as $property) {
$tpl = $property->isNullable() ? '(!(%s in %s) || %s)' : '(%s in %s && %s)';
$tests[] = sprintf(
$tpl,
TypescriptHelper::quoteString($property->getName()),
$expr,
$property->getType()->checkType($property->getUsage($expr), false)
);
}
return '(' . implode("\n && ", $tests) . ')';
}
/**
......
......@@ -35,7 +35,7 @@ class ClassType extends AbstractHierarchicalObject
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
public function checkType(string $expr, bool $explicit = false): string
{
return sprintf('(%s instanceof %s)', $expr, $this->name);
}
......
......@@ -19,8 +19,6 @@
namespace Irstea\NgModelGeneratorBundle\Models\Types\Objects;
use Irstea\NgModelGeneratorBundle\TypescriptHelper;
/**
* Class InterfaceType.
*/
......@@ -33,25 +31,4 @@ class InterfaceType extends AbstractHierarchicalObject
{
return 'interface';
}
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
{
$tests = [parent::checkType($expr)];
/** @var Property $property */
foreach ($this->getProperties() as $property) {
if (!$property->isNullable()) {
$tests[] = sprintf(
'(%s in %s && %s)',
TypescriptHelper::quoteString($property->getName()),
$expr,
$property->getType()->checkType($property->getUsage($expr))
);
}
}
return '(' . implode("\n && ", $tests) . ')';
}
}
......@@ -142,7 +142,7 @@ public static isValidResource(data: any): data is %s {
CODE
,
$this->rootType->getUsage(),
$this->rootType->checkType('data')
$this->rootType->checkType('data', true)
);
// Extraction d'ID
......
......@@ -86,9 +86,9 @@ class Reference extends AbstractType
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
public function checkType(string $expr, bool $explicit = false): string
{
return $this->dereference()->checkType($expr);
return $this->dereference()->checkType($expr, $explicit);
}
/**
......
......@@ -73,7 +73,7 @@ final class AtType extends AbstractType
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
public function checkType(string $expr, bool $explicit = false): string
{
$names = $this->getNames();
if (\count($names) === 1) {
......
......@@ -72,7 +72,7 @@ final class IRI extends AbstractType
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
public function checkType(string $expr, bool $explicit = false): string
{
return sprintf('(%s)', implode(
' || ',
......
<?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\Types\Resources;
use Irstea\NgModelGeneratorBundle\Models\PHPClass;
use Irstea\NgModelGeneratorBundle\Models\Types\Objects\InterfaceType;
use Irstea\NgModelGeneratorBundle\Models\Types\Type;
/**
* Class Representation.
*/
final class Representation extends InterfaceType
{
/** @var PHPClass */
private $resource;
/**
* {@inheritdoc}
*/
public function __construct(PHPClass $resource, string $name, ?Type $parent, $properties = [], string $description = '', array $children = [])
{
parent::__construct($name, $parent, $properties, $description, $children);
$this->resource = $resource;
}
/**
* Get resource.
*
* @return PHPClass
*/
public function getResource(): PHPClass
{
return $this->resource;
}
/**
* @return string
*/
public function getResourceName(): string
{
return $this->resource->getBaseName();
}
/**
* {@inheritdoc}
*/
public function checkType(string $expr, bool $explicit = false): string
{
if (!$explicit) {
return sprintf('%sRepository.isValidResource(%s)', $this->getResourceName(), $expr);
}
return parent::checkType($expr, false);
}
}
......@@ -52,7 +52,7 @@ final class UUID extends AbstractType
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
public function checkType(string $expr, bool $explicit = false): string
{
return "isUUID($expr)";
}
......
......@@ -60,7 +60,7 @@ final class StringConst extends AbstractType
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
public function checkType(string $expr, bool $explicit = false): string
{
return sprintf('(%s === %s)', $expr, $this->getUsage());
}
......
......@@ -38,8 +38,9 @@ interface Type extends \IteratorAggregate, \JsonSerializable
/**
* @param string $expr
* @param bool $explicit
*
* @return string
*/
public function checkType(string $expr): string;
public function checkType(string $expr, bool $explicit = false): string;
}
......@@ -75,11 +75,11 @@ final class Union extends AbstractType
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
public function checkType(string $expr, bool $explicit = false): string
{
$checks = [];
foreach ($this->types as $type) {
$checks[$type->checkType($expr)] = true;
$checks[$type->checkType($expr, $explicit)] = true;
}
return '(' . implode(' || ', array_keys($checks)) . ')';
......
......@@ -82,7 +82,7 @@ final class Unresolved implements Type
/**
* {@inheritdoc}
*/
public function checkType(string $expr): string
public function checkType(string $expr, bool $explicit = false): string
{
return 'false /* unresolved */';
}
......
......@@ -29,12 +29,12 @@ use Irstea\NgModelGeneratorBundle\Models\Types\Alias;
use Irstea\NgModelGeneratorBundle\Models\Types\ArrayType;
use Irstea\NgModelGeneratorBundle\Models\Types\BuiltinType;
use Irstea\NgModelGeneratorBundle\Models\Types\Objects\AnonymousObject;
use Irstea\NgModelGeneratorBundle\Models\Types\Objects\InterfaceType;
use Irstea\NgModelGeneratorBundle\Models\Types\Objects\Property;
use Irstea\NgModelGeneratorBundle\Models\Types\Placeholder;
use Irstea\NgModelGeneratorBundle\Models\Types\Reference;
use Irstea\NgModelGeneratorBundle\Models\Types\Resources\AtType;
use Irstea\NgModelGeneratorBundle\Models\Types\Resources\IRI;
use Irstea\NgModelGeneratorBundle\Models\Types\Resources\Representation;
use Irstea\NgModelGeneratorBundle\Models\Types\Type;
use Irstea\NgModelGeneratorBundle\Models\Types\Union;
use Symfony\Component\PropertyInfo\Type as APIType;
......@@ -218,7 +218,7 @@ final class SerializationMapper implements TypeFactoryInterface
}
}
return new InterfaceType($repr->getName(), $parent, $properties, $desc, $children);
return new Representation($resourceClass, $repr->getName(), $parent, $properties, $desc, $children);
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment