forked from corcel/corcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Options.php
92 lines (82 loc) · 1.7 KB
/
Options.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Corcel;
use Exception;
use Illuminate\Database\Eloquent\Model as Eloquent;
/**
* Options class.
*
* @author José CI <[email protected]>
*/
class Options extends Eloquent
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'options';
/**
* The primary key of the model.
*
* @var string
*/
protected $primaryKey = 'option_id';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'option_name',
'option_value',
'autoload',
];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['value'];
/**
* Gets the value.
* Tries to unserialize the object and returns the value if that doesn't work.
*
* @return value
*/
public function getValueAttribute()
{
try {
return unserialize($this->option_value);
} catch (Exception $ex) {
return $this->option_value;
}
}
/**
* Gets option field by its name.
*
* @param string $name
*
* @return string|array
*/
public static function get($name)
{
if ($option = self::where('option_name', $name)->first()) {
return $option->value;
}
return;
}
/**
* Gets all the options.
*
* @return array
*/
public static function getAll()
{
$options = self::all();
$result = [];
foreach ($options as $option) {
$result[$option->option_name] = $option->value;
}
return $result;
}
}