ItemField::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace zacksleo\yii2\cms\components;
4
5
use yii\base\Component;
6
use yii\caching\Cache;
7
use Yii;
8
9
/**
10
 * @author Aris Karageorgos <[email protected]>
11
 */
12
class ItemField extends Component
13
{
14
    /**
15
     * @var string settings model. Make sure your settings model calls clearCache in the afterSave callback
16
     */
17
    public $modelClass = 'zacksleo\yii2\cms\models\ItemField';
18
19
    /**
20
     * Model to for storing and retrieving settings
21
     * @var \pheme\settings\models\SettingInterface
22
     */
23
    protected $model;
24
25
    /**
26
     * Holds a cached copy of the data for the current request
27
     *
28
     * @var mixed
29
     */
30
    private $_data = null;
31
32
    /**
33
     * Initialize the component
34
     *
35
     * @throws \yii\base\InvalidConfigException
36
     */
37
    public function init()
38
    {
39
        parent::init();
40
41
        $this->model = new $this->modelClass;
42
    }
43
44
    /**
45
     * Get's the value for the given key and section.
46
     * You can use dot notation to separate the section from the key:
47
     * $value = $settings->get('section.key');
48
     * and
49
     * $value = $settings->get('key', 'section');
50
     * are equivalent
51
     *
52
     * @param $key
53
     * @param string|null $section
54
     * @param string|null $default
55
     * @return mixed
56
     */
57
    public function get($key, $section, $default = null)
58
    {
59
        $data = $this->getRawConfig();
60
61
        if (isset($data[$section][$key][0])) {
62
            if (in_array($data[$section][$key][1], ['boolean', 'bool', 'integer', 'int', 'float', 'string', 'array'])) {
63
                settype($data[$section][$key][0], $data[$section][$key][1]);
64
            }
65
        } else {
66
            $data[$section][$key][0] = $default;
67
        }
68
        return $data[$section][$key][0];
69
    }
70
71
    /**
72
     * Checks to see if a setting exists.
73
     * If $searchDisabled is set to true, calling this function will result in an additional query.
74
     * @param $key
75
     * @param string|null $section
76
     * @param boolean $searchDisabled
77
     * @return boolean
78
     */
79
    public function has($key, $section = null, $searchDisabled = false)
80
    {
81
        if ($searchDisabled) {
82
            $setting = $this->model->findSetting($key, $section);
83
        } else {
84
            $setting = $this->get($key, $section);
85
        }
86
        is_null($setting) ? false : true;
87
    }
88
89
    /**
90
     * @param $key
91
     * @param $value
92
     * @param null $section
93
     * @param null $type
94
     * @return boolean
95
     */
96
    public function set($key, $value, $section, $type = null)
97
    {
98
        return $this->model->setSetting($section, $key, $value, $type);
99
    }
100
101
    /**
102
     * Deletes a setting
103
     *
104
     * @param $key
105
     * @param null|string $section
106
     * @return bool
107
     */
108
    public function delete($key, $section)
109
    {
110
        return $this->model->deleteSetting($section, $key);
111
    }
112
113
    /**
114
     * Returns the raw configuration array
115
     *
116
     * @return array
117
     */
118
    public function getRawConfig()
119
    {
120
        if ($this->_data === null) {
121
            $data = $this->model->getSettings();
122
            $this->_data = $data;
123
        }
124
        return $this->_data;
125
    }
126
}
127