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
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
use BoxedCode\Eloquent\Meta\Contracts\MetaItem as MetaItemContract; |
16
|
|
|
use BoxedCode\Eloquent\Meta\Types\Registry; |
17
|
|
|
|
18
|
|
|
class MetaItem extends Model implements MetaItemContract |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The attributes that are mass assignable. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $fillable = ['key', 'tag', 'value']; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The table associated with the model. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $table = 'meta'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Parent model relation. |
36
|
|
|
* |
37
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
38
|
|
|
*/ |
39
|
1 |
|
public function model() |
40
|
|
|
{ |
41
|
1 |
|
return $this->morphTo(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the value type registry. |
46
|
|
|
* |
47
|
|
|
* @return \BoxedCode\Eloquent\Meta\Types\Registry |
48
|
|
|
*/ |
49
|
54 |
|
protected function getTypeRegistry() |
50
|
|
|
{ |
51
|
54 |
|
return app(Registry::class); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the models value type class name. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
54 |
|
protected function getTypeClass() |
60
|
|
|
{ |
61
|
54 |
|
return $this->getTypeRegistry()[$this->type]; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the models value type instance. |
66
|
|
|
* |
67
|
|
|
* @return \BoxedCode\Eloquent\Meta\Contracts\Type |
68
|
|
|
*/ |
69
|
54 |
|
protected function getTypeInstance() |
70
|
|
|
{ |
71
|
54 |
|
$class = $this->getTypeClass(); |
72
|
|
|
|
73
|
54 |
|
return new $class($this); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Parse and get the value attribute. |
78
|
|
|
* |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
7 |
|
public function getValueAttribute() |
82
|
|
|
{ |
83
|
7 |
|
return $this->getTypeInstance()->get(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Parse and set the value attribute. |
88
|
|
|
* |
89
|
|
|
* @param mixed $value |
90
|
|
|
* @param string $type |
91
|
|
|
*/ |
92
|
54 |
|
public function setValueAttribute($value, $type = null) |
93
|
|
|
{ |
94
|
54 |
|
if (is_null($type) && ! isset($this->attributes['type'])) { |
95
|
54 |
|
$registry = $this->getTypeRegistry(); |
96
|
54 |
|
$this->attributes['type'] = $registry->findTypeFor($value)->getClass(); |
97
|
54 |
|
} elseif (isset($type)) { |
98
|
1 |
|
$this->attributes['type'] = $type; |
99
|
1 |
|
} |
100
|
|
|
|
101
|
54 |
|
return $this->getTypeInstance()->set($value); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the value attribute by-passing any accessors. |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
20 |
|
public function getRawValue() |
110
|
|
|
{ |
111
|
20 |
|
return $this->attributes['value']; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set the value attribute by-passing the mutators. |
116
|
|
|
* |
117
|
|
|
* @param mixed $value |
118
|
|
|
*/ |
119
|
54 |
|
public function setRawValue($value) |
120
|
|
|
{ |
121
|
54 |
|
$this->attributes['value'] = $value; |
122
|
54 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Create a new Eloquent Collection instance. |
126
|
|
|
* |
127
|
|
|
* @param array $models |
128
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
129
|
|
|
*/ |
130
|
9 |
|
public function newCollection(array $models = []) |
131
|
|
|
{ |
132
|
9 |
|
$collection = new MetaItemCollection($models); |
133
|
|
|
|
134
|
9 |
|
$collection::setMetaItemClass($this); |
135
|
|
|
|
136
|
9 |
|
return $collection; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get the string value of the meta item. |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
1 |
|
public function __toString() |
145
|
|
|
{ |
146
|
1 |
|
return $this->getTypeInstance()->__toString(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.