Skip to content

Commit

Permalink
Merge pull request #44 from chubbyphp/use-chubbyphp-decode-encode
Browse files Browse the repository at this point in the history
use-chubbyphp-decode-encode
  • Loading branch information
dominikzogg committed Aug 2, 2022
2 parents 0fa842d + 086ed06 commit 2309f91
Show file tree
Hide file tree
Showing 25 changed files with 329 additions and 333 deletions.
16 changes: 8 additions & 8 deletions README.md
Expand Up @@ -26,9 +26,9 @@ A simple deserialization.
## Requirements

* php: ^8.0
* chubbyphp/chubbyphp-decode-encode: ^1.0.0
* psr/http-message: ^1.0.1
* psr/log: ^1.1.4|^2.0|^3.0
* symfony/yaml: ^4.4.38|^5.4.5|^6.0

## Suggest

Expand All @@ -42,7 +42,7 @@ A simple deserialization.
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-deserialization][1].

```sh
composer require chubbyphp/chubbyphp-deserialization "^3.4"
composer require chubbyphp/chubbyphp-deserialization "^3.5"
```

## Usage
Expand Down Expand Up @@ -96,12 +96,12 @@ composer require chubbyphp/chubbyphp-deserialization "^3.4"
```php
<?php

use Chubbyphp\Deserialization\Decoder\Decoder;
use Chubbyphp\Deserialization\Decoder\JsonTypeDecoder;
use Chubbyphp\Deserialization\Decoder\JsonxTypeDecoder;
use Chubbyphp\Deserialization\Decoder\UrlEncodedTypeDecoder;
use Chubbyphp\Deserialization\Decoder\XmlTypeDecoder;
use Chubbyphp\Deserialization\Decoder\YamlTypeDecoder;
use Chubbyphp\DecodeEncode\Decoder\Decoder;
use Chubbyphp\DecodeEncode\Decoder\JsonTypeDecoder;
use Chubbyphp\DecodeEncode\Decoder\JsonxTypeDecoder;
use Chubbyphp\DecodeEncode\Decoder\UrlEncodedTypeDecoder;
use Chubbyphp\DecodeEncode\Decoder\XmlTypeDecoder;
use Chubbyphp\DecodeEncode\Decoder\YamlTypeDecoder;
use Chubbyphp\Deserialization\Denormalizer\Denormalizer;
use Chubbyphp\Deserialization\Denormalizer\DenormalizerObjectMappingRegistry;
use Chubbyphp\Deserialization\Deserializer;
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Expand Up @@ -14,9 +14,9 @@
"ext-dom": "*",
"ext-json": "*",
"ext-mbstring": "*",
"chubbyphp/chubbyphp-decode-encode": "^1.0.0",
"psr/http-message": "^1.0.1",
"psr/log": "^1.1.4|^2.0|^3.0",
"symfony/yaml": "^4.4.38|^5.4.5|^6.0"
"psr/log": "^1.1.4|^2.0|^3.0"
},
"require-dev": {
"chubbyphp/chubbyphp-container": "^2.1",
Expand Down Expand Up @@ -50,7 +50,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.4-dev"
"dev-master": "3.5-dev"
}
},
"scripts": {
Expand All @@ -65,7 +65,7 @@
"@test:cs"
],
"test:cs": "mkdir -p build && PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation --cache-file=build/phpcs.cache",
"test:infection": "vendor/bin/infection --threads=$(nproc) --min-msi=94 --verbose --coverage=build/phpunit",
"test:infection": "vendor/bin/infection --threads=$(nproc) --min-msi=93 --verbose --coverage=build/phpunit",
"test:integration": "vendor/bin/phpunit --testsuite=Integration --cache-result-file=build/phpunit/result.cache",
"test:lint": "mkdir -p build && find src tests -name '*.php' -print0 | xargs -0 -n1 -P$(nproc) php -l | tee build/phplint.log",
"test:loc": "mkdir -p build && vendor/bin/phploc src | tee build/phploc.log",
Expand Down
34 changes: 2 additions & 32 deletions doc/Decoder/Decoder.md
@@ -1,35 +1,5 @@
# Decoder

```php
<?php
Use [Decoder][1] from chubbyphp/chubbyphp-decode-encode.

use Chubbyphp\Deserialization\Decoder\Decoder;
use Chubbyphp\Deserialization\Decoder\JsonTypeDecoder;
use Chubbyphp\Deserialization\Decoder\JsonxTypeDecoder;
use Chubbyphp\Deserialization\Decoder\UrlEncodedTypeDecoder;
use Chubbyphp\Deserialization\Decoder\XmlTypeDecoder;
use Chubbyphp\Deserialization\Decoder\YamlTypeDecoder;

$decoder = new Decoder([
new JsonTypeDecoder(),
new JsonxTypeDecoder(),
new UrlEncodedTypeDecoder(),
new XmlTypeDecoder(),
new YamlTypeDecoder()
]);

print_r($decoder->getContentTypes());
//[
// 'application/json',
// 'application/jsonx+xml',
// 'application/x-www-form-urlencoded',
// 'application/xml',
// 'application/x-yaml'
//]

print_r($decoder->decode(
'{"name": "php"}',
'application/json'
));
// ['name' => 'php']
```
[1]: https://github.com/chubbyphp/chubbyphp-decode-encode/blob/master/doc/Decoder/Decoder.md
14 changes: 2 additions & 12 deletions doc/Decoder/JsonTypeDecoder.md
@@ -1,15 +1,5 @@
# JsonTypeDecoder

```php
<?php
Use [JsonTypeDecoder][1] from chubbyphp/chubbyphp-decode-encode.

use Chubbyphp\Deserialization\Decoder\JsonTypeDecoder;

$decoderType = new JsonTypeDecoder();

echo $decoderType->getContentType();
// 'application/json'

print_r($decoderType->decode('{"name": "php"}'));
// ['name' => 'php']
```
[1]: https://github.com/chubbyphp/chubbyphp-decode-encode/blob/master/doc/Decoder/JsonTypeDecoder.md
14 changes: 2 additions & 12 deletions doc/Decoder/JsonxTypeDecoder.md
@@ -1,15 +1,5 @@
# JsonxTypeDecoder

```php
<?php
Use [JsonxTypeDecoder][1] from chubbyphp/chubbyphp-decode-encode.

use Chubbyphp\Deserialization\Decoder\JsonxTypeDecoder;

$decoderType = new JsonxTypeDecoder();

echo $decoderType->getContentType();
// 'application/jsonx+xml'

print_r($decoderType->decode('<json:object><json:string name="name">php</json:string></json:object>'));
// ['name' => 'php']
```
[1]: https://github.com/chubbyphp/chubbyphp-decode-encode/blob/master/doc/Decoder/JsonxTypeDecoder.md
14 changes: 2 additions & 12 deletions doc/Decoder/UrlEncodedTypeDecoder.md
@@ -1,15 +1,5 @@
# UrlEncodedTypeDecoder

```php
<?php
Use [UrlEncodedTypeDecoder][1] from chubbyphp/chubbyphp-decode-encode.

use Chubbyphp\Deserialization\Decoder\UrlEncodedTypeDecoder;

$decoderType = new UrlEncodedTypeDecoder();

echo $decoderType->getContentType();
// 'application/x-www-form-urlencoded'

print_r($decoderType->decode('name=php'));
// ['name' => 'php']
```
[1]: https://github.com/chubbyphp/chubbyphp-decode-encode/blob/master/doc/Decoder/UrlEncodedTypeDecoder.md
16 changes: 3 additions & 13 deletions doc/Decoder/XmlTypeDecoder.md
@@ -1,15 +1,5 @@
# XmlTypeDecoder (alias for Jsonx)
# XmlTypeDecoder

```php
<?php
Use [XmlTypeDecoder][1] from chubbyphp/chubbyphp-decode-encode.

use Chubbyphp\Deserialization\Decoder\XmlTypeDecoder;

$decoderType = new XmlTypeDecoder();

echo $decoderType->getContentType();
// 'application/xml'

print_r($decoderType->decode('<json:object><json:string name="name">php</json:string></json:object>'));
// ['name' => 'php']
```
[1]: https://github.com/chubbyphp/chubbyphp-decode-encode/blob/master/doc/Decoder/XmlTypeDecoder.md
14 changes: 2 additions & 12 deletions doc/Decoder/YamlTypeDecoder.md
@@ -1,15 +1,5 @@
# YamlTypeDecoder

```php
<?php
Use [YamlTypeDecoder][1] from chubbyphp/chubbyphp-decode-encode.

use Chubbyphp\Deserialization\Decoder\YamlTypeDecoder;

$decoderType = new YamlTypeDecoder();

echo $decoderType->getContentType();
// 'application/x-yaml'

print_r($decoderType->decode('name: php'));
// ['name' => 'php']
```
[1]: https://github.com/chubbyphp/chubbyphp-decode-encode/blob/master/doc/Decoder/YamlTypeDecoder.md
52 changes: 34 additions & 18 deletions src/Decoder/Decoder.php
Expand Up @@ -4,33 +4,43 @@

namespace Chubbyphp\Deserialization\Decoder;

use Chubbyphp\DecodeEncode\Decoder\Decoder as BaseDecoder;
use Chubbyphp\DecodeEncode\Decoder\DecoderInterface as BaseDecoderInterface;
use Chubbyphp\DecodeEncode\LogicException;
use Chubbyphp\DecodeEncode\RuntimeException;
use Chubbyphp\Deserialization\DeserializerLogicException;
use Chubbyphp\Deserialization\DeserializerRuntimeException;

/**
* @deprecated use \Chubbyphp\DecodeEncode\Decoder\Decoder
*/
final class Decoder implements DecoderInterface
{
/**
* @var array<string, TypeDecoderInterface>
*/
private array $decoderTypes;
private BaseDecoderInterface $decoder;

/**
* @param array<int, TypeDecoderInterface> $decoderTypes
*/
public function __construct(array $decoderTypes)
{
$this->decoderTypes = [];
foreach ($decoderTypes as $decoderType) {
$this->addTypeDecoder($decoderType);
}
$this->decoder = new BaseDecoder($decoderTypes);
}

/**
* @return array<int, string>
*/
public function getContentTypes(): array
{
return array_keys($this->decoderTypes);
@trigger_error(
sprintf(
'%s:getContentTypes use %s:getContentTypes',
self::class,
BaseDecoder::class
),
E_USER_DEPRECATED
);

return $this->decoder->getContentTypes();
}

/**
Expand All @@ -41,15 +51,21 @@ public function getContentTypes(): array
*/
public function decode(string $data, string $contentType): array
{
if (isset($this->decoderTypes[$contentType])) {
return $this->decoderTypes[$contentType]->decode($data);
}
@trigger_error(
sprintf(
'%s:decode use %s:decode',
self::class,
BaseDecoder::class
),
E_USER_DEPRECATED
);

throw DeserializerLogicException::createMissingContentType($contentType);
}

private function addTypeDecoder(TypeDecoderInterface $decoderType): void
{
$this->decoderTypes[$decoderType->getContentType()] = $decoderType;
try {
return $this->decoder->decode($data, $contentType);
} catch (RuntimeException $e) {
throw new DeserializerRuntimeException($e->getMessage(), $e->getCode(), $e);
} catch (LogicException $e) {
throw new DeserializerLogicException($e->getMessage(), $e->getCode(), $e);
}
}
}
20 changes: 5 additions & 15 deletions src/Decoder/DecoderInterface.php
Expand Up @@ -4,21 +4,11 @@

namespace Chubbyphp\Deserialization\Decoder;

use Chubbyphp\Deserialization\DeserializerLogicException;
use Chubbyphp\Deserialization\DeserializerRuntimeException;
use Chubbyphp\DecodeEncode\Decoder\DecoderInterface as BaseDecoderInterface;

interface DecoderInterface
/**
* @deprecated use \Chubbyphp\DecodeEncode\Decoder\DecoderInterface
*/
interface DecoderInterface extends BaseDecoderInterface
{
/**
* @return array<int, string>
*/
public function getContentTypes(): array;

/**
* @throws DeserializerLogicException
* @throws DeserializerRuntimeException
*
* @return array<string, null|array|bool|float|int|string>
*/
public function decode(string $data, string $contentType): array;
}
46 changes: 35 additions & 11 deletions src/Decoder/JsonTypeDecoder.php
Expand Up @@ -4,13 +4,34 @@

namespace Chubbyphp\Deserialization\Decoder;

use Chubbyphp\DecodeEncode\Decoder\JsonTypeDecoder as BaseJsonTypeDecoder;
use Chubbyphp\DecodeEncode\RuntimeException;
use Chubbyphp\Deserialization\DeserializerRuntimeException;

/**
* @deprecated use \Chubbyphp\DecodeEncode\Decoder\JsonTypeDecoder
*/
final class JsonTypeDecoder implements TypeDecoderInterface
{
private BaseJsonTypeDecoder $jsonTypeDecoder;

public function __construct()
{
$this->jsonTypeDecoder = new BaseJsonTypeDecoder();
}

public function getContentType(): string
{
return 'application/json';
@trigger_error(
sprintf(
'%s:getContentType use %s:getContentType',
self::class,
BaseJsonTypeDecoder::class
),
E_USER_DEPRECATED
);

return $this->jsonTypeDecoder->getContentType();
}

/**
Expand All @@ -20,16 +41,19 @@ public function getContentType(): string
*/
public function decode(string $data): array
{
$decoded = json_decode($data, true);

if (JSON_ERROR_NONE !== json_last_error()) {
throw DeserializerRuntimeException::createNotParsable($this->getContentType(), json_last_error_msg());
}

if (!\is_array($decoded)) {
throw DeserializerRuntimeException::createNotParsable($this->getContentType(), 'Not an object');
@trigger_error(
sprintf(
'%s:decode use %s:decode',
self::class,
BaseJsonTypeDecoder::class
),
E_USER_DEPRECATED
);

try {
return $this->jsonTypeDecoder->decode($data);
} catch (RuntimeException $e) {
throw new DeserializerRuntimeException($e->getMessage(), $e->getCode(), $e);
}

return $decoded;
}
}

0 comments on commit 2309f91

Please sign in to comment.