AbstractLibrary::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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