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

Inclut les métadonnées des modèles dans le typescript.

parent 0a9e2145
......@@ -130,13 +130,19 @@ final class ModelGenerator
return $this->twigEnv->render(
'@NgModelGenerator/typescript_models.ts.twig',
[
'title' => $this->documentation->getTitle(),
'version' => $this->documentation->getVersion(),
'description' => $this->documentation->getDescription() ?: '',
'resources' => $this->resources,
'repositories' => $repositories,
'declarations' => $declarations,
'iriPatterns' => $iriPatterns,
'title' => $this->documentation->getTitle(),
'version' => $this->documentation->getVersion(),
'description' => $this->documentation->getDescription() ?: '',
'resources' => $this->resources,
'repositories' => $repositories,
'declarations' => $declarations,
'iriPatterns' => $iriPatterns,
'metadata' => array_filter(
$declarations,
function (Type $t): bool {
return $t instanceof Representation;
}
),
]
);
}
......
......@@ -66,4 +66,12 @@ abstract class AbstractType implements Type
{
throw new DomainException('cannot type-check a ' . \get_class($this));
}
/**
* @return string
*/
public function jsonSerialize()
{
return $this->getUsage();
}
}
......@@ -75,4 +75,12 @@ final class Collection extends AbstractType
{
yield $this->itemType;
}
/**
* @return array
*/
public function jsonSerialize()
{
return ['type' => 'Array', 'itemType' => $this->itemType];
}
}
......@@ -169,6 +169,18 @@ abstract class AbstractHierarchicalObject extends AnonymousObject implements Hie
yield from parent::getIterator();
}
/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return [
'name' => $this->getName(),
'description' => $this->getDescription(),
'properties' => $this->getAllProperties(),
];
}
/**
* @return string
*/
......
......@@ -181,4 +181,12 @@ class AnonymousObject extends AbstractType implements Object
{
yield from $this->getProperties();
}
/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return ['properties' => $this->properties];
}
}
......@@ -26,7 +26,7 @@ use Irstea\NgModelGeneratorBundle\TypescriptHelper;
/**
* Class Property.
*/
class Property implements \IteratorAggregate
class Property implements \IteratorAggregate, \JsonSerializable
{
use DeclarationTrait;
......@@ -160,6 +160,21 @@ class Property implements \IteratorAggregate
yield $this->getType();
}
/**
* {@inheritDoc}
*/
public function jsonSerialize()
{
return [
'name' => $this->name,
'description' => $this->description,
'type' => $this->type,
'identifier' => $this->isIdentifier,
'nullable' => $this->isNullable,
'readonly' => $this->isReadonly,
];
}
/**
* @param Property $a
* @param Property $b
......
......@@ -106,6 +106,20 @@ final class Repository extends ClassType
return $this->operations;
}
/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return array_merge(
parent::jsonSerialize(),
[
'resource' => $this->resourceName,
'operations' => $this->operations,
]
);
}
/**
* @param self $a
* @param self $b
......
......@@ -116,5 +116,16 @@ final class Representation extends InterfaceType
return array_keys($names);
}
/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return
[
'resource' => $this->getShortName(),
] +
parent::jsonSerialize();
}
}
......@@ -79,6 +79,14 @@ final class CachedClientCall implements ClientCall
return [$body, sprintf($this->decorator, $expr)];
}
/**
* @return ClientCall
*/
public function jsonSerialize()
{
return $this->innerCall;
}
/**
* {@inheritdoc}
*/
......
......@@ -22,7 +22,7 @@ namespace Irstea\NgModelGeneratorBundle\Models\Types\Operations;
/**
* Class ClientCall.
*/
interface ClientCall extends \IteratorAggregate
interface ClientCall extends \IteratorAggregate, \JsonSerializable
{
/**
* @return string
......
......@@ -206,4 +206,18 @@ final class DirectClientCall implements ClientCall
return [$body, $outputRef];
}
/**
* @return array
*/
public function jsonSerialize()
{
return [
'method' => $this->httpMethod,
'path' => $this->path,
'filters' => $this->queryParameters,
'request' => $this->requestBody,
'response' => $this->responseBody,
];
}
}
......@@ -25,7 +25,7 @@ use Irstea\NgModelGeneratorBundle\TypescriptHelper;
/**
* Class Operation.
*/
final class Operation implements \IteratorAggregate
final class Operation implements \IteratorAggregate, \JsonSerializable
{
use DeclarationTrait;
......@@ -87,4 +87,12 @@ final class Operation implements \IteratorAggregate
{
yield $this->clientCall;
}
/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return array_merge(['name' => $this->name], $this->clientCall->jsonSerialize());
}
}
......@@ -25,7 +25,7 @@ use Irstea\NgModelGeneratorBundle\Models\Types\Type;
/**
* Class Parameter.
*/
class Parameter implements \IteratorAggregate
class Parameter implements \IteratorAggregate, \JsonSerializable
{
use NamedTrait;
......@@ -87,4 +87,16 @@ class Parameter implements \IteratorAggregate
{
yield $this->type;
}
/**
* @return array|mixed
*/
public function jsonSerialize()
{
return [
'name' => $this->name,
'type' => $this->type,
'required' => !$this->optional,
];
}
}
......@@ -24,7 +24,7 @@ use Irstea\NgModelGeneratorBundle\TypescriptHelper;
/**
* Class PathTemplate.
*/
class PathTemplate implements Path
class PathTemplate implements Path, \JsonSerializable
{
/** @var string */
private $template;
......@@ -85,4 +85,16 @@ class PathTemplate implements Path
{
return $this->parameters;
}
/**
* @return mixed|array
*/
public function jsonSerialize()
{
return [
'template' => $this->template,
'pattern' => $this->pattern,
'parameters' => $this->parameters,
];
}
}
......@@ -24,7 +24,7 @@ use Irstea\NgModelGeneratorBundle\TypescriptHelper;
/**
* Class PlainPath.
*/
class PlainPath implements Path
class PlainPath implements Path, \JsonSerializable
{
/** @var string */
private $path;
......@@ -62,4 +62,12 @@ class PlainPath implements Path
{
return preg_quote($this->path, '/');
}
/**
* @return mixed|string
*/
public function jsonSerialize()
{
return $this->path;
}
}
......@@ -19,6 +19,8 @@
namespace Irstea\NgModelGeneratorBundle\Models\Types;
use Irstea\NgModelGeneratorBundle\Models\Declaration;
/**
* Class Ref.
*/
......@@ -97,6 +99,18 @@ class Reference extends AbstractType
yield $this->target;
}
/**
* @return mixed
*/
public function jsonSerialize()
{
if ($this->target instanceof Declaration) {
return $this->target->getName();
}
return $this->target;
}
/**
* {@inheritdoc}
*/
......
......@@ -22,7 +22,7 @@ namespace Irstea\NgModelGeneratorBundle\Models\Types;
/**
* interface Type.
*/
interface Type extends \IteratorAggregate
interface Type extends \IteratorAggregate, \JsonSerializable
{
/**
* @return string
......
......@@ -123,4 +123,15 @@ final class Union extends AbstractType
{
yield from $this->types;
}
/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return [
'type' => 'union',
'types' => $this->types,
];
}
}
......@@ -152,4 +152,10 @@ export const REPOSITORIES = [
{{ repo.usage }},
{% endfor %}
];
/**
* Métadonnées des modèles.
*/
export const MODEL_METADATA = {{ metadata | json_encode(constant('JSON_PRETTY_PRINT') + constant('JSON_UNESCAPED_UNICODE') + constant('JSON_UNESCAPED_SLASHES')) }};
{% endautoescape -%}
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