OptionsAwareTrait   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 14
c 3
b 0
f 0
dl 0
loc 65
ccs 0
cts 16
cp 0
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptionsSchema() 0 3 1
A resolveOptions() 0 7 2
A setOptions() 0 9 2
A getOptions() 0 9 2
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
     * Asigna las opciones de la clase.
46
     *
47
     * @param array|DataContainerInterface $options
48
     * @return static
49
     */
50
    public function setOptions(array|DataContainerInterface $options): static
51
    {
52
        if (is_array($options)) {
0 ignored issues
show
introduced by
The condition is_array($options) is always true.
Loading history...
53
            $options = new DataContainer($options, $this->getOptionsSchema());
54
        }
55
56
        $this->options = $options;
57
58
        return $this;
59
    }
60
61
    /**
62
     * Obtiene las opciones de la clase.
63
     *
64
     * @return DataContainerInterface
65
     */
66
    public function getOptions(): DataContainerInterface
67
    {
68
        if (!isset($this->options)) {
69
            $this->setOptions([]);
70
        }
71
72
        $this->options->validate();
73
74
        return $this->options;
75
    }
76
77
    /**
78
     * Resuelve las opciones de la clase.
79
     *
80
     * @param array $options
81
     * @return DataContainerInterface
82
     */
83
    public function resolveOptions(array $options = []): DataContainerInterface
84
    {
85
        if (isset($this->options)) {
86
            $options = array_merge($this->options->all(), $options);
87
        }
88
89
        return $this->setOptions($options)->options;
90
    }
91
92
    /**
93
     * Obtiene el esquema de las opciones.
94
     *
95
     * @return array
96
     */
97
    public function getOptionsSchema(): array
98
    {
99
        return $this->optionsSchema ?? [];
0 ignored issues
show
Bug introduced by
The property optionsSchema does not exist on Derafu\Lib\Core\Common\Trait\OptionsAwareTrait. Did you mean options?
Loading history...
100
    }
101
}
102