Issues (590)

src/Mapper/Info/PropertyInfo.php (1 issue)

Severity
1
<?php
2
3
namespace Bdf\Prime\Mapper\Info;
4
5
use Bdf\Prime\Mapper\Metadata;
6
use Bdf\Prime\Types\PhpTypeInterface;
7
use Bdf\Prime\Types\TypeInterface;
8
use Bdf\Prime\Types\TypesRegistryInterface;
9
use DateTimeInterface;
10
11
/**
12
 * PropertyInfo
13
 */
14
class PropertyInfo implements InfoInterface
15
{
16
    /**
17
     * The property name
18
     *
19
     * @var string
20
     */
21
    protected $name;
22
23
    /**
24
     * The metadata from the metadata object
25
     *
26
     * @var array
27
     */
28
    protected $metadata;
29
30
    /**
31
     * The metadata from the metadata object
32
     *
33
     * @var array
34
     */
35
    protected $relation;
36
37
    /**
38
     * The types registry
39
     *
40
     * @var TypesRegistryInterface
41
     */
42
    protected $typesRegistry;
43
44
45
    /**
46
     * Constructor
47
     *
48
     * @param string $name The property name
49
     * @param array $metadata The property metadata or the relation metadata
50
     * @param TypesRegistryInterface|null $typesRegistry The types registry
51
     */
52 77
    public function __construct(string $name, array $metadata = [], ?TypesRegistryInterface $typesRegistry = null)
53
    {
54 77
        $this->name = $name;
55 77
        $this->metadata = $metadata;
56 77
        $this->typesRegistry = $typesRegistry;
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 51
    public function name(): string
63
    {
64 51
        return $this->name;
65
    }
66
67
    /**
68
     * Get the property type
69
     *
70
     * @return string
71
     */
72 60
    public function type(): string
73
    {
74 60
        return $this->metadata['type'];
75
    }
76
77
    /**
78
     * Get the property alias
79
     *
80
     * @return string|null
81
     */
82 1
    public function alias(): ?string
83
    {
84 1
        return $this->metadata['field'] !== $this->metadata['attribute']
85 1
            ? $this->metadata['field']
86 1
            : null;
87
    }
88
89
    /**
90
     * Get the php type of the property
91
     * Class should have '\' char to be resolved with namespace.
92
     *
93
     * @return string
94
     */
95 59
    public function phpType(): string
96
    {
97 59
        if (isset($this->metadata['phpOptions']['className'])) {
98 8
            return '\\'.ltrim($this->metadata['phpOptions']['className'], '\\');
99
        }
100
101 57
        $type = $this->type();
102
103 57
        if ($this->typesRegistry === null || !$this->typesRegistry->has($type)) {
104 1
            return $type;
105
        }
106
107 56
        return $this->typesRegistry->get($type)->phpType();
108
    }
109
110
    /**
111
     * Check whether the property is a primary key
112
     *
113
     * @return bool
114
     */
115 76
    public function isPrimary(): bool
116
    {
117 76
        return $this->metadata['primary'] !== null;
118
    }
119
120
    /**
121
     * Check if the property value is auto-generated (auto increment or sequence)
122
     *
123
     * @return bool
124
     */
125 34
    public function isGenerated(): bool
126
    {
127 34
        return in_array($this->metadata['primary'], [Metadata::PK_AUTOINCREMENT, Metadata::PK_SEQUENCE], true);
128
    }
129
130
    /**
131
     * Check if the property can be null
132
     *
133
     * - Marked as nullable on mapper
134
     * - It's value is auto-generated
135
     *
136
     * @return bool
137
     */
138 33
    public function isNullable(): bool
139
    {
140 33
        return !empty($this->metadata['nillable']) || $this->isGenerated();
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 76
    public function isEmbedded(): bool
147
    {
148 76
        return $this->metadata['embedded'] !== null;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 76
    public function belongsToRoot(): bool
155
    {
156 76
        return !$this->isEmbedded();
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162 45
    public function isObject(): bool
163
    {
164 45
        return false;
165
    }
166
167
    /**
168
     * Check whether the property is a date time object
169
     *
170
     * @return bool
171
     */
172 35
    public function isDateTime(): bool
173
    {
174 35
        return is_subclass_of($this->phpType(), DateTimeInterface::class);
175
    }
176
177
    /**
178
     * Gets the date timezone
179
     *
180
     * @return string|null
181
     */
182 5
    public function getTimezone(): ?string
183
    {
184 5
        if (!$this->isDateTime()) {
185 1
            return null;
186
        }
187
188 5
        if (isset($this->metadata['phpOptions']['timezone'])) {
189 5
            return $this->metadata['phpOptions']['timezone'];
190
        }
191
192
        /** @var \Bdf\Prime\Types\AbstractDateTimeType $type */
193 5
        $type = $this->getType();
194
        /** @var \DateTimeZone $timezone */
195 5
        $timezone = $type->getTimezone();
196
197 5
        return $timezone ? $timezone->getName() : null;
0 ignored issues
show
$timezone is of type DateTimeZone, thus it always evaluated to true.
Loading history...
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203 34
    public function isArray(): bool
204
    {
205 34
        return $this->phpType() === PhpTypeInterface::TARRAY;
206
    }
207
208
    /**
209
     * Check whether the property has a default value
210
     *
211
     * @return bool
212
     */
213 33
    public function hasDefault(): bool
214
    {
215 33
        return $this->getDefault() !== null;
216
    }
217
218
    /**
219
     * Get the default value
220
     *
221
     * @return mixed
222
     */
223 34
    public function getDefault()
224
    {
225 34
        return $this->metadata['default'];
226
    }
227
228
    /**
229
     * Get the default value
230
     *
231
     * @param mixed $value
232
     * @param bool  $toPhp
233
     * @param array $fieldOptions
234
     *
235
     * @return mixed
236
     */
237 7
    public function convert($value, bool $toPhp = true, array $fieldOptions = [])
238
    {
239 7
        if ($toPhp) {
240 6
            return $this->getType()->fromDatabase($value, $fieldOptions);
241
        }
242
243 1
        return $this->getType()->toDatabase($value);
244
    }
245
246
    /**
247
     * Get the type object
248
     *
249
     * @return TypeInterface
250
     */
251 8
    protected function getType()
252
    {
253 8
        return $this->typesRegistry->get($this->type());
254
    }
255
}
256