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

23
namespace Irstea\NgModelGeneratorBundle\Models\Types\Objects;
24

25
use Irstea\NgModelGeneratorBundle\Models\DeclarationTrait;
26
use Irstea\NgModelGeneratorBundle\Models\Types\Type;
Guillaume Perréal's avatar
Guillaume Perréal committed
27
use Irstea\NgModelGeneratorBundle\TypescriptHelper;
Guillaume Perréal's avatar
Guillaume Perréal committed
28

29
30
31
/**
 * Class Property.
 */
32
class Property implements \IteratorAggregate, \JsonSerializable
33
{
34
    use DeclarationTrait;
35

Guillaume Perréal's avatar
Guillaume Perréal committed
36
    /** @var Type */
37
38
39
40
41
42
43
44
45
46
47
    private $type;

    /** @var bool */
    private $isIdentifier = false;

    /** @var bool */
    private $isNullable = false;

    /** @var bool */
    private $isReadonly = false;

48
49
50
    /** @var string */
    private $originalName;

51
52
53
54
55
56
    /**
     * Property constructor.
     */
    public function __construct(
        string $name,
        string $description,
Guillaume Perréal's avatar
Guillaume Perréal committed
57
        Type $type,
58
59
        bool $isIdentifier = false,
        bool $isNullable = false,
60
61
        bool $isReadonly = false,
        string $originalName = null
62
    ) {
Guillaume Perréal's avatar
Guillaume Perréal committed
63
        $this->name = $name;
64
65
66
67
68
        $this->description = $description;
        $this->type = $type;
        $this->isIdentifier = $isIdentifier;
        $this->isNullable = $isNullable;
        $this->isReadonly = $isReadonly;
69
        $this->originalName = $originalName ?: $name;
70
71
72
73
74
    }

    /**
     * Get type.
     */
Guillaume Perréal's avatar
Guillaume Perréal committed
75
    public function getType(): Type
76
77
78
79
80
81
82
83
84
85
86
87
    {
        return $this->type;
    }

    /**
     * Get description.
     */
    public function getDescription(): string
    {
        return $this->description;
    }

88
89
90
91
92
93
94
95
    /**
     * Get originalName.
     */
    public function getOriginalName(): string
    {
        return $this->originalName;
    }

96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    /**
     * Get isIdentifier.
     */
    public function isIdentifier(): bool
    {
        return $this->isIdentifier;
    }

    /**
     * Get isNullable.
     */
    public function isNullable(): bool
    {
        return $this->isNullable;
    }

    /**
     * Get isReadonly.
     */
    public function isReadonly(): bool
    {
        return $this->isReadonly;
    }

    /**
     * {@inheritdoc}
     */
    public function getDeclaration(): string
    {
        return sprintf(
            '%s%s%s: %s',
            $this->isReadonly ? 'readonly ' : '',
128
            $this->getObjectKey(),
129
            $this->isNullable ? '?' : '',
130
            $this->getType()->getUsage()
131
132
133
        );
    }

134
    public function getObjectKey(bool $usingOriginal = false): string
135
    {
136
        return TypescriptHelper::objectLiteralKey($this->doGetName($usingOriginal));
137
138
    }

139
140
141
    /**
     * {@inheritdoc}
     */
142
    public function getUsage(string $varName, bool $usingOriginal = false): string
143
    {
144
        return TypescriptHelper::propertyAccessor($varName, $this->doGetName($usingOriginal));
145
146
    }

147
148
149
150
151
    private function doGetName(bool $usingOriginal = false): string
    {
        return $usingOriginal ? $this->originalName : $this->name;
    }

152
153
154
155
156
157
158
159
    /**
     * {@inheritdoc}
     */
    public function getIterator()
    {
        yield $this->getType();
    }

160
    /**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
161
     * {@inheritdoc}
162
163
164
165
166
167
168
169
170
171
172
173
174
     */
    public function jsonSerialize()
    {
        return [
            'name'        => $this->name,
            'description' => $this->description,
            'type'        => $this->type,
            'identifier'  => $this->isIdentifier,
            'nullable'    => $this->isNullable,
            'readonly'    => $this->isReadonly,
        ];
    }

Guillaume Perréal's avatar
PHP CS.    
Guillaume Perréal committed
175
    public static function compare(self $a, self $b): bool
176
    {
Guillaume Perréal's avatar
Guillaume Perréal committed
177
178
179
        if ($a === $b) {
            return false;
        }
180
181
182
183
184
185
186
187
188
189
190
191
        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;
    }
192

193
194
195
196
197
    /**
     * @return static
     */
    public static function createIdentifier(string $name, string $description, Type $type)
    {
198
        return new self($name, $description, $type, true, false, true);
199
    }
200
}