|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Wildfire\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use CI_Model; |
|
6
|
|
|
use Rougin\Describe\Column; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Object Trait |
|
10
|
|
|
* |
|
11
|
|
|
* @package Wildfire |
|
12
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
13
|
|
|
* |
|
14
|
|
|
* @property \Rougin\Describe\Describe $describe |
|
15
|
|
|
*/ |
|
16
|
|
|
trait ObjectTrait |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $tables = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Creates an object from the specified table and row. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $table |
|
27
|
|
|
* @param object $row |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
18 |
|
protected function createObject($table, $row) |
|
31
|
|
|
{ |
|
32
|
18 |
|
list($model, $newTable) = $this->getModel($table); |
|
33
|
|
|
|
|
34
|
18 |
|
if ( ! array_key_exists($newTable, $this->tables)) { |
|
35
|
18 |
|
$tableInfo = $this->describe->getTable($newTable); |
|
36
|
|
|
|
|
37
|
18 |
|
$this->tables[$newTable] = $tableInfo; |
|
38
|
18 |
|
} else { |
|
39
|
15 |
|
$tableInfo = $this->tables[$newTable]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
18 |
|
$columns = property_exists($model, 'columns') ? $model->columns : []; |
|
43
|
|
|
|
|
44
|
18 |
|
foreach ($tableInfo as $column) { |
|
45
|
18 |
|
$key = $column->getField(); |
|
46
|
|
|
|
|
47
|
18 |
|
if ( ! empty($columns) && ! isset($modal->columns[$key])) { |
|
|
|
|
|
|
48
|
3 |
|
continue; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
15 |
|
$model->$key = $row->$key; |
|
52
|
|
|
|
|
53
|
15 |
|
$this->setForeignField($model, $column); |
|
54
|
18 |
|
} |
|
55
|
|
|
|
|
56
|
18 |
|
return $model; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Finds the row from the specified ID or with the list of delimiters from |
|
61
|
|
|
* the specified table. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $table |
|
64
|
|
|
* @param array|integer $delimiters |
|
65
|
|
|
* @return object|boolean |
|
66
|
|
|
*/ |
|
67
|
|
|
abstract protected function find($table, $delimiters = []); |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Sets the foreign field of the column, if any. |
|
71
|
|
|
* |
|
72
|
|
|
* @param \CI_Model $model |
|
73
|
|
|
* @param \Rougin\Describe\Column $column |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
15 |
|
protected function setForeignField(CI_Model $model, Column $column) |
|
77
|
|
|
{ |
|
78
|
15 |
|
if ( ! $column->isForeignKey()) { |
|
79
|
15 |
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
15 |
|
$key = $column->getField(); |
|
83
|
15 |
|
$foreignColumn = $column->getReferencedField(); |
|
84
|
15 |
|
$foreignTable = $column->getReferencedTable(); |
|
85
|
|
|
|
|
86
|
15 |
|
$delimiters = [ $foreignColumn => $model->$key ]; |
|
87
|
15 |
|
$foreignData = $this->find($foreignTable, $delimiters); |
|
88
|
15 |
|
$newColumn = $this->getTableName($foreignTable); |
|
89
|
|
|
|
|
90
|
15 |
|
$model->$newColumn = $foreignData; |
|
91
|
15 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Gets the modal class of the said table. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string|null $table |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
24 |
|
protected function getModel($table = null) |
|
100
|
|
|
{ |
|
101
|
24 |
|
if ($table == null) { |
|
|
|
|
|
|
102
|
6 |
|
return [ null, '' ]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
24 |
|
$newTable = $this->getTableName($table); |
|
106
|
24 |
|
$model = new $newTable; |
|
107
|
|
|
|
|
108
|
24 |
|
if (property_exists($model, 'table')) { |
|
109
|
3 |
|
$newTable = $model->table; |
|
110
|
3 |
|
} |
|
111
|
|
|
|
|
112
|
24 |
|
return [ $model, strtolower($newTable) ]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Parses the table name from Describe class. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $table |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
abstract protected function getTableName($table); |
|
122
|
|
|
} |
|
123
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.