-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
FieldMapping.php
169 lines (154 loc) · 5.52 KB
/
FieldMapping.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
declare(strict_types=1);
namespace Doctrine\ORM\Mapping;
use ArrayAccess;
use BackedEnum;
use function in_array;
use function property_exists;
/** @template-implements ArrayAccess<string, mixed> */
final class FieldMapping implements ArrayAccess
{
use ArrayAccessImplementation;
/** The database length of the column. Optional. Default value taken from the type. */
public int|null $length = null;
/**
* Marks the field as the primary key of the entity. Multiple
* fields of an entity can have the id attribute, forming a composite key.
*/
public bool|null $id = null;
public bool|null $nullable = null;
public bool|null $notInsertable = null;
public bool|null $notUpdatable = null;
public string|null $columnDefinition = null;
/** @psalm-var ClassMetadata::GENERATED_*|null */
public int|null $generated = null;
/** @var class-string<BackedEnum>|null */
public string|null $enumType = null;
/**
* The precision of a decimal column.
* Only valid if the column type is decimal
*/
public int|null $precision = null;
/**
* The scale of a decimal column.
* Only valid if the column type is decimal
*/
public int|null $scale = null;
/** Whether a unique constraint should be generated for the column. */
public bool|null $unique = null;
/**
* @var class-string|null This is set when the field is inherited by this
* class from another (inheritance) parent <em>entity</em> class. The value
* is the FQCN of the topmost entity class that contains mapping information
* for this field. (If there are transient classes in the class hierarchy,
* these are ignored, so the class property may in fact come from a class
* further up in the PHP class hierarchy.)
* Fields initially declared in mapped superclasses are
* <em>not</em> considered 'inherited' in the nearest entity subclasses.
*/
public string|null $inherited = null;
public string|null $originalClass = null;
public string|null $originalField = null;
public bool|null $quoted = null;
/**
* @var class-string|null This is set when the field does not appear for
* the first time in this class, but is originally declared in another
* parent <em>entity or mapped superclass</em>. The value is the FQCN of
* the topmost non-transient class that contains mapping information for
* this field.
*/
public string|null $declared = null;
public string|null $declaredField = null;
public array|null $options = null;
public bool|null $version = null;
public string|int|null $default = null;
/**
* @param string $type The type name of the mapped field. Can be one of
* Doctrine's mapping types or a custom mapping type.
* @param string $fieldName The name of the field in the Entity.
* @param string $columnName The column name. Optional. Defaults to the field name.
*/
public function __construct(
public string $type,
public string $fieldName,
public string $columnName,
) {
}
/**
* @param array<string, mixed> $mappingArray
* @psalm-param array{
* type: string,
* fieldName: string,
* columnName: string,
* length?: int|null,
* id?: bool|null,
* nullable?: bool|null,
* notInsertable?: bool|null,
* notUpdatable?: bool|null,
* columnDefinition?: string|null,
* generated?: ClassMetadata::GENERATED_*|null,
* enumType?: string|null,
* precision?: int|null,
* scale?: int|null,
* unique?: bool|null,
* inherited?: string|null,
* originalClass?: string|null,
* originalField?: string|null,
* quoted?: bool|null,
* declared?: string|null,
* declaredField?: string|null,
* options?: array<string, mixed>|null,
* version?: bool|null,
* default?: string|int|null,
* } $mappingArray
*/
public static function fromMappingArray(array $mappingArray): self
{
$mapping = new self(
$mappingArray['type'],
$mappingArray['fieldName'],
$mappingArray['columnName'],
);
foreach ($mappingArray as $key => $value) {
if (in_array($key, ['type', 'fieldName', 'columnName'])) {
continue;
}
if (property_exists($mapping, $key)) {
$mapping->$key = $value;
}
}
return $mapping;
}
/** @return list<string> */
public function __sleep(): array
{
$serialized = ['type', 'fieldName', 'columnName'];
foreach (['nullable', 'notInsertable', 'notUpdatable', 'id', 'unique', 'version', 'quoted'] as $boolKey) {
if ($this->$boolKey) {
$serialized[] = $boolKey;
}
}
foreach (
[
'length',
'columnDefinition',
'generated',
'enumType',
'precision',
'scale',
'inherited',
'originalClass',
'originalField',
'declared',
'declaredField',
'options',
'default',
] as $key
) {
if ($this->$key !== null) {
$serialized[] = $key;
}
}
return $serialized;
}
}