|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Bluz Framework Component |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Bluz PHP Team |
|
7
|
|
|
* @link https://github.com/bluzphp/framework |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Bluz\Container; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Container of data for an object |
|
16
|
|
|
* |
|
17
|
|
|
* @package Bluz\Common |
|
18
|
|
|
* @author Anton Shevchuk |
|
19
|
|
|
* @link https://github.com/bluzphp/framework/wiki/Trait-Container |
|
20
|
|
|
*/ |
|
21
|
|
|
trait Container |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var array Container of elements |
|
25
|
|
|
*/ |
|
26
|
|
|
protected array $container = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Set key/value pair |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $key |
|
32
|
|
|
* @param mixed $value |
|
33
|
|
|
* |
|
34
|
|
|
* @return void |
|
35
|
|
|
*/ |
|
36
|
5 |
|
protected function doSetContainer(string $key, mixed $value): void |
|
37
|
|
|
{ |
|
38
|
5 |
|
$this->container[$key] = $value; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get value by key |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $key |
|
45
|
|
|
* |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
5 |
|
protected function doGetContainer(string $key): mixed |
|
49
|
|
|
{ |
|
50
|
5 |
|
if ($this->doContainsContainer($key)) { |
|
51
|
5 |
|
return $this->container[$key]; |
|
52
|
|
|
} |
|
53
|
5 |
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Check contains key in container |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $key |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
5 |
|
protected function doContainsContainer(string $key): bool |
|
64
|
|
|
{ |
|
65
|
5 |
|
return array_key_exists($key, $this->container); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Delete value by key |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $key |
|
72
|
|
|
* |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
5 |
|
protected function doDeleteContainer(string $key): void |
|
76
|
|
|
{ |
|
77
|
5 |
|
unset($this->container[$key]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Sets all data in the row from an array |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $data |
|
84
|
|
|
* |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
4 |
|
public function setFromArray(array $data): void |
|
88
|
|
|
{ |
|
89
|
4 |
|
foreach ($data as $key => $value) { |
|
90
|
4 |
|
$this->container[$key] = $value; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns the column/value data as an array |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
4 |
|
public function toArray(): array |
|
100
|
|
|
{ |
|
101
|
4 |
|
return $this->container; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Reset the container array |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function resetArray(): void |
|
110
|
|
|
{ |
|
111
|
1 |
|
foreach ($this->container as &$value) { |
|
112
|
1 |
|
$value = null; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|