Passed
Push — master ( ea120c...3a5b64 )
by Radu
01:10
created

AbstractLibrary::when()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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