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

FieldOptions   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 12
Bugs 4 Features 0
Metric Value
wmc 13
c 12
b 4
f 0
lcom 1
cbo 4
dl 0
loc 102
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A has() 0 4 1
A get() 0 8 2
A set() 0 7 1
A all() 0 9 2
A model() 0 10 3
A modelField() 0 4 2
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
}