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