1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Mailable. |
5
|
|
|
* |
6
|
|
|
* (c) Oliver Green <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BoxedCode\Eloquent\Meta; |
13
|
|
|
|
14
|
|
|
trait FluentMeta |
15
|
|
|
{ |
16
|
|
|
use Metable; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Model table columns. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
public static $model_table_columns = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get a models table columns. |
27
|
|
|
* |
28
|
|
|
* @param $class |
29
|
|
|
* @return mixed |
30
|
|
|
*/ |
31
|
|
|
protected function getClassColumns($class) |
32
|
|
|
{ |
33
|
|
|
if (! isset(static::$model_table_columns[$class])) { |
34
|
|
|
static::$model_table_columns[$class] = $this->getConnection() |
|
|
|
|
35
|
|
|
->getSchemaBuilder() |
36
|
|
|
->getColumnListing($this->getTable()); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return static::$model_table_columns[$class]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Check whether a method, property or attribute |
44
|
|
|
* name exists on the model. |
45
|
|
|
* |
46
|
|
|
* @param $name |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
protected function existsOnParent($name) |
50
|
|
|
{ |
51
|
|
|
$columns = $this->getClassColumns(static::class); |
52
|
|
|
|
53
|
|
|
return property_exists($this, $name) |
54
|
|
|
|| method_exists($this, $name) |
55
|
|
|
|| in_array($name, $columns); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Dynamically determine whether a meta item or model property isset. |
60
|
|
|
* |
61
|
|
|
* @param $name |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function __isset($name) |
65
|
|
|
{ |
66
|
|
|
if ($this->existsOnParent($name)) { |
67
|
|
|
return parent::__isset($name); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return isset($this->meta->$name); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Dynamically get a model property / attribute or meta item. |
75
|
|
|
* |
76
|
|
|
* @param $name |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
|
|
public function __get($name) |
80
|
|
|
{ |
81
|
|
|
if ($this->existsOnParent($name)) { |
82
|
|
|
return parent::__get($name); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($meta = $this->meta->$name) { |
|
|
|
|
86
|
|
|
return $this->meta->$name; |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Dynamically set a meta item or model property / attribute. |
92
|
|
|
* |
93
|
|
|
* @param $name |
94
|
|
|
* @param $value |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function __set($name, $value) |
98
|
|
|
{ |
99
|
|
|
if ($this->existsOnParent($name)) { |
100
|
|
|
return parent::__set($name, $value); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->meta->$name = $value; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Dynamically unset a meta item or model property / attribute. |
108
|
|
|
* |
109
|
|
|
* @param $name |
110
|
|
|
*/ |
111
|
|
|
public function __unset($name) |
112
|
|
|
{ |
113
|
|
|
if ($this->existsOnParent($name)) { |
114
|
|
|
unset($this->$name); |
115
|
|
|
|
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($meta = $this->meta->$name) { |
|
|
|
|
120
|
|
|
unset($this->meta->$name); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
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
Idable
provides a methodequalsId
that 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.