1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Derafu: Biblioteca PHP (Núcleo). |
7
|
|
|
* Copyright (C) Derafu <https://www.derafu.org> |
8
|
|
|
* |
9
|
|
|
* Este programa es software libre: usted puede redistribuirlo y/o modificarlo |
10
|
|
|
* bajo los términos de la Licencia Pública General Affero de GNU publicada por |
11
|
|
|
* la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o |
12
|
|
|
* (a su elección) cualquier versión posterior de la misma. |
13
|
|
|
* |
14
|
|
|
* Este programa se distribuye con la esperanza de que sea útil, pero SIN |
15
|
|
|
* GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD |
16
|
|
|
* PARA UN PROPÓSITO DETERMINADO. Consulte los detalles de la Licencia Pública |
17
|
|
|
* General Affero de GNU para obtener una información más detallada. |
18
|
|
|
* |
19
|
|
|
* Debería haber recibido una copia de la Licencia Pública General Affero de GNU |
20
|
|
|
* junto a este programa. |
21
|
|
|
* |
22
|
|
|
* En caso contrario, consulte <http://www.gnu.org/licenses/agpl.html>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Derafu\Lib\Core\Support\Store\Abstract; |
26
|
|
|
|
27
|
|
|
use ArrayAccess; |
28
|
|
|
use ArrayObject; |
29
|
|
|
use Derafu\Lib\Core\Helper\Selector; |
30
|
|
|
use Derafu\Lib\Core\Support\Store\Contract\StoreInterface; |
31
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
32
|
|
|
use Doctrine\Common\Collections\Criteria; |
33
|
|
|
use InvalidArgumentException; |
34
|
|
|
use Traversable; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Clase base para todos los almacenamientos. |
38
|
|
|
*/ |
39
|
|
|
abstract class AbstractStore implements StoreInterface |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* Colección de datos almacenados. |
43
|
|
|
* |
44
|
|
|
* @var ArrayCollection |
45
|
|
|
*/ |
46
|
|
|
protected ArrayCollection $data; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
|
|
public function collection(): ArrayCollection |
52
|
|
|
{ |
53
|
|
|
return $this->data; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
48 |
|
public function all(): array |
60
|
|
|
{ |
61
|
48 |
|
return $this->toArray(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritDoc} |
66
|
|
|
*/ |
67
|
25 |
|
public function set(string $key, mixed $value): static |
68
|
|
|
{ |
69
|
25 |
|
$data = $this->toArray(); |
70
|
25 |
|
Selector::set($data, $key, $value); |
71
|
25 |
|
$this->data = $this->createFrom($data); |
72
|
|
|
|
73
|
25 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritDoc} |
78
|
|
|
*/ |
79
|
64 |
|
public function get(string $key, mixed $default = null): mixed |
80
|
|
|
{ |
81
|
64 |
|
return Selector::get($this->toArray(), $key, $default); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
4 |
|
public function has(string $key): bool |
88
|
|
|
{ |
89
|
4 |
|
return Selector::has($this->toArray(), $key); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
6 |
|
public function clear(?string $key = null): void |
96
|
|
|
{ |
97
|
6 |
|
if ($key === null) { |
98
|
6 |
|
$this->data = $this->createFrom([]); |
99
|
|
|
} else { |
100
|
|
|
$data = $this->toArray(); |
101
|
|
|
Selector::clear($data, $key); |
102
|
|
|
$this->data = $this->createFrom($data); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
1 |
|
public function matching(Criteria $criteria): ArrayCollection |
110
|
|
|
{ |
111
|
1 |
|
return $this->createFrom($this->data->matching($criteria)->toArray()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritDoc} |
116
|
|
|
*/ |
117
|
|
|
public function offsetSet(mixed $offset, mixed $value): void |
118
|
|
|
{ |
119
|
|
|
$this->set((string) $offset, $value); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritDoc} |
124
|
|
|
*/ |
125
|
|
|
public function offsetGet(mixed $offset): mixed |
126
|
|
|
{ |
127
|
|
|
return $this->get((string) $offset); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritDoc} |
132
|
|
|
*/ |
133
|
|
|
public function offsetExists(mixed $offset): bool |
134
|
|
|
{ |
135
|
|
|
return $this->has((string) $offset); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritDoc} |
140
|
|
|
*/ |
141
|
|
|
public function offsetUnset(mixed $offset): void |
142
|
|
|
{ |
143
|
|
|
$this->clear((string) $offset); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Crea una nueva instancia de ArrayCollection, para usar o asignar a la |
148
|
|
|
* propiedad $data de la clase. |
149
|
|
|
* |
150
|
|
|
* @param array|ArrayAccess|ArrayObject $data |
151
|
|
|
* @return ArrayCollection |
152
|
|
|
*/ |
153
|
87 |
|
protected function createFrom( |
154
|
|
|
array|ArrayAccess|ArrayObject $data |
155
|
|
|
): ArrayCollection { |
156
|
87 |
|
if ($data instanceof ArrayObject) { |
|
|
|
|
157
|
1 |
|
$data = (array) $data; |
158
|
87 |
|
} elseif ($data instanceof ArrayAccess) { |
|
|
|
|
159
|
1 |
|
if ($data instanceof Traversable) { |
160
|
1 |
|
$data = iterator_to_array($data); |
161
|
|
|
} else { |
162
|
|
|
throw new InvalidArgumentException( |
163
|
|
|
'ArrayAccess debe implementar Traversable para ser convertible.' |
164
|
|
|
); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
87 |
|
return new ArrayCollection($data); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Entrega los elementos de $data como arreglo. |
173
|
|
|
* |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
77 |
|
protected function toArray(): array |
177
|
|
|
{ |
178
|
77 |
|
return $this->data->toArray(); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|