Completed
Push — dev ( 17f62c )
by Marc
09:19
created

FieldOptions::model()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 3 Features 0
Metric Value
c 6
b 3
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 3
nop 0
1
<?php namespace Mascame\Artificer\Fields;
2
3
use Mascame\Artificer\Model\Model;
4
use Mascame\Artificer\Options\AdminOption;
5
use Mascame\Artificer\Options\FieldOption;
6
use Mascame\Artificer\Options\ModelOption;
7
8
class FieldOptions
9
{
10
11
    protected $name;
12
    protected $options;
13
    protected $defaultOptions;
14
    public $model;
15
16
    /**
17
     * @param $name
18
     */
19
    public function __construct($name, $type)
20
    {
21
        $this->name = $name;
22
        $this->defaultOptions = (AdminOption::has('fields.types.' . $type)) ? AdminOption::get('fields.types.' . $type) : [];
23
        $this->model = $this->model();
24
25
        $this->options = array_merge($this->modelField(), $this->defaultOptions, $this->all() );
26
    }
27
28
    /**
29
     * @param string $key
30
     * @return bool
31
     */
32
    public function has($key)
33
    {
34
        return (isset($this->options[$key]));
35
    }
36
37
38
    /**
39
     * @param string $key
40
     * @return mixed
41
     */
42
    public function get($key, $default = null)
43
    {
44
        if ($this->has($key)) {
45
            return $this->options[$key];
46
        }
47
48
        return $default;
49
    }
50
51
52
    /**
53
     * @param string $key
54
     * @param $value
55
     */
56
    public function set($key, $value)
57
    {
58
        FieldOption::set($key, $value, $this->name);
59
60
        // Refresh options
61
        return $this->all();
62
    }
63
64
65
    /**
66
     * @return array|mixed
67
     */
68
    public function all()
69
    {
70
        if (isset($this->options)) return $this->options;
71
//        if (isset($this->options)) {
72
//            return (array)$this->options = array_merge($this->options, FieldOption::field($this->name));
73
//        }
74
75
        return (array)$this->options = FieldOption::field($this->name);
76
    }
77
78
79
    /**
80
     * @return array|mixed
81
     */
82
    public function model()
83
    {
84
        if (isset($this->model)) return $this->model;
85
86
        $model = config('admin.models.' . Model::getCurrent());
87
88
        $defaultModel = ModelOption::getDefault();
89
90
        return $this->model = ( ! empty($model)) ? array_merge_recursive($model, $defaultModel) : ModelOption::getDefault();
91
    }
92
93
    public function modelField()
94
    {
95
        return (isset($this->model['fields'][$this->name])) ? $this->model['fields'][$this->name] : [];
96
    }
97
    /**
98
     * @param string $key
99
     * @param $value
100
     * @return array|mixed
101
     */
102
//    public function add($key, $value)
103
//    {
104
//        $this->set($key, $value);
105
//
106
//        // Refresh options
107
//        return $this->all();
108
//    }
109
}