-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializationContextImpl.php
More file actions
64 lines (54 loc) · 1.53 KB
/
SerializationContextImpl.php
File metadata and controls
64 lines (54 loc) · 1.53 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
53
54
55
56
57
58
59
60
61
62
63
64
<?php
declare(strict_types=1);
namespace KaririCode\Serializer\Core;
use KaririCode\Serializer\Contract\SerializationContext;
/**
* Immutable serialization context carrying the active format and parameters.
*
* Uses the named-constructor pattern: instantiate via `SerializationContextImpl::create()`.
* `withFormat()` and `withParameters()` return new instances (full immutability).
*
* @package KaririCode\Serializer\Core
* @author Walmir Silva <walmir.silva@kariricode.org>
* @since 3.1.0 ARFA 1.3
*/
final readonly class SerializationContextImpl implements SerializationContext
{
/**
* @param array<string, mixed> $parameters
*/
private function __construct(
private string $format,
private array $parameters,
) {
}
public static function create(string $format = 'json'): self
{
return new self($format, []);
}
#[\Override]
public function getFormat(): string
{
return $this->format;
}
#[\Override]
public function getParameter(string $key, mixed $default = null): mixed
{
return $this->parameters[$key] ?? $default;
}
#[\Override]
public function getParameters(): array
{
return $this->parameters;
}
#[\Override]
public function withFormat(string $format): static
{
return new self($format, $this->parameters);
}
#[\Override]
public function withParameters(array $parameters): static
{
return new self($this->format, [...$this->parameters, ...$parameters]);
}
}