An error occurred while loading the file. Please try again.
-
Grelot Frederic authored
ref #4
fe69a907
<?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\Metadata;
use Irstea\NgModelGeneratorBundle\Models\HasName;
use Symfony\Component\PropertyInfo\Type;
/**
* Class PropertyMetadata.
*/
class PropertyMetadata implements \JsonSerializable, HasName
{
/** @var string */
private $name;
/** @var string */
private $description;
/** @var Type */
private $type;
/** @var bool */
private $identifier;
/** @var bool */
private $readable;
/** @var bool */
private $writable;
/** @var bool */
private $initializable;
/** @var bool */
private $link;
/** @var bool */
private $embedded;
/**
* PropertyMetadata constructor.
*
* @param string $name
* @param string $description
* @param Type $type
* @param bool $identifier
* @param bool $readable
* @param bool $writable
* @param bool $initializable
* @param bool $link
* @param bool $embedded
*/
public function __construct(
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
string $name,
string $description,
Type $type,
bool $identifier,
bool $readable,
bool $writable,
bool $initializable,
bool $link,
bool $embedded
) {
$this->name = $name;
$this->description = $description;
$this->type = $type;
$this->identifier = $identifier;
$this->readable = $readable;
$this->writable = $writable;
$this->initializable = $initializable;
$this->link = $link;
$this->embedded = $embedded;
}
/**
* Get name.
*
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* Get description.
*
* @return string
*/
public function getDescription(): string
{
return $this->description;
}
/**
* Get type.
*
* @return Type
*/
public function getType(): Type
{
return $this->type;
}
/**
* @return Type
*/
public function getLeafType(): Type
{
$type = $this->type;
while ($type && $type->isCollection() && $type->getCollectionValueType()) {
$type = $type->getCollectionValueType();
}
return $type;
}
/**
* Get identifier.
*
* @return bool
*/
public function isIdentifier(): bool
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
{
return $this->identifier;
}
/**
* Get readable.
*
* @return bool
*/
public function isReadable(): bool
{
return $this->readable;
}
/**
* Get writable.
*
* @return bool
*/
public function isWritable(): bool
{
return $this->writable;
}
/**
* @return bool
*/
public function isNullable(): bool
{
return $this->type ? $this->type->isNullable() : true;
}
/**
* Get initializable.
*
* @return bool
*/
public function isInitializable(): bool
{
return $this->initializable;
}
/**
* Get link.
*
* @return bool
*/
public function isLink(): bool
{
return $this->link;
}
/**
* Get embedded.
*
* @return bool
*/
public function isEmbedded(): bool
{
return $this->embedded;
}
/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
$vars = \get_object_vars($this);
$vars['type'] = $this->serializeType($this->type);
unset($vars['resource']);
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
return $vars;
}
/**
* @param null|Type $type
*
* @return array|null
*/
private function serializeType(?Type $type)
{
if (!$type) {
return null;
}
if ($type->getCollectionValueType()) {
return [
'key_type' => $this->serializeType($type->getCollectionKeyType()),
'value_type' => $this->serializeType($type->getCollectionValueType()),
'nullable' => $type->isNullable(),
];
}
if ($type->getClassName()) {
return [
'class_name' => $type->getClassName(),
'nullable' => $type->isNullable(),
];
}
return [
'builtin_type' => $type->getBuiltinType(),
'nullable' => $type->isNullable(),
];
}
}