1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Database\Repository; |
3
|
|
|
|
4
|
|
|
use Doctrine\Common\Annotations\Reader; |
5
|
|
|
use ReflectionClass; |
6
|
|
|
use Wandu\Database\Annotations\Column; |
7
|
|
|
use Wandu\Database\Annotations\Table; |
8
|
|
|
|
9
|
|
|
class RepositorySettings |
10
|
|
|
{ |
11
|
12 |
|
public static function fromAnnotation($model, Reader $reader) |
12
|
|
|
{ |
13
|
|
|
$settings = [ |
14
|
12 |
|
'model' => $model, |
15
|
12 |
|
]; |
16
|
12 |
|
$classRefl = new ReflectionClass($model); |
17
|
12 |
|
$propertiesRefl = $classRefl->getProperties(); |
18
|
|
|
|
19
|
12 |
|
class_exists(Table::class); |
20
|
12 |
|
class_exists(Column::class); |
21
|
|
|
|
22
|
|
|
/* @var \Wandu\Database\Annotations\Table $table */ |
23
|
12 |
|
if ($table = $reader->getClassAnnotation($classRefl, Table::class)) { |
24
|
12 |
|
$settings['identifier'] = $table->identifier; |
25
|
12 |
|
$settings['increments'] = $table->increments; |
26
|
12 |
|
} |
27
|
|
|
|
28
|
12 |
|
$columns = []; |
29
|
12 |
|
$casts = []; |
30
|
12 |
|
foreach ($propertiesRefl as $propertyRefl) { |
31
|
|
|
/* @var \Wandu\Database\Annotations\Column $column */ |
32
|
12 |
|
if ($column = $reader->getPropertyAnnotation($propertyRefl, Column::class)) { |
|
|
|
|
33
|
12 |
|
$columns[$column->name] = $propertyRefl->name; |
34
|
12 |
|
$casts[$column->name] = $column->cast; |
35
|
12 |
|
} |
36
|
12 |
|
} |
37
|
12 |
|
if (count($columns)) { |
38
|
12 |
|
$settings['columns'] = $columns; |
39
|
12 |
|
} |
40
|
12 |
|
if (count($casts)) { |
41
|
12 |
|
$settings['casts'] = $casts; |
42
|
12 |
|
} |
43
|
12 |
|
return new RepositorySettings($table->name, $settings); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** @var string */ |
47
|
|
|
protected $table; |
48
|
|
|
|
49
|
|
|
/** @var string */ |
50
|
|
|
protected $model; |
51
|
|
|
|
52
|
|
|
/** @var array */ |
53
|
|
|
protected $columns = []; |
54
|
|
|
|
55
|
|
|
/** @var array */ |
56
|
|
|
protected $casts = []; |
57
|
|
|
|
58
|
|
|
/** @var string */ |
59
|
|
|
protected $identifier = 'id'; |
60
|
|
|
|
61
|
|
|
/** @var bool */ |
62
|
|
|
protected $increments = true; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @example |
66
|
|
|
* [ |
67
|
|
|
* 'model' => RepositoryTestActor::class, |
68
|
|
|
* 'table' => 'actor', |
69
|
|
|
* 'columns' => [ |
70
|
|
|
* 'id' => 'act_id', |
71
|
|
|
* 'firstName' => 'first_name', |
72
|
|
|
* 'lastName' => 'last_name', |
73
|
|
|
* 'lastUpdate' => 'last_update', |
74
|
|
|
* ], |
75
|
|
|
* 'identifier' => 'id', |
76
|
|
|
* 'increments' => true, |
77
|
|
|
* ] |
78
|
|
|
* @param string $table |
79
|
|
|
* @param array $settings |
80
|
|
|
*/ |
81
|
12 |
|
public function __construct($table, array $settings = []) |
82
|
|
|
{ |
83
|
12 |
|
$this->table = $table; |
84
|
12 |
|
foreach ($settings as $name => $setting) { |
85
|
12 |
|
$this->{$name} = $setting; |
86
|
12 |
|
} |
87
|
12 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
7 |
|
public function getTable() |
93
|
|
|
{ |
94
|
7 |
|
return $this->table; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
10 |
|
public function getModel() |
101
|
|
|
{ |
102
|
10 |
|
return $this->model; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
11 |
|
public function getColumns() |
109
|
|
|
{ |
110
|
11 |
|
return $this->columns; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
2 |
|
public function getIdentifier() |
117
|
|
|
{ |
118
|
2 |
|
return $this->identifier; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return boolean |
123
|
|
|
*/ |
124
|
1 |
|
public function isIncrements() |
125
|
|
|
{ |
126
|
1 |
|
return $this->increments; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
10 |
|
public function getCasts() |
133
|
|
|
{ |
134
|
10 |
|
return $this->casts; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.