Skip to content

Commit dd4739a

Browse files
committed
add TypeDecodersFactory and TypeEncodersFactory
1 parent c3f0350 commit dd4739a

6 files changed

Lines changed: 160 additions & 2 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.
3838
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-decode-encode][1].
3939

4040
```sh
41-
composer require chubbyphp/chubbyphp-decode-encode "^1.3.1"
41+
composer require chubbyphp/chubbyphp-decode-encode "^1.4"
4242
```
4343

4444
## Usage
@@ -139,6 +139,8 @@ echo $encoder->encode(
139139

140140
* [DecoderFactory][20]
141141
* [EncoderFactory][21]
142+
* [TypeDecodersFactory][22]
143+
* [TypeEncodersFactory][23]
142144

143145
## Copyright
144146

@@ -161,3 +163,5 @@ echo $encoder->encode(
161163

162164
[20]: doc/ServiceFactory/DecoderFactory.md
163165
[21]: doc/ServiceFactory/EncoderFactory.md
166+
[22]: doc/ServiceFactory/TypeDecodersFactory.md
167+
[23]: doc/ServiceFactory/TypeEncodersFactory.md

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
},
5757
"extra": {
5858
"branch-alias": {
59-
"dev-master": "1.3-dev"
59+
"dev-master": "1.4-dev"
6060
}
6161
},
6262
"scripts": {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Chubbyphp\DecodeEncode\ServiceFactory;
6+
7+
use Chubbyphp\DecodeEncode\Decoder\JsonTypeDecoder;
8+
use Chubbyphp\DecodeEncode\Decoder\JsonxTypeDecoder;
9+
use Chubbyphp\DecodeEncode\Decoder\TypeDecoderInterface;
10+
use Chubbyphp\DecodeEncode\Decoder\UrlEncodedTypeDecoder;
11+
use Chubbyphp\DecodeEncode\Decoder\YamlTypeDecoder;
12+
13+
final class TypeDecodersFactory
14+
{
15+
/**
16+
* @return array<int, TypeDecoderInterface>
17+
*/
18+
public function __invoke(): array
19+
{
20+
return [
21+
new JsonTypeDecoder(),
22+
new JsonxTypeDecoder(),
23+
new UrlEncodedTypeDecoder(),
24+
new YamlTypeDecoder(),
25+
];
26+
}
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Chubbyphp\DecodeEncode\ServiceFactory;
6+
7+
use Chubbyphp\DecodeEncode\Encoder\JsonTypeEncoder;
8+
use Chubbyphp\DecodeEncode\Encoder\JsonxTypeEncoder;
9+
use Chubbyphp\DecodeEncode\Encoder\TypeEncoderInterface;
10+
use Chubbyphp\DecodeEncode\Encoder\UrlEncodedTypeEncoder;
11+
use Chubbyphp\DecodeEncode\Encoder\YamlTypeEncoder;
12+
use Psr\Container\ContainerInterface;
13+
14+
final class TypeEncodersFactory
15+
{
16+
/**
17+
* @return array<int, TypeEncoderInterface>
18+
*/
19+
public function __invoke(ContainerInterface $container): array
20+
{
21+
/** @var array{debug: bool} $config */
22+
$config = $container->get('config');
23+
24+
$debug = $config['debug'];
25+
26+
return [
27+
new JsonTypeEncoder($debug),
28+
new JsonxTypeEncoder($debug),
29+
new UrlEncodedTypeEncoder(),
30+
new YamlTypeEncoder(),
31+
];
32+
}
33+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Chubbyphp\Tests\DecodeEncode\Unit\ServiceFactory;
6+
7+
use Chubbyphp\DecodeEncode\Decoder\JsonTypeDecoder;
8+
use Chubbyphp\DecodeEncode\Decoder\JsonxTypeDecoder;
9+
use Chubbyphp\DecodeEncode\Decoder\UrlEncodedTypeDecoder;
10+
use Chubbyphp\DecodeEncode\Decoder\YamlTypeDecoder;
11+
use Chubbyphp\DecodeEncode\ServiceFactory\TypeDecodersFactory;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* @covers \Chubbyphp\DecodeEncode\ServiceFactory\TypeDecodersFactory
16+
*
17+
* @internal
18+
*/
19+
final class TypeDecodersFactoryTest extends TestCase
20+
{
21+
public function testInvoke(): void
22+
{
23+
$factory = new TypeDecodersFactory();
24+
25+
$typeDecoders = $factory();
26+
27+
self::assertIsArray($typeDecoders);
28+
29+
self::assertCount(4, $typeDecoders);
30+
31+
self::assertInstanceOf(JsonTypeDecoder::class, array_shift($typeDecoders));
32+
33+
/** @var JsonxTypeDecoder $jsonxTypeDecoder */
34+
$jsonxTypeDecoder = array_shift($typeDecoders);
35+
self::assertInstanceOf(JsonxTypeDecoder::class, $jsonxTypeDecoder);
36+
37+
self::assertSame('application/jsonx+xml', $jsonxTypeDecoder->getContentType());
38+
39+
self::assertInstanceOf(UrlEncodedTypeDecoder::class, array_shift($typeDecoders));
40+
self::assertInstanceOf(YamlTypeDecoder::class, array_shift($typeDecoders));
41+
}
42+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Chubbyphp\Tests\DecodeEncode\Unit\ServiceFactory;
6+
7+
use Chubbyphp\DecodeEncode\Encoder\JsonTypeEncoder;
8+
use Chubbyphp\DecodeEncode\Encoder\JsonxTypeEncoder;
9+
use Chubbyphp\DecodeEncode\Encoder\UrlEncodedTypeEncoder;
10+
use Chubbyphp\DecodeEncode\Encoder\YamlTypeEncoder;
11+
use Chubbyphp\DecodeEncode\ServiceFactory\TypeEncodersFactory;
12+
use Chubbyphp\Mock\MockMethod\WithReturn;
13+
use Chubbyphp\Mock\MockObjectBuilder;
14+
use PHPUnit\Framework\TestCase;
15+
use Psr\Container\ContainerInterface;
16+
17+
/**
18+
* @covers \Chubbyphp\DecodeEncode\ServiceFactory\TypeEncodersFactory
19+
*
20+
* @internal
21+
*/
22+
final class TypeEncodersFactoryTest extends TestCase
23+
{
24+
public function testInvoke(): void
25+
{
26+
$builder = new MockObjectBuilder();
27+
28+
/** @var ContainerInterface $container */
29+
$container = $builder->create(ContainerInterface::class, [
30+
new WithReturn('get', ['config'], ['debug' => true]),
31+
]);
32+
33+
$factory = new TypeEncodersFactory();
34+
35+
$typeEncoders = $factory($container);
36+
37+
self::assertIsArray($typeEncoders);
38+
39+
self::assertCount(4, $typeEncoders);
40+
41+
self::assertInstanceOf(JsonTypeEncoder::class, array_shift($typeEncoders));
42+
43+
/** @var JsonxTypeEncoder $jsonxTypeEncoder */
44+
$jsonxTypeEncoder = array_shift($typeEncoders);
45+
self::assertInstanceOf(JsonxTypeEncoder::class, $jsonxTypeEncoder);
46+
47+
self::assertSame('application/jsonx+xml', $jsonxTypeEncoder->getContentType());
48+
49+
self::assertInstanceOf(UrlEncodedTypeEncoder::class, array_shift($typeEncoders));
50+
self::assertInstanceOf(YamlTypeEncoder::class, array_shift($typeEncoders));
51+
}
52+
}

0 commit comments

Comments
 (0)