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 opciones. |
32
|
|
|
* |
33
|
|
|
* @see Derafu\Lib\Core\Common\Contract\OptionsAwareInterface |
34
|
|
|
*/ |
35
|
|
|
trait OptionsAwareTrait |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Contenedor para las opciones. |
39
|
|
|
* |
40
|
|
|
* @var DataContainerInterface |
41
|
|
|
*/ |
42
|
|
|
protected DataContainerInterface $options; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
*/ |
47
|
|
|
public function setOptions(array|DataContainerInterface $options): static |
48
|
|
|
{ |
49
|
|
|
if (is_array($options)) { |
|
|
|
|
50
|
|
|
$options = new DataContainer($options, $this->getOptionsSchema()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->options = $options; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritDoc} |
60
|
|
|
*/ |
61
|
|
|
public function getOptions(): DataContainerInterface |
62
|
|
|
{ |
63
|
|
|
if (!isset($this->options)) { |
64
|
|
|
$this->setOptions([]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->options->validate(); |
68
|
|
|
|
69
|
|
|
return $this->options; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
|
|
public function resolveOptions(array $options = []): DataContainerInterface |
76
|
|
|
{ |
77
|
|
|
if (isset($this->options)) { |
78
|
|
|
$options = array_merge($this->options->all(), $options); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->setOptions($options)->options; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
public function getOptionsSchema(): array |
88
|
|
|
{ |
89
|
|
|
return $this->optionsSchema ?? []; |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|