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\Foundation\Abstract; |
26
|
|
|
|
27
|
|
|
use Derafu\Lib\Core\Common\Contract\ConfigurableInterface; |
28
|
|
|
use Derafu\Lib\Core\Common\Trait\IdentifiableTrait; |
29
|
|
|
use Derafu\Lib\Core\Foundation\Contract\ServiceInterface; |
30
|
|
|
use Derafu\Lib\Core\Helper\Arr; |
31
|
|
|
use Derafu\Lib\Core\Support\Store\Contract\DataContainerInterface; |
32
|
|
|
use Derafu\Lib\Core\Support\Store\DataContainer; |
33
|
|
|
use Symfony\Component\VarExporter\LazyObjectInterface; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Clase base para los servicios de la aplicación. |
37
|
|
|
*/ |
38
|
|
|
abstract class AbstractService implements ServiceInterface |
39
|
|
|
{ |
40
|
|
|
use IdentifiableTrait; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
3 |
|
public function __toString(): string |
46
|
|
|
{ |
47
|
3 |
|
if ($this instanceof LazyObjectInterface) { |
48
|
3 |
|
return get_parent_class($this); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return static::class; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Resuelve las opciones del servicio a partir del esquema de opciones. |
56
|
|
|
* |
57
|
|
|
* @param array $options |
58
|
|
|
* @return DataContainerInterface |
59
|
|
|
*/ |
60
|
|
|
protected function resolveOptions( |
61
|
|
|
array|DataContainerInterface $options = [] |
62
|
|
|
): DataContainerInterface { |
63
|
|
|
// Si las opciones que se pasaron son un contenedor de datos se extraen |
64
|
|
|
// como arreglo. |
65
|
|
|
if ($options instanceof DataContainerInterface) { |
|
|
|
|
66
|
|
|
$options = $options->all(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Si la clase implementa ConfigurableInterface se busca si tiene |
70
|
|
|
// opciones que estén definidas en la configuración para incluir al |
71
|
|
|
// resolver las opciones haciendo merge entre las de la configuración y |
72
|
|
|
// las que se puedan haber pasado. |
73
|
|
|
if ($this instanceof ConfigurableInterface) { |
74
|
|
|
$optionsFromConfiguration = $this->getConfiguration()->get('options'); |
75
|
|
|
if ($optionsFromConfiguration) { |
76
|
|
|
$options = Arr::mergeRecursiveDistinct( |
77
|
|
|
$optionsFromConfiguration, |
78
|
|
|
$options |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Crear un nuevo contenedor con las opciones resuelvas y validar contra |
84
|
|
|
// el esquema de las opciones (si está definido). |
85
|
|
|
return new DataContainer($options, $this->getOptionsSchema()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
*/ |
91
|
|
|
public function getOptionsSchema(): array |
92
|
|
|
{ |
93
|
|
|
return $this->optionsSchema ?? []; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|