SerializationMetadata.php 4.17 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-2020 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\ClassName;
/**
 * Class SerializationMetadata.
final class SerializationMetadata implements ClassName
    /** @var ClassName */
    private $root;
    /** @var string[] */
    private $groups;
    /** @var bool */
    private $normalization;
    /** @var RepresentationMetadata[] */
    private $representations = [];
    /**
     * SerializationMetadata constructor.
     * @param ClassName                $root
     * @param string[]                 $groups
     * @param bool                     $normalization
     * @param RepresentationMetadata[] $representations
    public function __construct(ClassName $root, array $groups, bool $normalization, array $representations)
        $this->groups = $groups;
        sort($this->groups);
        $this->root = $root;
        $this->normalization = $normalization;
        foreach ($representations as $representation) {
            $this->representations[$representation->getFullName()] = $representation;
        ksort($this->representations);
    /**
     * Get groups.
     * @return string[]
    public function getGroups(): array
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
{ return $this->groups; } /** * Get root. * * @return ClassName */ public function getRoot(): ClassName { return $this->root; } /** * {@inheritdoc} */ public function getNamespace(): string { return $this->root->getNamespace(); } /** * {@inheritdoc} */ public function getBaseName(): string { return $this->root->getBaseName(); } /** * {@inheritdoc} */ public function getFullName(): string { return $this->root->getFullName(); } /** * {@inheritdoc} */ public function __toString() { return $this->root->__toString(); } /** * Get representations. * * @return RepresentationMetadata[] */ public function getRepresentations(): array { return $this->representations; } /** * @param ClassName $class * * @return bool */ public function hasRepresentationOf(ClassName $class): bool { return isset($this->representations[$class->getFullName()]); } /** * @param ClassName $class * * @return RepresentationMetadata
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
*/ public function getRepresentationOf(ClassName $class): RepresentationMetadata { return $this->representations[$class->getFullName()]; } /** * Get normalization. * * @return bool */ public function isNormalization(): bool { return $this->normalization; } /** * @return ClassName[] */ public function getRootSubClasses(): array { $subclasses = []; $rootClassName = $this->getFullName(); foreach ($this->representations as $className => $meta) { if (\is_subclass_of($className, $rootClassName, true)) { $subclasses[$className] = $meta; } } return $subclasses; } /** * {@inheritdoc} */ public function jsonSerialize() { return \get_object_vars($this); } }