Skip to content

Commit a8c2851

Browse files
committed
[Refactor] Add codec improvements
1 parent 528d683 commit a8c2851

File tree

9 files changed

+148
-80
lines changed

9 files changed

+148
-80
lines changed

src/Codec/ChecksMediaTypes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
declare(strict_types=1);
19+
1820
namespace CloudCreativity\LaravelJsonApi\Codec;
1921

2022
/**

src/Codec/Codec.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
declare(strict_types=1);
19+
1820
namespace CloudCreativity\LaravelJsonApi\Codec;
1921

2022
use CloudCreativity\LaravelJsonApi\Contracts\ContainerInterface;
@@ -36,6 +38,11 @@ class Codec
3638
*/
3739
private Factory $factory;
3840

41+
/**
42+
* @var MediaTypeParser
43+
*/
44+
private MediaTypeParser $mediaTypeParser;
45+
3946
/**
4047
* @var ContainerInterface
4148
*/
@@ -55,17 +62,20 @@ class Codec
5562
* Codec constructor.
5663
*
5764
* @param Factory $factory
65+
* @param MediaTypeParser $mediaTypeParser
5866
* @param ContainerInterface $container
5967
* @param Encoding $encoding
6068
* @param Decoding|null $decoding
6169
*/
6270
public function __construct(
6371
Factory $factory,
72+
MediaTypeParser $mediaTypeParser,
6473
ContainerInterface $container,
6574
Encoding $encoding,
6675
?Decoding $decoding
6776
) {
6877
$this->factory = $factory;
78+
$this->mediaTypeParser = $mediaTypeParser;
6979
$this->container = $container;
7080
$this->encoding = $encoding;
7181
$this->decoding = $decoding;
@@ -135,7 +145,7 @@ public function encodes(string ...$mediaTypes): bool
135145
$encoding = $this->getEncodingMediaType();
136146

137147
return Collection::make($mediaTypes)->contains(
138-
fn($mediaType) => $encoding->equalsTo(MediaTypeParser::getInstance()->parse($mediaType))
148+
fn($mediaType) => $encoding->equalsTo($this->mediaTypeParser->parse($mediaType))
139149
);
140150
}
141151

@@ -184,7 +194,7 @@ public function decodes(string ...$mediaTypes): bool
184194
}
185195

186196
return Collection::make($mediaTypes)->contains(
187-
static fn($mediaType) => $decoding->equalsTo(MediaTypeParser::getInstance()->parse($mediaType))
197+
fn($mediaType) => $decoding->equalsTo($this->mediaTypeParser->parse($mediaType))
188198
);
189199
}
190200

@@ -213,5 +223,4 @@ public function all($request): array
213223
{
214224
return $this->decoding ? $this->decoding->getDecoder()->decode($request) : [];
215225
}
216-
217226
}

src/Codec/Decoding.php

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
* limitations under the License.
1616
*/
1717

18+
declare(strict_types=1);
19+
1820
namespace CloudCreativity\LaravelJsonApi\Codec;
1921

2022
use CloudCreativity\LaravelJsonApi\Contracts\Decoder\DecoderInterface;
2123
use CloudCreativity\LaravelJsonApi\Decoder\JsonApiDecoder;
2224
use CloudCreativity\LaravelJsonApi\Exceptions\RuntimeException;
2325
use CloudCreativity\LaravelJsonApi\Http\Headers\MediaTypeParser;
2426
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
25-
use Neomerx\JsonApi\Http\Headers\MediaType;
2627

2728
/**
2829
* Class Decoding
@@ -31,16 +32,15 @@
3132
*/
3233
class Decoding
3334
{
34-
3535
/**
3636
* @var MediaTypeInterface
3737
*/
38-
private $mediaType;
38+
private MediaTypeInterface $mediaType;
3939

4040
/**
4141
* @var DecoderInterface
4242
*/
43-
private $decoder;
43+
private DecoderInterface $decoder;
4444

4545
/**
4646
* Create a decoding.
@@ -52,7 +52,7 @@ class Decoding
5252
public static function create($mediaType, $decoder): self
5353
{
5454
if (is_string($mediaType)) {
55-
$mediaType = MediaTypeParser::getInstance()->parse($mediaType);
55+
$mediaType = MediaTypeParser::make()->parse($mediaType);
5656
}
5757

5858
if (!$mediaType instanceof MediaTypeInterface) {
@@ -146,37 +146,9 @@ public function isNotJsonApi(): bool
146146
/**
147147
* @param MediaTypeInterface $mediaType
148148
* @return bool
149-
* @todo normalization will not be necessary for neomerx/json-api:^3.0
150-
* @see https://github.com/neomerx/json-api/issues/221
151149
*/
152150
public function equalsTo(MediaTypeInterface $mediaType): bool
153151
{
154-
return $this->normalize($this->mediaType)->equalsTo(
155-
$this->normalize($mediaType)
156-
);
157-
}
158-
159-
/**
160-
* @return array
161-
*/
162-
private function getWildCardParameters(): array
163-
{
164-
return collect((array) $this->mediaType->getParameters())->filter(function ($value) {
165-
return '*' === $value;
166-
})->keys()->all();
167-
}
168-
169-
/**
170-
* @param MediaTypeInterface $mediaType
171-
* @return MediaTypeInterface
172-
*/
173-
private function normalize(MediaTypeInterface $mediaType): MediaTypeInterface
174-
{
175-
$params = collect((array) $mediaType->getParameters())->forget(
176-
$this->getWildCardParameters()
177-
)->all();
178-
179-
return new MediaType($mediaType->getType(), $mediaType->getSubType(), $params ?: null);
152+
return $mediaType->matchesTo($this->mediaType);
180153
}
181-
182154
}

src/Codec/DecodingList.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,28 @@
1515
* limitations under the License.
1616
*/
1717

18+
declare(strict_types=1);
19+
1820
namespace CloudCreativity\LaravelJsonApi\Codec;
1921

2022
use CloudCreativity\LaravelJsonApi\Contracts\Http\Headers\HeaderInterface;
2123
use CloudCreativity\LaravelJsonApi\Http\Headers\MediaTypeParser;
24+
use Countable;
25+
use Illuminate\Support\Collection;
26+
use IteratorAggregate;
2227
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
2328

2429
/**
2530
* Class DecodingList
2631
*
2732
* @package CloudCreativity\LaravelJsonApi
2833
*/
29-
class DecodingList implements \IteratorAggregate, \Countable
34+
class DecodingList implements IteratorAggregate, Countable
3035
{
31-
3236
/**
3337
* @var Decoding[]
3438
*/
35-
private $stack;
39+
private array $stack;
3640

3741
/**
3842
* @param iterable $input
@@ -41,9 +45,9 @@ class DecodingList implements \IteratorAggregate, \Countable
4145
public static function fromArray(iterable $input): self
4246
{
4347
$list = new self();
44-
$list->stack = collect($input)->map(function ($value, $key) {
45-
return Decoding::fromArray($key, $value);
46-
})->all();
48+
$list->stack = Collection::make($input)->map(
49+
static fn($value, $key) => Decoding::fromArray($key, $value)
50+
)->all();
4751

4852
return $list;
4953
}
@@ -81,7 +85,7 @@ public function prepend(Decoding ...$decodings): self
8185
public function push(Decoding ...$decodings): self
8286
{
8387
$copy = new self();
84-
$copy->stack = collect($this->stack)->merge($decodings)->all();
88+
$copy->stack = Collection::make($this->stack)->merge($decodings)->all();
8589

8690
return $copy;
8791
}
@@ -95,7 +99,7 @@ public function push(Decoding ...$decodings): self
9599
public function merge(DecodingList $decodings): self
96100
{
97101
$copy = new self();
98-
$copy->stack = collect($this->stack)->merge($decodings->stack)->all();
102+
$copy->stack = Collection::make($this->stack)->merge($decodings->stack)->all();
99103

100104
return $copy;
101105
}
@@ -142,7 +146,7 @@ public function unless(bool $test, $decodings): self
142146
*/
143147
public function find(string $mediaType): ?Decoding
144148
{
145-
return $this->equalsTo(MediaTypeParser::getInstance()->parse($mediaType));
149+
return $this->equalsTo(MediaTypeParser::make()->parse($mediaType));
146150
}
147151

148152
/**
@@ -153,7 +157,7 @@ public function find(string $mediaType): ?Decoding
153157
*/
154158
public function equalsTo(MediaTypeInterface $mediaType): ?Decoding
155159
{
156-
return collect($this->stack)->first(function (Decoding $decoding) use ($mediaType) {
160+
return Collection::make($this->stack)->first(function (Decoding $decoding) use ($mediaType) {
157161
return $decoding->equalsTo($mediaType);
158162
});
159163
}
@@ -178,7 +182,7 @@ public function forHeader(HeaderInterface $header): ?Decoding
178182
*/
179183
public function first(): ?Decoding
180184
{
181-
return collect($this->stack)->first();
185+
return Collection::make($this->stack)->first();
182186
}
183187

184188
/**
@@ -192,9 +196,9 @@ public function all(): array
192196
/**
193197
* @inheritDoc
194198
*/
195-
public function getIterator(): \ArrayIterator
199+
public function getIterator(): \Generator
196200
{
197-
return new \ArrayIterator($this->stack);
201+
yield from $this->stack;
198202
}
199203

200204
/**
@@ -220,5 +224,4 @@ public function isNotEmpty(): bool
220224
{
221225
return !$this->isEmpty();
222226
}
223-
224227
}

src/Codec/Encoding.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
* limitations under the License.
1616
*/
1717

18+
declare(strict_types=1);
19+
1820
namespace CloudCreativity\LaravelJsonApi\Codec;
1921

2022
use CloudCreativity\LaravelJsonApi\Encoder\EncoderOptions;
2123
use CloudCreativity\LaravelJsonApi\Http\Headers\MediaTypeParser;
24+
use Illuminate\Support\Collection;
2225
use Neomerx\JsonApi\Contracts\Http\Headers\AcceptMediaTypeInterface;
2326
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
2427

@@ -56,7 +59,7 @@ public static function create(
5659
): self
5760
{
5861
if (!$mediaType instanceof MediaTypeInterface) {
59-
$mediaType = MediaTypeParser::getInstance()->parse($mediaType);
62+
$mediaType = MediaTypeParser::make()->parse($mediaType);
6063
}
6164

6265
return new self($mediaType, new EncoderOptions($options, $urlPrefix, $depth));
@@ -89,7 +92,7 @@ public static function jsonApi(int $options = 0, string $urlPrefix = null, int $
8992
public static function custom($mediaType): self
9093
{
9194
if (!$mediaType instanceof MediaTypeInterface) {
92-
$mediaType = MediaTypeParser::getInstance()->parse($mediaType);
95+
$mediaType = MediaTypeParser::make()->parse($mediaType);
9396
}
9497

9598
return new self($mediaType, null);
@@ -101,7 +104,7 @@ public static function custom($mediaType): self
101104
* @param string|null $urlPrefix
102105
* @return Encoding
103106
*/
104-
public static function fromArray($key, $value, string $urlPrefix = null)
107+
public static function fromArray($key, $value, string $urlPrefix = null): self
105108
{
106109
if (is_numeric($key)) {
107110
$key = $value;
@@ -160,9 +163,9 @@ public function hasOptions(): bool
160163
*/
161164
public function is(string ...$mediaTypes): bool
162165
{
163-
$mediaTypes = collect($mediaTypes)->map(function ($mediaType) {
164-
return MediaTypeParser::getInstance()->parse($mediaType);
165-
});
166+
$mediaTypes = Collection::make($mediaTypes)->map(
167+
fn($mediaType) => MediaTypeParser::make()->parse($mediaType)
168+
);
166169

167170
return $this->any(...$mediaTypes);
168171
}
@@ -208,5 +211,4 @@ public function accept(AcceptMediaTypeInterface $mediaType): bool
208211

209212
return $this->matchesTo($mediaType);
210213
}
211-
212214
}

0 commit comments

Comments
 (0)