AnonymousObject.php 4.75 KiB
<?php declare(strict_types=1);
/*
 * This file is part of "irstea/ng-model-generator-bundle".
 * "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\Objects;
use Irstea\NgModelGeneratorBundle\Exceptions\DomainException;
use Irstea\NgModelGeneratorBundle\Models\Types\AbstractType;
use Irstea\NgModelGeneratorBundle\Models\Types\BuiltinType;
use Irstea\NgModelGeneratorBundle\TypescriptHelper;
/**
 * Class AnonymousObject.
class AnonymousObject extends AbstractType
    /**
     * @var Property[]
    private $properties = [];
    /**
     * Resource constructor.
     * @param Property[] $properties
    public function __construct(array $properties)
        foreach ($properties as $property) {
            $name = $property->getName();
            if (isset($this->properties[$name])) {
                throw new DomainException("duplicate object property: $name");
            $this->properties[$name] = $property;
        uasort($this->properties, [Property::class, 'compare']);
    /**
     * @return Property[]
    public function getProperties(): array
        return $this->properties;
    /**
     * @param string $name
     * @return bool
    public function hasProperty(string $name): bool
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
$props = $this->getProperties(); return isset($props[$name]); } /** * @param string $name * * @return Property */ public function getProperty(string $name): Property { $props = $this->getProperties(); return $props[$name]; } /** * {@inheritdoc} */ public function getUsage(): string { return $this->getDeclarationBody(false); } /** * {@inheritdoc} */ public function castToStringOrStringArray(string $expr): string { return sprintf('JSON.stringify(%s)', $expr); } /** * {@inheritdoc} */ public function checkType(string $expr, bool $explicit = false): string { $tests = [BuiltinType::get('object')->checkType($expr, false)]; /** @var Property $property */ foreach ($this->properties as $property) { $tpl = $property->isNullable() ? '(!(%s) || %s)' : '(%s && %s)'; $tests[] = sprintf( $tpl, TypescriptHelper::propertyTestor($expr, $property->getName()), $expr, $property->getType()->checkType($property->getUsage($expr), false) ); } return '(' . implode("\n && ", $tests) . ')'; } /** * {@inheritdoc} */ public function getDeclaration(): string { return trim(trim($this->getDeclarationHeader()) . ' ' . $this->getDeclarationBody(true)); } /** * @return string */ protected function getDeclarationHeader(): string { return ''; }
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
/** * @return string */ private function getDeclarationBody(bool $multiline): string { $body = array_merge( $this->getPropertyDeclarations(), $this->getMethodDeclarations() ); if ($multiline) { return "{\n" . TypescriptHelper::indent(implode("\n", $body)) . "\n}"; } return '{ ' . implode(' ', $body) . ' }'; } /** * @return array */ protected function getPropertyDeclarations(): array { return array_map( function (Property $p): string { return $p->getDeclaration() . ';'; }, $this->getProperties() ); } /** * @param bool $multiline * * @return array */ protected function getMethodDeclarations(): array { return []; } /** * {@inheritdoc} */ public function getIterator() { yield from $this->getProperties(); } /** * {@inheritdoc} */ public function jsonSerialize() { return \get_object_vars($this); } }