Passed
Push — master ( 994b13...9eb24c )
by Radu
01:38
created

AbstractLibrary::setSetting()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 2
1
<?php
2
namespace WebServCo\Framework;
3
4
abstract class AbstractLibrary implements
5
    \WebServCo\Framework\Interfaces\ArrayInterface,
6
    \WebServCo\Framework\Interfaces\DataInterface,
7
    \WebServCo\Framework\Interfaces\SettingsInterface
8
{
9
    private $data;
10
    private $settings;
11
12
    public function __construct($settings = [])
13
    {
14
        $this->clearData();
15
16
        if (is_array($settings)) { // check instead of cast to prevent unexpected results
17
            $this->settings = $settings;
18
        } else {
19
            $this->settings = [];
20
        }
21
    }
22
23
    final public function clearData()
24
    {
25
        $this->data = [];
26
    }
27
28
    /**
29
     * Returns data if exists, $defaultValue otherwise.
30
     *
31
     * @param string $key
32
     * @param mixed $defaultValue
33
     * @return mixed
34
     */
35
    final public function data($key, $defaultValue = false)
36
    {
37
        return \WebServCo\Framework\ArrayStorage::get(
38
            $this->data,
39
            $key,
40
            $defaultValue
41
        );
42
    }
43
44
    final public function getData()
45
    {
46
        return $this->data;
47
    }
48
49
    /**
50
     * @param mixed $key Can be an array, a string,
51
     *                          or a special formatted string
52
     *                          (eg 'app/path/project').
53
     * @param mixed $value The value to be stored.
54
     *
55
     * @return bool True on success and false on failure.
56
     */
57
    final public function setData($key, $value)
58
    {
59
        if (empty($key)) {
60
            return false;
61
        }
62
        $this->data = \WebServCo\Framework\ArrayStorage::set($this->data, $key, $value);
63
        return true;
64
    }
65
66
    /**
67
     * @param mixed $key Can be an array, a string,
68
     *                          or a special formatted string
69
     *                          (eg 'app/path/project').
70
     * @param mixed $value The value to be stored.
71
     *
72
     * @return bool True on success and false on failure.
73
     */
74
    final public function setSetting($key, $value)
75
    {
76
        if (empty($key)) {
77
            return false;
78
        }
79
        $this->settings = \WebServCo\Framework\ArrayStorage::set($this->settings, $key, $value);
80
        return true;
81
    }
82
83
    final public function setting($key, $defaultValue = false)
84
    {
85
        return \WebServCo\Framework\ArrayStorage::get(
86
            $this->settings,
87
            $key,
88
            $defaultValue
89
        );
90
    }
91
92
    public function toArray()
93
    {
94
        return [
95
            'data' => $this->data,
96
            'settings' => $this->settings,
97
        ];
98
    }
99
100
    /**
101
     * Returns data if not empty, $defaultValue otherwise.
102
     *
103
     * @param string $key
104
     * @param mixed $defaultValue
105
     * @return mixed
106
     */
107
    final public function when($key, $defaultValue = false)
108
    {
109
        $data = $this->data($key, false);
110
        return !empty($data) ? $data : $defaultValue;
111
    }
112
}
113