Skip to content

Commit

Permalink
use-chubbyphp-decode-encode
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed Aug 7, 2022
1 parent 1bb11a5 commit 615e228
Show file tree
Hide file tree
Showing 26 changed files with 384 additions and 388 deletions.
16 changes: 8 additions & 8 deletions README.md
Expand Up @@ -27,6 +27,7 @@ A simple serialization.
## Requirements

* php: ^8.0
* chubbyphp/chubbyphp-decode-encode: ^1.0.0
* doctrine/inflector: ^1.4.4|^2.0.4
* psr/http-message: ^1.0.1
* psr/link: ^1.0
Expand All @@ -38,14 +39,13 @@ A simple serialization.
* pimple/pimple: ^3.5
* psr/container: ^1.1.2|^2.0.2
* symfony/dependency-injection: ^4.4.38|^5.4.5|^6.0 (symfony integration)
* symfony/yaml: ^4.4.38|^5.4.5|^6.0 (application/x-yaml support)

## Installation

Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-serialization][1].

```sh
composer require chubbyphp/chubbyphp-serialization "^3.2"
composer require chubbyphp/chubbyphp-serialization "^3.3"
```

## Usage
Expand Down Expand Up @@ -155,12 +155,12 @@ composer require chubbyphp/chubbyphp-serialization "^3.2"
```php
<?php

use Chubbyphp\Serialization\Encoder\Encoder;
use Chubbyphp\Serialization\Encoder\JsonTypeEncoder;
use Chubbyphp\Serialization\Encoder\JsonxTypeEncoder;
use Chubbyphp\Serialization\Encoder\UrlEncodedTypeEncoder;
use Chubbyphp\Serialization\Encoder\XmlTypeEncoder;
use Chubbyphp\Serialization\Encoder\YamlTypeEncoder;
use Chubbyphp\DecodeEncode\Encoder\Encoder;
use Chubbyphp\DecodeEncode\Encoder\JsonTypeEncoder;
use Chubbyphp\DecodeEncode\Encoder\JsonxTypeEncoder;
use Chubbyphp\DecodeEncode\Encoder\UrlEncodedTypeEncoder;
use Chubbyphp\DecodeEncode\Encoder\XmlTypeEncoder;
use Chubbyphp\DecodeEncode\Encoder\YamlTypeEncoder;
use Chubbyphp\Serialization\Normalizer\Normalizer;
use Chubbyphp\Serialization\Normalizer\NormalizerObjectMappingRegistry;
use Chubbyphp\Serialization\Serializer;
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Expand Up @@ -13,11 +13,11 @@
"php": "^8.0",
"ext-dom": "*",
"ext-json": "*",
"chubbyphp/chubbyphp-decode-encode": "^1.0.0",
"doctrine/inflector": "^1.4.4|^2.0.4",
"psr/http-message": "^1.0.1",
"psr/link": "^1.0",
"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 @@ -51,7 +51,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.2-dev"
"dev-master": "3.3-dev"
}
},
"scripts": {
Expand All @@ -66,7 +66,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=86 --verbose --coverage=build/phpunit",
"test:infection": "vendor/bin/infection --threads=$(nproc) --min-msi=83 --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/Encoder/Encoder.md
@@ -1,35 +1,5 @@
# Encoder

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

use Chubbyphp\Serialization\Encoder\Encoder;
use Chubbyphp\Serialization\Encoder\JsonTypeEncoder;
use Chubbyphp\Serialization\Encoder\JsonxTypeEncoder;
use Chubbyphp\Serialization\Encoder\UrlEncodedTypeEncoder;
use Chubbyphp\Serialization\Encoder\XmlTypeEncoder;
use Chubbyphp\Serialization\Encoder\YamlTypeEncoder;

$encoder = new Encoder([
new JsonTypeEncoder(),
new JsonxTypeEncoder(),
new UrlEncodedTypeEncoder(),
new XmlTypeEncoder(),
new YamlTypeEncoder()
]);

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

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

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

use Chubbyphp\Serialization\Encoder\JsonTypeEncoder;

$encoderType = new JsonTypeEncoder();

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

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

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

use Chubbyphp\Serialization\Encoder\JsonxTypeEncoder;

$encoderType = new JsonxTypeEncoder();

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

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

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

use Chubbyphp\Serialization\Encoder\UrlEncodedTypeEncoder;

$encoderType = new UrlEncodedTypeEncoder();

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

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

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

use Chubbyphp\Serialization\Encoder\XmlTypeEncoder;

$encoderType = new XmlTypeEncoder();

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

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

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

use Chubbyphp\Serialization\Encoder\YamlTypeEncoder;

$encoderType = new YamlTypeEncoder();

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

echo $encoderType->encode(['name' => 'php']);
// 'name: php'
```
[1]: https://github.com/chubbyphp/chubbyphp-decode-encode/blob/master/doc/Encoder/YamlTypeEncoder.md
3 changes: 0 additions & 3 deletions phpstan.neon
Expand Up @@ -3,9 +3,6 @@ parameters:
-
message: '/no value type specified in iterable type array/'
path: %currentWorkingDirectory%/src/*.php
-
message: '/Method Chubbyphp\\Serialization\\Encoder\\JsonTypeEncoder\:\:encode\(\) should return string but returns string\|false\./'
path: %currentWorkingDirectory%/src/Encoder/JsonTypeEncoder.php
-
message: '//'
path: %currentWorkingDirectory%/src/Encoder/JsonxTypeEncoder.php
Expand Down
49 changes: 31 additions & 18 deletions src/Encoder/Encoder.php
Expand Up @@ -4,32 +4,41 @@

namespace Chubbyphp\Serialization\Encoder;

use Chubbyphp\DecodeEncode\Encoder\Encoder as BaseEncoder;
use Chubbyphp\DecodeEncode\Encoder\EncoderInterface as BaseEncoderInterface;
use Chubbyphp\DecodeEncode\LogicException;
use Chubbyphp\Serialization\SerializerLogicException;

/**
* @deprecated use \Chubbyphp\DecodeEncode\Encoder\Encoder
*/
final class Encoder implements EncoderInterface
{
/**
* @var array<string, TypeEncoderInterface>
*/
private array $encoderTypes;
private BaseEncoderInterface $encoder;

/**
* @param array<int, TypeEncoderInterface> $encoderTypes
*/
public function __construct(array $encoderTypes)
{
$this->encoderTypes = [];
foreach ($encoderTypes as $encoderType) {
$this->addTypeEncoder($encoderType);
}
$this->encoder = new BaseEncoder($encoderTypes);
}

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

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

/**
Expand All @@ -39,15 +48,19 @@ public function getContentTypes(): array
*/
public function encode(array $data, string $contentType): string
{
if (isset($this->encoderTypes[$contentType])) {
return $this->encoderTypes[$contentType]->encode($data);
}
@trigger_error(
sprintf(
'%s:encode use %s:encode',
self::class,
BaseEncoder::class
),
E_USER_DEPRECATED
);

throw SerializerLogicException::createMissingContentType($contentType);
}

private function addTypeEncoder(TypeEncoderInterface $encoderType): void
{
$this->encoderTypes[$encoderType->getContentType()] = $encoderType;
try {
return $this->encoder->encode($data, $contentType);
} catch (LogicException $e) {
throw new SerializerLogicException($e->getMessage(), $e->getCode(), $e);
}
}
}
18 changes: 5 additions & 13 deletions src/Encoder/EncoderInterface.php
Expand Up @@ -4,19 +4,11 @@

namespace Chubbyphp\Serialization\Encoder;

use Chubbyphp\Serialization\SerializerLogicException;
use Chubbyphp\DecodeEncode\Encoder\EncoderInterface as BaseEncoderInterface;

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

/**
* @param array<string, null|array|bool|float|int|string> $data
*
* @throws SerializerLogicException
*/
public function encode(array $data, string $contentType): string;
}
35 changes: 28 additions & 7 deletions src/Encoder/JsonTypeEncoder.php
Expand Up @@ -4,27 +4,48 @@

namespace Chubbyphp\Serialization\Encoder;

use Chubbyphp\DecodeEncode\Encoder\JsonTypeEncoder as BaseJsonTypeEncoder;

/**
* @deprecated use \Chubbyphp\DecodeEncode\Encoder\JsonTypeEncoder
*/
final class JsonTypeEncoder implements TypeEncoderInterface
{
public function __construct(private bool $prettyPrint = false)
private BaseJsonTypeEncoder $jsonTypeEncoder;

public function __construct(bool $prettyPrint = false)
{
$this->jsonTypeEncoder = new BaseJsonTypeEncoder($prettyPrint);
}

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

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

/**
* @param array<string, null|array|bool|float|int|string> $data
*/
public function encode(array $data): string
{
$options = JSON_PRESERVE_ZERO_FRACTION | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_IGNORE;
if ($this->prettyPrint) {
$options |= JSON_PRETTY_PRINT;
}
@trigger_error(
sprintf(
'%s:encode use %s:encode',
self::class,
BaseJsonTypeEncoder::class
),
E_USER_DEPRECATED
);

return json_encode($data, $options);
return $this->jsonTypeEncoder->encode($data);
}
}

0 comments on commit 615e228

Please sign in to comment.