1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the reva2/jsonapi. |
4
|
|
|
* |
5
|
|
|
* (c) OrbitScripts LLC <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Reva2\JsonApi\Services; |
13
|
|
|
|
14
|
|
|
use Neomerx\JsonApi\Contracts\Decoder\DecoderInterface; |
15
|
|
|
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface; |
16
|
|
|
use Reva2\JsonApi\Contracts\Services\JsonApiRegistryInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* JSON API decoders/encoders registry |
20
|
|
|
* |
21
|
|
|
* @package Reva2\JsonApi\Services |
22
|
|
|
* @author Sergey Revenko <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class JsonApiRegistry implements JsonApiRegistryInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Decoders map |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $decoders = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Encoders map |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $encoders = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function registerDecoder($name, $decoder) |
44
|
|
|
{ |
45
|
|
|
if ((!$decoder instanceof DecoderInterface) && (!$decoder instanceof \Closure)) { |
46
|
|
|
throw new \InvalidArgumentException(sprintf( |
47
|
|
|
"Decoder must be a %s instance or closure", |
48
|
|
|
DecoderInterface::class |
49
|
|
|
)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->decoders[$name] = $decoder; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function getDecoder($name) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
if (!array_key_exists($name, $this->decoders)) { |
63
|
|
|
throw new \RuntimeException(sprintf("Decoder with name '%s' is not registered", $name)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if ($this->decoders[$name] instanceof \Closure) { |
67
|
|
|
$factory = $this->decoders[$name]; |
68
|
|
|
$decoder = $factory(); |
69
|
|
|
if (!$decoder instanceof DecoderInterface) { |
70
|
|
|
throw new \LogicException(sprintf( |
71
|
|
|
"Decoder '%s' should implement %s interface", |
72
|
|
|
$name, |
73
|
|
|
DecoderInterface::class |
74
|
|
|
)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->decoders[$name] = $decoder; |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->decoders[$name]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @inheritdoc |
86
|
|
|
*/ |
87
|
|
|
public function registerEncoder($name, $encoder) |
88
|
|
|
{ |
89
|
|
|
if ((!$encoder instanceof EncoderInterface) && (!$encoder instanceof \Closure)) { |
90
|
|
|
throw new \InvalidArgumentException(sprintf( |
91
|
|
|
"Encoder must be a %s instance or closure", |
92
|
|
|
EncoderInterface::class |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->encoders[$name] = $encoder; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritdoc |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function getEncoder($name) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
if (!array_key_exists($name, $this->encoders)) { |
107
|
|
|
throw new \RuntimeException(sprintf("Encoder with name '%s' is not registered", $name)); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($this->encoders[$name] instanceof \Closure) { |
111
|
|
|
$factory = $this->encoders[$name]; |
112
|
|
|
$encoder = $factory(); |
113
|
|
|
if (!$encoder instanceof EncoderInterface) { |
114
|
|
|
throw new \LogicException(sprintf( |
115
|
|
|
"Encoder '%s' should implement %s interface", |
116
|
|
|
$name, |
117
|
|
|
EncoderInterface::class |
118
|
|
|
)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->encoders[$name] = $encoder; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $this->encoders[$name]; |
125
|
|
|
} |
126
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.