|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wandu\Database\Entity; |
|
3
|
|
|
|
|
4
|
|
|
class Metadata |
|
5
|
|
|
{ |
|
6
|
|
|
/** @var string */ |
|
7
|
|
|
protected $connection; |
|
8
|
|
|
|
|
9
|
|
|
/** @var string */ |
|
10
|
|
|
protected $table; |
|
11
|
|
|
|
|
12
|
|
|
/** @var string */ |
|
13
|
|
|
protected $class; |
|
14
|
|
|
|
|
15
|
|
|
/** @var array */ |
|
16
|
|
|
protected $columns = []; |
|
17
|
|
|
|
|
18
|
|
|
/** @var array */ |
|
19
|
|
|
protected $casts = []; |
|
20
|
|
|
|
|
21
|
|
|
/** @var \Wandu\Database\Annotations\RelationInterface[] */ |
|
22
|
|
|
protected $relations = []; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
protected $primaryKey = 'id'; |
|
26
|
|
|
|
|
27
|
|
|
/** @var bool */ |
|
28
|
|
|
protected $increments = true; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @example |
|
32
|
|
|
* [ |
|
33
|
|
|
* 'class' => RepositoryTestActor::class, |
|
34
|
|
|
* 'table' => 'actor', |
|
35
|
|
|
* 'columns' => [ |
|
36
|
|
|
* 'id' => 'act_id', |
|
37
|
|
|
* 'firstName' => 'first_name', |
|
38
|
|
|
* 'lastName' => 'last_name', |
|
39
|
|
|
* 'lastUpdate' => 'last_update', |
|
40
|
|
|
* ], |
|
41
|
|
|
* 'primaryKey' => 'id', |
|
42
|
|
|
* 'increments' => true, |
|
43
|
|
|
* ] |
|
44
|
|
|
* @param string $table |
|
45
|
|
|
* @param array $settings |
|
46
|
|
|
*/ |
|
47
|
17 |
|
public function __construct($table, array $settings = []) |
|
48
|
|
|
{ |
|
49
|
17 |
|
$this->table = $table; |
|
50
|
17 |
|
foreach ($settings as $name => $setting) { |
|
51
|
17 |
|
$this->{$name} = $setting; |
|
52
|
|
|
} |
|
53
|
17 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
14 |
|
public function getConnection(): string |
|
59
|
|
|
{ |
|
60
|
14 |
|
return $this->connection; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
14 |
|
public function getTable() |
|
67
|
|
|
{ |
|
68
|
14 |
|
return $this->table; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
15 |
|
public function getClass() |
|
75
|
|
|
{ |
|
76
|
15 |
|
return $this->class; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
14 |
|
public function getColumns() |
|
83
|
|
|
{ |
|
84
|
14 |
|
return $this->columns; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return \Wandu\Database\Annotations\RelationInterface[] |
|
89
|
|
|
*/ |
|
90
|
13 |
|
public function getRelations(): array |
|
91
|
|
|
{ |
|
92
|
13 |
|
return $this->relations; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
5 |
|
public function getPrimaryKey() |
|
99
|
|
|
{ |
|
100
|
5 |
|
return $this->primaryKey; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return boolean |
|
105
|
|
|
*/ |
|
106
|
3 |
|
public function isIncrements() |
|
107
|
|
|
{ |
|
108
|
3 |
|
return $this->increments; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
13 |
|
public function getCasts() |
|
115
|
|
|
{ |
|
116
|
13 |
|
return $this->casts; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
2 |
|
public function getPrimaryProperty() |
|
123
|
|
|
{ |
|
124
|
2 |
|
foreach ($this->columns as $propertyName => $columnName) { |
|
125
|
2 |
|
if ($this->primaryKey === $columnName) { |
|
126
|
2 |
|
return $propertyName; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|