TypeFactory.php 2.87 KB
Newer Older
1
2
<?php declare(strict_types=1);
/*
Guillaume Perréal's avatar
Guillaume Perréal committed
3
4
5
 * This file is part of "irstea/ng-model-generator-bundle".
 *
 * "irstea/ng-model-generator-bundle" generates Typescript interfaces for Angular using api-platform metadata.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * 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;

24
25
use Irstea\NgModelGeneratorBundle\Exceptions\TypeAlreadyExistsException;
use Irstea\NgModelGeneratorBundle\Exceptions\TypeNotFoundException;
26
use Irstea\NgModelGeneratorBundle\Models\Types\Deferred;
Guillaume Perréal's avatar
Guillaume Perréal committed
27
28
use Irstea\NgModelGeneratorBundle\Models\Types\Reference;
use Irstea\NgModelGeneratorBundle\Models\Types\Type;
29
30
31
32

/**
 * Class TypeFactory.
 */
33
final class TypeFactory implements TypeFactoryInterface, \IteratorAggregate
34
{
35
36
37
    /** @var Type[] */
    private $types = [];

38
    /**
39
     * {@inheritdoc}
40
     */
41
42
43
44
45
    public function withContext(?ContextInterface $context): TypeFactoryInterface
    {
        // On utilise pas le contexte dans cette classe, donc en changer ne change rien.
        return $this;
    }
46

Guillaume Perréal's avatar
Guillaume Perréal committed
47
48
49
    /**
     * {@inheritdoc}
     */
50
    public function getIterator()
Guillaume Perréal's avatar
Guillaume Perréal committed
51
    {
52
        yield from $this->types;
Guillaume Perréal's avatar
Guillaume Perréal committed
53
    }
54

Guillaume Perréal's avatar
Guillaume Perréal committed
55
56
57
58
59
60
61
    /**
     * @param string $name
     *
     * @return bool
     */
    public function has(string $name): bool
    {
62
        return isset($this->types[$name]);
Guillaume Perréal's avatar
Guillaume Perréal committed
63
    }
64

Guillaume Perréal's avatar
Guillaume Perréal committed
65
66
67
68
69
70
71
    /**
     * @param string $name
     *
     * @return Type
     */
    public function get(string $name): Type
    {
72
        if (!isset($this->types[$name])) {
73
            throw new TypeNotFoundException("unknown type ${name}");
Guillaume Perréal's avatar
Guillaume Perréal committed
74
        }
75

76
        return $this->types[$name];
77
78
79
    }

    /**
Guillaume Perréal's avatar
Guillaume Perréal committed
80
     * {@inheritdoc}
81
     */
82
    public function add(string $name, Type $type): void
83
    {
84
        if (isset($this->types[$name])) {
85
            throw new TypeAlreadyExistsException("type already exists: ${name}");
Guillaume Perréal's avatar
Guillaume Perréal committed
86
87
        }

88
        $this->types[$name] = $type;
89
90
91
    }

    /**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
92
     * {@inheritdoc}
93
     */
94
    public function defer(string $name): Deferred
95
    {
96
97
98
        if (isset($this->types[$name]) && $this->types[$name] instanceof Deferred) {
            /* @noinspection PhpIncompatibleReturnTypeInspection */
            return $this->types[$name];
Guillaume Perréal's avatar
Guillaume Perréal committed
99
        }
100
101
        $ref = new Reference($name);
        $this->add($name, $ref);
Guillaume Perréal's avatar
Guillaume Perréal committed
102

103
        return $ref;
104
    }
105
}