1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Daniel Król <[email protected]> |
4
|
|
|
* @license MIT |
5
|
|
|
* @package Mleko\Alchemist |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mleko\Alchemist\Normalizer; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
use Mleko\Alchemist\Exception\NormalizerNotFound; |
16
|
|
|
use Mleko\Alchemist\Normalizer; |
17
|
|
|
use Mleko\Alchemist\Type; |
18
|
|
|
|
19
|
|
|
class ChainNormalizer implements Normalizer, NormalizerAware |
20
|
|
|
{ |
21
|
|
|
/** @var Normalizer[] */ |
22
|
|
|
private $normalizers = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* ChainNormalizer constructor. |
26
|
|
|
* @param Normalizer[] $normalizers |
27
|
|
|
*/ |
28
|
9 |
|
public function __construct(array $normalizers) { |
29
|
9 |
|
foreach ($normalizers as $normalizer) { |
30
|
7 |
|
$this->addNormalizer($normalizer); |
31
|
|
|
} |
32
|
9 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @inheritdoc |
36
|
|
|
*/ |
37
|
4 |
|
public function normalize($value, string $format, array $context = []) { |
38
|
4 |
|
$type = Type::fromValue($value); |
39
|
4 |
|
$normalizer = $this->findTypeFormatNormalizer($type, $format); |
40
|
4 |
|
if (null === $normalizer) { |
41
|
1 |
|
throw new NormalizerNotFound($type, $format); |
42
|
|
|
} |
43
|
3 |
|
return $normalizer->normalize($value, $format, $context); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @inheritdoc |
48
|
|
|
*/ |
49
|
4 |
|
public function denormalize($data, Type $type, string $format, array $context = []) { |
50
|
4 |
|
$normalizer = $this->findTypeFormatNormalizer($type, $format); |
51
|
4 |
|
if (null === $normalizer) { |
52
|
1 |
|
throw new NormalizerNotFound($type, $format); |
53
|
|
|
} |
54
|
3 |
|
return $normalizer->denormalize($data, $type, $format, $context); |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
public function canProcess(Type $type, string $format): bool { |
58
|
4 |
|
return null !== $this->findTypeFormatNormalizer($type, $format); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function setNormalizer(Normalizer $subNormalizer): void { |
62
|
1 |
|
foreach ($this->normalizers as $normalizer) { |
63
|
1 |
|
if ($normalizer instanceof NormalizerAware) { |
64
|
1 |
|
$normalizer->setNormalizer($subNormalizer); |
65
|
|
|
} |
66
|
|
|
} |
67
|
1 |
|
} |
68
|
|
|
|
69
|
9 |
|
private function findTypeFormatNormalizer(Type $type, string $format): ?Normalizer { |
70
|
9 |
|
foreach ($this->normalizers as $normalizer) { |
71
|
7 |
|
if ($normalizer->canProcess($type, $format)) { |
72
|
7 |
|
return $normalizer; |
73
|
|
|
} |
74
|
|
|
} |
75
|
4 |
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
7 |
|
private function addNormalizer(Normalizer $normalizer) { |
79
|
7 |
|
if ($normalizer instanceof NormalizerAware) { |
80
|
4 |
|
$normalizer->setNormalizer($this); |
81
|
|
|
} |
82
|
7 |
|
array_unshift($this->normalizers, $normalizer); |
83
|
7 |
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|