FieldOptions   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

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