Property.php 5.6 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
 * 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/>.
20
21
 */

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

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

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

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

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

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

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

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

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

    /**
     * Get type.
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
81
     *
Guillaume Perréal's avatar
Guillaume Perréal committed
82
     * @return Type
83
     */
Guillaume Perréal's avatar
Guillaume Perréal committed
84
    public function getType(): Type
85
86
87
88
89
90
    {
        return $this->type;
    }

    /**
     * Get description.
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
91
     *
92
93
94
95
96
97
98
     * @return string
     */
    public function getDescription(): string
    {
        return $this->description;
    }

99
100
101
102
103
104
105
106
107
108
    /**
     * Get originalName.
     *
     * @return string
     */
    public function getOriginalName(): string
    {
        return $this->originalName;
    }

109
110
    /**
     * Get isIdentifier.
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
111
     *
112
113
114
115
116
117
118
119
120
     * @return bool
     */
    public function isIdentifier(): bool
    {
        return $this->isIdentifier;
    }

    /**
     * Get isNullable.
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
121
     *
122
123
124
125
126
127
128
129
130
     * @return bool
     */
    public function isNullable(): bool
    {
        return $this->isNullable;
    }

    /**
     * Get isReadonly.
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
131
     *
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
     * @return bool
     */
    public function isReadonly(): bool
    {
        return $this->isReadonly;
    }

    /**
     * {@inheritdoc}
     */
    public function getDeclaration(): string
    {
        return sprintf(
            '%s%s%s: %s',
            $this->isReadonly ? 'readonly ' : '',
147
            $this->getObjectKey(),
148
            $this->isNullable ? '?' : '',
149
            $this->getType()->getUsage()
150
151
152
        );
    }

153
    /**
154
155
     * @param bool $usingOriginal
     *
156
157
     * @return string
     */
158
    public function getObjectKey(bool $usingOriginal = false): string
159
    {
160
        return TypescriptHelper::objectLiteralKey($this->doGetName($usingOriginal));
161
162
    }

163
164
165
    /**
     * {@inheritdoc}
     */
166
    public function getUsage(string $varName, bool $usingOriginal = false): string
167
    {
168
        return TypescriptHelper::propertyAccessor($varName, $this->doGetName($usingOriginal));
169
170
    }

171
172
173
174
175
176
177
178
179
180
    /**
     * @param bool $usingOriginal
     *
     * @return string
     */
    private function doGetName(bool $usingOriginal = false): string
    {
        return $usingOriginal ? $this->originalName : $this->name;
    }

181
182
183
184
185
186
187
188
    /**
     * {@inheritdoc}
     */
    public function getIterator()
    {
        yield $this->getType();
    }

189
    /**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
190
     * {@inheritdoc}
191
192
193
194
195
196
197
198
199
200
201
202
203
     */
    public function jsonSerialize()
    {
        return [
            'name'        => $this->name,
            'description' => $this->description,
            'type'        => $this->type,
            'identifier'  => $this->isIdentifier,
            'nullable'    => $this->isNullable,
            'readonly'    => $this->isReadonly,
        ];
    }

204
205
206
207
208
209
210
211
    /**
     * @param Property $a
     * @param Property $b
     *
     * @return bool
     */
    public static function compare(Property $a, Property $b): bool
    {
Guillaume Perréal's avatar
Guillaume Perréal committed
212
213
214
        if ($a === $b) {
            return false;
        }
215
216
217
218
219
220
221
222
223
224
225
226
        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;
    }
227

228
229
230
231
232
233
234
235
236
237
238
    /**
     * @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);
    }
239
}