1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ntentan\nibii; |
4
|
|
|
|
5
|
|
|
class ModelDescription |
6
|
|
|
{ |
7
|
|
|
private $fields = []; |
8
|
|
|
private $primaryKey = []; |
9
|
|
|
private $uniqueKeys = []; |
10
|
|
|
private $autoPrimaryKey = false; |
11
|
|
|
private $relationships = []; |
12
|
|
|
private $table; |
13
|
|
|
private $name; |
14
|
|
|
|
15
|
8 |
|
public function __construct($model) |
16
|
|
|
{ |
17
|
8 |
|
$this->table = $model->getTable(); |
18
|
8 |
|
$this->name = Nibii::getModelName((new \ReflectionClass($model))->getName()); |
19
|
8 |
|
$relationships = $model->getRelationships(); |
20
|
8 |
|
$adapter = DriverAdapter::getDefaultInstance(); |
21
|
8 |
|
$schema = $adapter->getDriver()->describeTable($this->table)[$this->table]; |
22
|
8 |
|
$this->autoPrimaryKey = $schema['auto_increment']; |
23
|
|
|
|
24
|
8 |
|
foreach ($schema['columns'] as $field => $details) { |
25
|
8 |
|
$this->fields[$field] = [ |
26
|
8 |
|
'type' => $adapter->mapDataTypes($details['type']), |
27
|
8 |
|
'required' => !$details['nulls'], |
28
|
8 |
|
'default' => $details['default'], |
29
|
|
|
'name' => $field |
30
|
8 |
|
]; |
31
|
8 |
|
if(isset($details['default'])) { |
32
|
4 |
|
$this->fields[$field]['default'] = $details['default']; |
33
|
4 |
|
} |
34
|
8 |
|
if(isset($details['length'])) { |
35
|
8 |
|
$this->fields[$field]['length'] = $details['length']; |
36
|
8 |
|
} |
37
|
8 |
|
} |
38
|
|
|
|
39
|
8 |
|
$this->appendConstraints($schema['primary_key'], $this->primaryKey, true); |
40
|
8 |
|
$this->appendConstraints($schema['unique_keys'], $this->uniqueKeys); |
41
|
|
|
|
42
|
8 |
|
foreach($relationships as $type => $relations) { |
43
|
8 |
|
$this->createRelationships($type, $relations); |
44
|
8 |
|
} |
45
|
|
|
|
46
|
8 |
|
} |
47
|
|
|
|
48
|
8 |
|
private function appendConstraints($constraints, &$key, $flat = false) |
49
|
|
|
{ |
50
|
8 |
|
foreach ($constraints as $constraint) { |
51
|
8 |
|
if ($flat) { |
52
|
8 |
|
$key = $constraint['columns']; |
53
|
8 |
|
break; |
54
|
|
|
} else { |
55
|
4 |
|
$key[] = array( |
56
|
4 |
|
'fields' => $constraint['columns'] |
57
|
4 |
|
); |
58
|
|
|
} |
59
|
8 |
|
} |
60
|
8 |
|
} |
61
|
|
|
|
62
|
8 |
|
private function getRelationshipDetails($relationship) |
63
|
|
|
{ |
64
|
8 |
|
if(is_string($relationship)) |
65
|
8 |
|
{ |
66
|
|
|
return [ |
67
|
4 |
|
'model' => $relationship, |
68
|
4 |
|
'name' => $relationship, |
69
|
4 |
|
'foreign_key' => '', |
70
|
|
|
'local_key' => '' |
71
|
4 |
|
]; |
72
|
4 |
|
} else if (is_array($relationship)) { |
73
|
|
|
return [ |
74
|
4 |
|
'model' => $relationship[0], |
75
|
4 |
|
'name' => isset($relationship['as']) ? $relationship['as'] : $relationship[0], |
76
|
4 |
|
'foreign_key' => isset($relationship['foreign_key']) ? $relationship['foreign_key'] : '', |
77
|
4 |
|
'local_key' => isset($relationship['local_key']) ? $relationship['local_key'] : '' |
78
|
4 |
|
]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
8 |
|
private function createRelationships($type, $relationships) |
83
|
|
|
{ |
84
|
8 |
|
foreach($relationships as $relationship) |
85
|
|
|
{ |
86
|
8 |
|
$relationship = $this->getRelationshipDetails($relationship); |
87
|
8 |
|
$class = "\\ntentan\\nibii\\relationships\\{$type}Relationship"; |
88
|
8 |
|
$relationshipObject = new $class(); |
89
|
8 |
|
$relationshipObject->setOptions($relationship); |
90
|
8 |
|
$relationshipObject->setup($this->name, $this->table, $this->primaryKey); |
91
|
8 |
|
$this->relationships[$relationship['name']] = $relationshipObject; |
92
|
8 |
|
} |
93
|
8 |
|
} |
94
|
|
|
|
95
|
18 |
|
public function getRelationships() |
96
|
|
|
{ |
97
|
18 |
|
return $this->relationships; |
98
|
|
|
} |
99
|
|
|
|
100
|
24 |
|
public function getPrimaryKey() |
101
|
|
|
{ |
102
|
24 |
|
return $this->primaryKey; |
103
|
|
|
} |
104
|
|
|
|
105
|
10 |
|
public function getAutoPrimaryKey() |
106
|
|
|
{ |
107
|
10 |
|
return $this->autoPrimaryKey; |
108
|
|
|
} |
109
|
|
|
|
110
|
10 |
|
public function getFields() |
111
|
|
|
{ |
112
|
10 |
|
return $this->fields; |
113
|
|
|
} |
114
|
|
|
|
115
|
10 |
|
public function getUniqueKeys() |
116
|
|
|
{ |
117
|
10 |
|
return $this->uniqueKeys; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|