|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Wildfire\Traits; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Object Trait |
|
7
|
|
|
* |
|
8
|
|
|
* @package Wildfire |
|
9
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @property \Rougin\Describe\Describe $describe |
|
12
|
|
|
*/ |
|
13
|
|
|
trait ObjectTrait |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $tables = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Creates an object from the specified table and row. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $table |
|
24
|
|
|
* @param object $row |
|
25
|
|
|
* @return array |
|
26
|
|
|
*/ |
|
27
|
18 |
|
protected function createObject($table, $row) |
|
28
|
|
|
{ |
|
29
|
18 |
|
list($model, $newTable) = $this->getModel($table); |
|
30
|
|
|
|
|
31
|
18 |
|
if ( ! array_key_exists($newTable, $this->tables)) { |
|
32
|
18 |
|
$tableInfo = $this->describe->getTable($newTable); |
|
33
|
|
|
|
|
34
|
18 |
|
$this->tables[$newTable] = $tableInfo; |
|
35
|
18 |
|
} |
|
36
|
|
|
|
|
37
|
18 |
|
if (isset($this->tables[$newTable])) { |
|
38
|
18 |
|
$tableInfo = $this->tables[$newTable]; |
|
39
|
18 |
|
} |
|
40
|
|
|
|
|
41
|
18 |
|
foreach ($tableInfo as $column) { |
|
|
|
|
|
|
42
|
18 |
|
if ( ! property_exists($row, $column->getField())) { |
|
43
|
|
|
continue; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
18 |
|
$key = $column->getField(); |
|
47
|
|
|
|
|
48
|
18 |
|
$model->$key = $row->$key; |
|
49
|
|
|
|
|
50
|
18 |
|
if ($column->isForeignKey()) { |
|
51
|
15 |
|
$foreignColumn = $column->getReferencedField(); |
|
52
|
15 |
|
$foreignTable = $column->getReferencedTable(); |
|
53
|
|
|
|
|
54
|
15 |
|
$delimiters = [ $foreignColumn => $model->$key ]; |
|
55
|
15 |
|
$foreignData = $this->find($foreignTable, $delimiters); |
|
56
|
15 |
|
$newColumn = $this->getTableName($foreignTable); |
|
57
|
|
|
|
|
58
|
15 |
|
$model->$newColumn = $foreignData; |
|
59
|
15 |
|
} |
|
60
|
18 |
|
} |
|
61
|
|
|
|
|
62
|
18 |
|
return $model; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Finds the row from the specified ID or with the list of delimiters from |
|
67
|
|
|
* the specified table. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $table |
|
70
|
|
|
* @param array|integer $delimiters |
|
71
|
|
|
* @return object|boolean |
|
72
|
|
|
*/ |
|
73
|
|
|
abstract protected function find($table, $delimiters = []); |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Gets the modal class of the said table. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string|null $table |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
24 |
|
protected function getModel($table = null) |
|
82
|
|
|
{ |
|
83
|
24 |
|
if ($table == null) { |
|
|
|
|
|
|
84
|
6 |
|
return [ null, '' ]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
24 |
|
$newTable = $this->getTableName($table); |
|
88
|
24 |
|
$model = new $newTable; |
|
89
|
|
|
|
|
90
|
24 |
|
if (property_exists($model, 'table')) { |
|
91
|
3 |
|
$newTable = $model->table; |
|
92
|
3 |
|
} |
|
93
|
|
|
|
|
94
|
24 |
|
return [ $model, strtolower($newTable) ]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Parses the table name from Describe class. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $table |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
abstract protected function getTableName($table); |
|
104
|
|
|
} |
|
105
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: