-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodecModel.php
More file actions
140 lines (110 loc) · 3.21 KB
/
CodecModel.php
File metadata and controls
140 lines (110 loc) · 3.21 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace SyncEngine\Model;
use SyncEngine\Model\Abstract\ServiceModel;
use SyncEngine\Model\Interface\Configurable;
use SyncEngine\Model\Trait\Config;
use SyncEngine\Model\Trait\Fields;
use SyncEngine\Service\Interface\CodecInterface;
use SyncEngine\Service\Locator\Codecs;
abstract class CodecModel extends ServiceModel implements Configurable
{
use Config;
use Fields;
const SERVICE = Codecs::class;
/**
* The type of codec, can be used for categorizing.
*
* @var string
*/
public string $type = '';
/**
* Human-readable name used in the interface.
*
* @var string
*/
public string $name = '';
/**
* Human-readable description used in the interface.
*
* @var string
*/
public string $description = '';
private CodecInterface $codec;
public function getType(): string
{
return $this->type;
}
public function getName(): string
{
return $this->name;
}
public function getDescription(): string
{
return $this->description;
}
/**
* @return string[]
*/
abstract public function getFormats(): array;
abstract public function getContentType( array $config = [], string $format = '' ): string;
abstract public function initEncoder( array $config = [] ): CodecInterface;
public function getFormat( ?iterable $config = null ): string
{
$formats = $this->getFormats();
return reset( $formats );
}
public function setEncoder( array $config = [] ): static
{
$this->codec = $this->initEncoder( $config );
return $this;
}
public function getEncoder( ?array $config = null ): ?CodecInterface
{
if ( is_array( $config ) ) {
return $this->initEncoder( $config );
}
if ( ! isset( $this->codec ) && $this->getConfig() ) {
$this->setEncoder( $this->getConfig() );
}
return $this->codec; // Maybe: throw new \ErrorException( 'Encoder is not initialized.' );
}
public function encode( $value, ?string $format = null, ?array $config = null ): string
{
return $this->getEncoder( $config )->encode( $value, $format ?? $this->getFormat( $config ) );
}
public function decode( string $value, ?string $format = null, ?array $config = null ): mixed
{
return $this->getEncoder( $config )->decode( $value, $format ?? $this->getFormat( $config ) );
}
public function getFields( array $defaults = [], array $filters = [] ): array
{
$fields = $this->getCodecFields( $defaults );
if ( $filters ) {
$fields = $this->filterFieldsBy( $filters, $fields, clean: true );
}
return $fields;
}
public function getEncodeFields( array $defaults = [], array $filters = [] ): array
{
return $this->getFields( $defaults, [ ...$filters, '_direction' => 'encode' ] );
}
public function getDecodeFields( array $defaults = [], array $filters = [] ): array
{
return $this->getFields( $defaults, [ ...$filters, '_direction' => 'decode' ] );
}
abstract function getCodecFields( array $defaults = [] ): array;
public function normalize(): array
{
$props = [
'_class' => $this->getClassLocator(),
'type' => $this->getType(),
'name' => $this->getName(),
'description' => $this->getDescription(),
'fields' => $this->getFields(),
];
if ( $this->isFromModule() ) {
$props['module'] = $this->getModule()->getName();
}
return $props;
}
}