. */ namespace Irstea\NgModelGeneratorBundle\Models\Types\Objects; use Irstea\NgModelGeneratorBundle\Models\DeclarationTrait; use Irstea\NgModelGeneratorBundle\Models\Types\Type; use Irstea\NgModelGeneratorBundle\TypescriptHelper; /** * Class Property. */ class Property implements \IteratorAggregate, \JsonSerializable { use DeclarationTrait; /** @var Type */ private $type; /** @var bool */ private $isIdentifier = false; /** @var bool */ private $isNullable = false; /** @var bool */ private $isReadonly = false; /** @var string */ private $originalName; /** * Property constructor. * * @param string $name * @param string $description * @param Type $type * @param bool $isIdentifier * @param bool $isNullable * @param bool $isReadonly * @param string|null $originalName */ public function __construct( string $name, string $description, Type $type, bool $isIdentifier = false, bool $isNullable = false, bool $isReadonly = false, string $originalName = null ) { $this->name = $name; $this->description = $description; $this->type = $type; $this->isIdentifier = $isIdentifier; $this->isNullable = $isNullable; $this->isReadonly = $isReadonly; $this->originalName = $originalName ?: $name; } /** * Get type. * * @return Type */ public function getType(): Type { return $this->type; } /** * Get description. * * @return string */ public function getDescription(): string { return $this->description; } /** * Get originalName. * * @return string */ public function getOriginalName(): string { return $this->originalName; } /** * Get isIdentifier. * * @return bool */ public function isIdentifier(): bool { return $this->isIdentifier; } /** * Get isNullable. * * @return bool */ public function isNullable(): bool { return $this->isNullable; } /** * Get isReadonly. * * @return bool */ public function isReadonly(): bool { return $this->isReadonly; } /** * {@inheritdoc} */ public function getDeclaration(): string { return sprintf( '%s%s%s: %s', $this->isReadonly ? 'readonly ' : '', $this->getObjectKey(), $this->isNullable ? '?' : '', $this->getType()->getUsage() ); } /** * @param bool $usingOriginal * * @return string */ public function getObjectKey(bool $usingOriginal = false): string { return TypescriptHelper::objectLiteralKey($this->doGetName($usingOriginal)); } /** * {@inheritdoc} */ public function getUsage(string $varName, bool $usingOriginal = false): string { return TypescriptHelper::propertyAccessor($varName, $this->doGetName($usingOriginal)); } /** * @param bool $usingOriginal * * @return string */ private function doGetName(bool $usingOriginal = false): string { return $usingOriginal ? $this->originalName : $this->name; } /** * {@inheritdoc} */ public function getIterator() { 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 * * @return bool */ public static function compare(Property $a, Property $b): bool { if ($a === $b) { return false; } if ($a->isIdentifier !== $b->isIdentifier) { return $b->isIdentifier; } if ($a->isReadonly !== $b->isReadonly) { return $b->isReadonly; } if ($a->isNullable !== $b->isNullable) { return $a->isNullable; } return $a->name > $b->name; } /** * @param string $name * @param string $description * @param Type $type * * @return static */ public static function createIdentifier(string $name, string $description, Type $type) { return new static ($name, $description, $type, true, false, true); } }