|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Wildfire\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Rougin\Describe\Column; |
|
6
|
|
|
use Rougin\Wildfire\CodeigniterModel; |
|
7
|
|
|
use Rougin\Wildfire\Helpers\TableHelper; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Object Trait |
|
11
|
|
|
* |
|
12
|
|
|
* @package Wildfire |
|
13
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
trait ObjectTrait |
|
16
|
|
|
{ |
|
17
|
|
|
use RelationshipTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Creates an object from the specified table and row. |
|
21
|
|
|
* |
|
22
|
|
|
* @param \Rougin\Wildfire\CodeigniterModel\string $table |
|
23
|
|
|
* @param object $row |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
36 |
|
protected function make($table, $row) |
|
27
|
|
|
{ |
|
28
|
36 |
|
list($table, $model) = $this->model($table); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
36 |
|
$properties = array('relationships' => array()); |
|
31
|
|
|
|
|
32
|
36 |
|
$properties['hidden'] = array(); |
|
33
|
|
|
|
|
34
|
36 |
|
$properties['columns'] = array(); |
|
35
|
|
|
|
|
36
|
36 |
|
if ($model instanceof CodeigniterModel === true) { |
|
37
|
36 |
|
$properties = (array) $model->properties(); |
|
38
|
|
|
|
|
39
|
36 |
|
$properties = $model->relationships($properties); |
|
40
|
24 |
|
} |
|
41
|
|
|
|
|
42
|
36 |
|
$columns = (array) $this->describe->columns($table); |
|
43
|
|
|
|
|
44
|
36 |
|
return $this->fields($columns, $properties, $model, $row); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Sets the foreign field of the column, if any. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \CI_Model $model |
|
51
|
|
|
* @param \Rougin\Describe\Column $column |
|
52
|
|
|
* @return \CI_Model |
|
53
|
|
|
*/ |
|
54
|
6 |
|
protected function foreign(\CI_Model $model, Column $column) |
|
55
|
|
|
{ |
|
56
|
6 |
|
$field = $column->getReferencedField(); |
|
57
|
|
|
|
|
58
|
6 |
|
$name = (string) $column->getField(); |
|
59
|
|
|
|
|
60
|
6 |
|
$table = $column->getReferencedTable(); |
|
61
|
|
|
|
|
62
|
6 |
|
$delimiters = array($field => $model->$name); |
|
63
|
|
|
|
|
64
|
6 |
|
$foreign = $this->find($table, $delimiters); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
6 |
|
$name = (string) TableHelper::name($foreign); |
|
67
|
|
|
|
|
68
|
6 |
|
is_object($foreign) && $model->$name = $foreign; |
|
69
|
|
|
|
|
70
|
6 |
|
return $model; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Sets the model values based on the result row. |
|
75
|
|
|
* |
|
76
|
|
|
* @param array $columns |
|
77
|
|
|
* @param array $properties |
|
78
|
|
|
* @param \CI_Model &$model |
|
79
|
|
|
* @param object $row |
|
80
|
|
|
* @return \CI_Model |
|
81
|
|
|
*/ |
|
82
|
36 |
|
protected function fields($columns, $properties, $model, $row) |
|
83
|
|
|
{ |
|
84
|
36 |
|
foreach ((array) $columns as $column) { |
|
85
|
36 |
|
$field = (string) $column->getField(); |
|
86
|
|
|
|
|
87
|
36 |
|
$allowed = in_array($field, $properties['columns']); |
|
88
|
|
|
|
|
89
|
36 |
|
$hidden = in_array($field, $properties['hidden']); |
|
90
|
|
|
|
|
91
|
36 |
|
$foreign = in_array($field, $properties['hidden']); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
36 |
|
if ($allowed === true || $hidden === false) { |
|
94
|
36 |
|
$table = (string) $column->getReferencedTable(); |
|
95
|
|
|
|
|
96
|
36 |
|
$model->$field = $row->$field; |
|
97
|
|
|
|
|
98
|
36 |
|
$exists = in_array($table, $properties['relationships']); |
|
99
|
|
|
|
|
100
|
36 |
|
$exists && $this->foreign($model, $column, $properties); |
|
|
|
|
|
|
101
|
24 |
|
} |
|
102
|
24 |
|
} |
|
103
|
|
|
|
|
104
|
36 |
|
return $model; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.