1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Sirius\Orm; |
5
|
|
|
|
6
|
|
|
use Sirius\Orm\Entity\GenericEntity; |
7
|
|
|
use Sirius\Orm\Helpers\QueryHelper; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class MapperConfig |
11
|
|
|
* Used to create mapper definitions that can be created using Mapper::make($mapperConfigInstance) |
12
|
|
|
* This is useful for dynamically generated mappers (think Wordpress custom post types) |
13
|
|
|
* @package Sirius\Orm |
14
|
|
|
*/ |
15
|
|
|
class MapperConfig |
16
|
|
|
{ |
17
|
|
|
const ENTITY_CLASS = 'entityClass'; |
18
|
|
|
const PRIMARY_KEY = 'primaryKey'; |
19
|
|
|
const NAME = 'name'; |
20
|
|
|
const TABLE = 'table'; |
21
|
|
|
const TABLE_ALIAS = 'tableAlias'; |
22
|
|
|
const COLUMNS = 'columns'; |
23
|
|
|
const COLUMN_ATTRIBUTE_MAP = 'columnAttributeMap'; |
24
|
|
|
const CASTS = 'casts'; |
25
|
|
|
const PIVOT_ATTRIBUTES = 'pivotAttributes'; |
26
|
|
|
const ATTRIBUTE_DEFAULTS = 'attributeDefaults'; |
27
|
|
|
const GUARDS = 'guards'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $entityClass = GenericEntity::class; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string|array |
36
|
|
|
*/ |
37
|
|
|
protected $primaryKey = 'id'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $table; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Used in queries like so: FROM table as tableAlias |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $tableAlias; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var |
52
|
|
|
*/ |
53
|
|
|
protected $tableReference; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Table columns |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $columns = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Columns casts |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $casts = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Column aliases (table column => entity attribute) |
69
|
|
|
* @var array |
70
|
|
|
*/ |
71
|
|
|
protected $columnAttributeMap = []; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Attributes that might come from THROUGH_COLUMNS |
75
|
|
|
* in many-to-many relations |
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
protected $pivotAttributes = []; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Default attributes |
82
|
|
|
* @var array |
83
|
|
|
*/ |
84
|
|
|
protected $attributeDefaults = []; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* List of column-value pairs that act as global filters |
88
|
|
|
* @var array |
89
|
|
|
*/ |
90
|
|
|
protected $guards = []; |
91
|
|
|
|
92
|
|
|
public static function fromArray(array $array) |
93
|
|
|
{ |
94
|
|
|
$instance = new self; |
95
|
|
|
foreach ($array as $k => $v) { |
96
|
|
|
$instance->{$k} = $v; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $instance; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getEntityClass(): string |
106
|
|
|
{ |
107
|
|
|
return $this->entityClass; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string|array |
112
|
|
|
*/ |
113
|
|
|
public function getPrimaryKey() |
114
|
|
|
{ |
115
|
|
|
return $this->primaryKey; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function getTable(): string |
122
|
|
|
{ |
123
|
|
|
return $this->table; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param bool $fallbackToTable |
128
|
|
|
* |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
public function getTableAlias($fallbackToTable = false) |
132
|
|
|
{ |
133
|
|
|
return (! $this->tableAlias && $fallbackToTable) ? $this->table : $this->tableAlias; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getColumns(): array |
137
|
|
|
{ |
138
|
|
|
return $this->columns; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getPivotAttributes(): array |
142
|
|
|
{ |
143
|
|
|
return $this->pivotAttributes; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
public function getAttributeNames(): array |
148
|
|
|
{ |
149
|
|
|
$columns = array_combine($this->columns, $this->columns); |
150
|
|
|
foreach ($this->getColumnAttributeMap() as $col => $attr) { |
151
|
|
|
$columns[$col] = $attr; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return array_merge(array_values($columns), $this->getPivotAttributes()); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getCasts(): array |
158
|
|
|
{ |
159
|
|
|
return $this->casts; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function getColumnAttributeMap(): array |
163
|
|
|
{ |
164
|
|
|
return $this->columnAttributeMap; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|
168
|
|
|
public function getAttributeDefaults(): array |
169
|
|
|
{ |
170
|
|
|
return $this->attributeDefaults; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function getGuards(): array |
174
|
|
|
{ |
175
|
|
|
return $this->guards; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function getTableReference() |
179
|
|
|
{ |
180
|
|
|
if (! $this->tableReference) { |
181
|
|
|
$this->tableReference = QueryHelper::reference($this->table, $this->tableAlias); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $this->tableReference; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|