|
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\Package\Prime\Component\Template\Service; |
|
26
|
|
|
|
|
27
|
|
|
use Derafu\Lib\Core\Package\Prime\Component\Template\Contract\DataFormatterInterface; |
|
28
|
|
|
use Derafu\Lib\Core\Support\Store\Contract\RepositoryInterface; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Servicio de formateo de datos. |
|
32
|
|
|
* |
|
33
|
|
|
* Permite recibir un valor y formatearlo según un mapa de formateos predefinido |
|
34
|
|
|
* mediante su identificador. |
|
35
|
|
|
*/ |
|
36
|
|
|
class DataFormatter implements DataFormatterInterface |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* Mapeo de identificadores a la forma que se usará para darle formato a los |
|
40
|
|
|
* valores asociados al identificador. |
|
41
|
|
|
* |
|
42
|
|
|
* @var array<string,string|array|callable|RepositoryInterface> |
|
43
|
|
|
*/ |
|
44
|
|
|
private array $formats; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Constructor del servicio. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $formats |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(array $formats = []) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->setFormats($formats); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritDoc |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setFormats(array $formats): static |
|
60
|
|
|
{ |
|
61
|
|
|
$this->formats = $formats; |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @inheritDoc |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getFormats(): array |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->formats; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @inheritDoc |
|
76
|
|
|
*/ |
|
77
|
|
|
public function addFormat( |
|
78
|
|
|
string $id, |
|
79
|
|
|
string|array|callable|RepositoryInterface $format |
|
80
|
|
|
): static { |
|
81
|
|
|
$this->formats[$id] = $format; |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritDoc |
|
88
|
|
|
*/ |
|
89
|
|
|
public function format(string $id, mixed $value): string |
|
90
|
|
|
{ |
|
91
|
|
|
// Si no hay formato, devolver como string el valor pasado. |
|
92
|
|
|
if (!isset($this->formats[$id])) { |
|
93
|
|
|
return (string) $value; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Obtener el formato que se debe utilizar. |
|
97
|
|
|
$format = $this->formats[$id]; |
|
98
|
|
|
|
|
99
|
|
|
// Si es un string es una máscara de sprint. |
|
100
|
|
|
if (is_string($format)) { |
|
101
|
|
|
return sprintf($format, $value); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Si es un arreglo es el arreglo deberá contener el valor a traducir. |
|
105
|
|
|
// Si no existe, se entregará el mismo valor como string. |
|
106
|
|
|
if (is_array($format)) { |
|
107
|
|
|
return $format[$value] ?? (string) $value; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// Si es una función se llama directamente y se retorna su resultado. |
|
111
|
|
|
if (is_callable($format)) { |
|
112
|
|
|
return $format($value); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// Si es un repositorio se busca la entidad y se retorna el string que |
|
116
|
|
|
// representa la interfaz. Cada Entidad deberá implementar __toString(). |
|
117
|
|
|
if ($format instanceof RepositoryInterface) { |
|
118
|
|
|
$entity = $format->find($value); |
|
119
|
|
|
return $entity->__toString(); |
|
120
|
|
|
} |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: