-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInMemoryEncoderRegistry.php
More file actions
52 lines (44 loc) · 1.33 KB
/
InMemoryEncoderRegistry.php
File metadata and controls
52 lines (44 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
namespace KaririCode\Serializer\Core;
use KaririCode\Serializer\Contract\Encoder;
use KaririCode\Serializer\Contract\EncoderRegistry;
use KaririCode\Serializer\Exception\SerializationException;
/**
* In-memory encoder registry backed by a plain PHP array.
*
* Throws SerializationException on duplicate registration or unknown format lookup.
*
* @package KaririCode\Serializer\Core
* @author Walmir Silva <walmir.silva@kariricode.org>
* @since 3.1.0 ARFA 1.3
*/
final class InMemoryEncoderRegistry implements EncoderRegistry
{
/** @var array<string, Encoder> */
private array $encoders = [];
#[\Override]
public function register(Encoder $encoder): void
{
$format = $encoder->getFormat();
if (isset($this->encoders[$format])) {
throw SerializationException::duplicateEncoder($format);
}
$this->encoders[$format] = $encoder;
}
#[\Override]
public function resolve(string $format): Encoder
{
return $this->encoders[$format] ?? throw SerializationException::unsupportedFormat($format);
}
#[\Override]
public function has(string $format): bool
{
return isset($this->encoders[$format]);
}
#[\Override]
public function formats(): array
{
return array_keys($this->encoders);
}
}