1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Timegridio\Concierge\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model as EloquentModel; |
6
|
|
|
|
7
|
|
|
class Preference extends EloquentModel |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* [$fillable description]. |
11
|
|
|
* |
12
|
|
|
* @var [type] |
13
|
|
|
*/ |
14
|
|
|
protected $fillable = ['key', 'value', 'type']; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* [preferenceable description]. |
18
|
|
|
* |
19
|
|
|
* @return [type] [description] |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
public function preferenceable() |
22
|
|
|
{ |
23
|
|
|
return $this->morphTo(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* [__toString description]. |
28
|
|
|
* |
29
|
|
|
* @return string [description] |
30
|
|
|
*/ |
31
|
|
|
public function __toString() |
32
|
|
|
{ |
33
|
|
|
return $this->attributes['value']; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* [getDefault description]. |
38
|
|
|
* |
39
|
|
|
* @param \Timegridio\Concierge\Traits\Preferenceable $model [description] |
|
|
|
|
40
|
|
|
* @param [type] $key [description] |
|
|
|
|
41
|
|
|
* |
42
|
|
|
* @return [type] [description] |
|
|
|
|
43
|
|
|
*/ |
44
|
5 |
|
public static function getDefault($model, $key) |
45
|
|
|
{ |
46
|
5 |
|
$class = get_class($model); |
47
|
5 |
|
$value = config("preferences.{$class}.{$key}.value"); |
48
|
5 |
|
$type = config("preferences.{$class}.{$key}.type"); |
49
|
|
|
|
50
|
5 |
|
return new self([ |
51
|
5 |
|
'key' => $key, |
52
|
5 |
|
'value' => $value, |
53
|
5 |
|
'type' => $type, |
54
|
5 |
|
'preferenceable_type' => $class, |
55
|
5 |
|
'preferenceable_id' => $model, |
56
|
5 |
|
]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* [question description]. |
61
|
|
|
* |
62
|
|
|
* @return [type] [description] |
|
|
|
|
63
|
|
|
*/ |
64
|
|
|
public function question() |
65
|
|
|
{ |
66
|
|
|
return trans("preferences.{$this->preferenceable_type}.question.{$this->key}"); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* [help description]. |
71
|
|
|
* |
72
|
|
|
* @return [type] [description] |
|
|
|
|
73
|
|
|
*/ |
74
|
|
|
public function help() |
75
|
|
|
{ |
76
|
|
|
return trans("preferences.{$this->preferenceable_type}.help.{$this->key}"); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* [scopeForKey description]. |
81
|
|
|
* |
82
|
|
|
* @param [type] $query [description] |
|
|
|
|
83
|
|
|
* @param [type] $key [description] |
|
|
|
|
84
|
|
|
* |
85
|
|
|
* @return [type] [description] |
|
|
|
|
86
|
|
|
*/ |
87
|
5 |
|
public function scopeForKey($query, $key) |
88
|
|
|
{ |
89
|
5 |
|
return $query->where('key', $key); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* [value description]. |
94
|
|
|
* |
95
|
|
|
* @return [type] [description] |
|
|
|
|
96
|
|
|
*/ |
97
|
5 |
|
public function value() |
98
|
|
|
{ |
99
|
5 |
|
switch ($this->type) { |
|
|
|
|
100
|
5 |
|
case 'string': |
101
|
1 |
|
return (string) $this->value; |
|
|
|
|
102
|
|
|
break; |
|
|
|
|
103
|
4 |
|
case 'bool': |
104
|
1 |
|
return (bool) $this->value; |
|
|
|
|
105
|
|
|
break; |
|
|
|
|
106
|
3 |
|
case 'int': |
107
|
1 |
|
return (int) $this->value; |
|
|
|
|
108
|
|
|
break; |
|
|
|
|
109
|
2 |
|
case 'float': |
110
|
1 |
|
return (float) $this->value; |
|
|
|
|
111
|
|
|
break; |
|
|
|
|
112
|
1 |
|
case 'json': |
113
|
|
|
return json_decode($this->value); |
|
|
|
|
114
|
|
|
break; |
|
|
|
|
115
|
1 |
|
case 'array': |
116
|
|
|
return unserialize($this->value); |
|
|
|
|
117
|
|
|
break; |
|
|
|
|
118
|
1 |
|
default: |
119
|
1 |
|
break; |
120
|
1 |
|
} |
121
|
|
|
|
122
|
1 |
|
return $this->value; |
|
|
|
|
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.