|
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\Common\Trait; |
|
26
|
|
|
|
|
27
|
|
|
use Derafu\Lib\Core\Support\Store\Contract\DataContainerInterface; |
|
28
|
|
|
use Derafu\Lib\Core\Support\Store\DataContainer; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Trait para clases que deben implementar un sistema simple de configuración. |
|
32
|
|
|
* |
|
33
|
|
|
* @see Derafu\Lib\Core\Common\Contract\ConfigurableInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
trait ConfigurableTrait |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* Contenedor para las configuraciones. |
|
39
|
|
|
* |
|
40
|
|
|
* @var DataContainerInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected DataContainerInterface $configuration; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Reglas de esquema de las configuraciones. |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected array $configurationSchema = []; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
44 |
|
public function setConfiguration( |
|
55
|
|
|
array|DataContainerInterface $configuration |
|
56
|
|
|
): static { |
|
57
|
44 |
|
if (is_array($configuration)) { |
|
|
|
|
|
|
58
|
44 |
|
$configuration = new DataContainer( |
|
59
|
44 |
|
$configuration, |
|
60
|
44 |
|
$this->configurationSchema |
|
61
|
44 |
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
44 |
|
$this->configuration = $configuration; |
|
65
|
|
|
|
|
66
|
44 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
38 |
|
public function getConfiguration(): DataContainerInterface |
|
73
|
|
|
{ |
|
74
|
38 |
|
if (!isset($this->configuration)) { |
|
75
|
38 |
|
$this->setConfiguration([]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
38 |
|
$this->configuration->validate(); |
|
79
|
|
|
|
|
80
|
38 |
|
return $this->configuration; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function resolveConfiguration( |
|
87
|
|
|
array $configuration |
|
88
|
|
|
): DataContainerInterface { |
|
89
|
|
|
if (isset($this->configuration)) { |
|
90
|
|
|
$configuration = array_merge( |
|
91
|
|
|
$this->configuration->all(), |
|
92
|
|
|
$configuration |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $this->setConfiguration($configuration)->configuration; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|