|
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
|
|
|
/** |
|
45
|
|
|
* Returns data if not empty, $defaultValue otherwise. |
|
46
|
|
|
* $this->data returns data if it exists (can be empty). |
|
47
|
|
|
* This method returns data if both exists and not empty. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $key |
|
50
|
|
|
* @param mixed $defaultValue |
|
51
|
|
|
* @return mixed |
|
52
|
|
|
*/ |
|
53
|
|
|
final public function dataElse($key, $defaultValue = false) |
|
54
|
|
|
{ |
|
55
|
|
|
$data = $this->data($key, false); |
|
56
|
|
|
return !empty($data) ? $data : $defaultValue; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
final public function getData() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->data; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param mixed $key Can be an array, a string, |
|
66
|
|
|
* or a special formatted string |
|
67
|
|
|
* (eg 'app/path/project'). |
|
68
|
|
|
* @param mixed $value The value to be stored. |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool True on success and false on failure. |
|
71
|
|
|
*/ |
|
72
|
|
|
final public function setData($key, $value) |
|
73
|
|
|
{ |
|
74
|
|
|
if (empty($key)) { |
|
75
|
|
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
$this->data = \WebServCo\Framework\ArrayStorage::set($this->data, $key, $value); |
|
78
|
|
|
return true; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param mixed $key Can be an array, a string, |
|
83
|
|
|
* or a special formatted string |
|
84
|
|
|
* (eg 'app/path/project'). |
|
85
|
|
|
* @param mixed $value The value to be stored. |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool True on success and false on failure. |
|
88
|
|
|
*/ |
|
89
|
|
|
final public function setSetting($key, $value) |
|
90
|
|
|
{ |
|
91
|
|
|
if (empty($key)) { |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
$this->settings = \WebServCo\Framework\ArrayStorage::set($this->settings, $key, $value); |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
final public function setting($key, $defaultValue = false) |
|
99
|
|
|
{ |
|
100
|
|
|
return \WebServCo\Framework\ArrayStorage::get( |
|
101
|
|
|
$this->settings, |
|
102
|
|
|
$key, |
|
103
|
|
|
$defaultValue |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function toArray() |
|
108
|
|
|
{ |
|
109
|
|
|
return [ |
|
110
|
|
|
'data' => $this->data, |
|
111
|
|
|
'settings' => $this->settings, |
|
112
|
|
|
]; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|