Skip to content

Commit

Permalink
v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Zogg committed Jul 4, 2020
1 parent 4aa2375 commit 7c2078c
Show file tree
Hide file tree
Showing 68 changed files with 405 additions and 2,862 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ A simple deserialization.
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-deserialization][1].

```sh
composer require chubbyphp/chubbyphp-deserialization "^2.17"
composer require chubbyphp/chubbyphp-deserialization "^3.0"
```

## Usage
Expand Down Expand Up @@ -126,7 +126,7 @@ echo $model->getName();
print_r($deserializer->getContentTypes());
//[
// 'application/json',
// 'application/x-jsonx',
// 'application/jsonx+xml',
// 'application/x-www-form-urlencoded',
// 'application/xml',
// 'application/x-yaml'
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.17-dev"
"dev-master": "3.0-dev"
}
},
"scripts": {
Expand All @@ -59,8 +59,8 @@
"@test:insights"
],
"test:cs": "mkdir -p build && 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=93 --verbose --coverage=build/phpunit",
"test:insights": "mkdir -p build && bash -c 'vendor/bin/phpinsights analyse -v --no-interaction --min-quality=82 --disable-security-check | tee build/phpinsights.log; if [ ${PIPESTATUS[0]} -ne \"0\" ]; then exit 1; fi'",
"test:infection": "vendor/bin/infection --threads=$(nproc) --min-msi=95 --verbose --coverage=build/phpunit",
"test:insights": "mkdir -p build && bash -c 'vendor/bin/phpinsights analyse -v --no-interaction --min-quality=93 --disable-security-check | tee build/phpinsights.log; if [ ${PIPESTATUS[0]} -ne \"0\" ]; then exit 1; fi'",
"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 --verbose | tee build/phploc.log",
Expand Down
5 changes: 4 additions & 1 deletion doc/Decoder/Decoder.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

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()
Expand All @@ -19,6 +21,7 @@ $decoder = new Decoder([
print_r($decoder->getContentTypes());
//[
// 'application/json',
// 'application/jsonx+xml',
// 'application/x-www-form-urlencoded',
// 'application/xml',
// 'application/x-yaml'
Expand All @@ -29,4 +32,4 @@ print_r($decoder->decode(
'application/json'
));
// ['name' => 'php']
```
```
2 changes: 1 addition & 1 deletion doc/Decoder/JsonxTypeDecoder.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use Chubbyphp\Deserialization\Decoder\JsonxTypeDecoder;
$decoderType = new JsonxTypeDecoder();

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

print_r($decoderType->decode('<json:object><json:string name="name">php</json:string></json:object>'));
// ['name' => 'php']
Expand Down
4 changes: 2 additions & 2 deletions doc/Decoder/XmlTypeDecoder.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# XmlTypeDecoder
# XmlTypeDecoder (alias for Jsonx)

```php
<?php
Expand All @@ -10,6 +10,6 @@ $decoderType = new XmlTypeDecoder();
echo $decoderType->getContentType();
// 'application/xml'

print_r($decoderType->decode('<name type="string">php</name>'));
print_r($decoderType->decode('<json:object><json:string name="name">php</json:string></json:object>'));
// ['name' => 'php']
```
9 changes: 2 additions & 7 deletions doc/Denormalizer/DenormalizerContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@ use Psr\Http\Message\ServerRequestInterface;
/** @var ServerRequestInterface $request */
$request = ...;

$context = new DenormalizerContext(['allowed_additional_field'], ['group1'], $request, true, [], true);
$context = new DenormalizerContext(['allowed_additional_field'], $request, [], true);

echo $context->getAllowedAdditionalFields();
// ['allowed_additional_field']

print_r($context->getGroups());
// ['group1']

$context->getRequest();
// instanceof ServerRequestInterface

echo $context->isResetMissingFields();
// true

echo $context->isClearMissing();
// true

$context->getAttributes();
$context->getAttribute('name');
Expand Down
8 changes: 0 additions & 8 deletions doc/Denormalizer/DenormalizerContextBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,16 @@ $request = ...;

$context = DenormalizerContextBuilder::create()
->setAllowedAdditionalFields(['allowed_additional_field'])
->setGroups(['group1'])
->setRequest($request)
->setResetMissingFields(true) // deprecated
->setClearMissing(true)
->getContext();

echo $context->getAllowedAdditionalFields();
// ['allowed_additional_field']

print_r($context->getGroups());
// ['group1']

$context->getRequest();
// instanceof ServerRequestInterface

echo $context->isResetMissingFields();
// true

echo $context->isClearMissing();
// true

Expand Down
15 changes: 0 additions & 15 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
parameters:
ignoreErrors:
-
message: '/Method Chubbyphp\\Deserialization\\Accessor\\AccessorInterface\:\:setValue\(\) has no return typehint specified\./'
path: %currentWorkingDirectory%/src/Accessor/AccessorInterface.php
-
message: '/Method Chubbyphp\\Deserialization\\Accessor\\MethodAccessor\:\:setValue\(\) has no return typehint specified\./'
path: %currentWorkingDirectory%/src/Accessor/MethodAccessor.php
-
message: '//'
path: %currentWorkingDirectory%/src/Decoder/JsonxTypeDecoder.php
-
message: '//'
path: %currentWorkingDirectory%/src/Decoder/UrlEncodedTypeDecoder.php
-
message: '//'
path: %currentWorkingDirectory%/src/Decoder/XmlTypeDecoder.php
-
message: '/Unreachable statement - code above always terminates\./'
path: %currentWorkingDirectory%/src/Denormalizer/DateTimeFieldDenormalizer.php
-
message: '/Parameter \#1 \$argument of class ReflectionClass constructor expects class\-string<T of object>\|T of object, string given\./'
path: %currentWorkingDirectory%/src/Denormalizer/DenormalizerObjectMappingRegistry.php
-
message: '/Method Chubbyphp\\Deserialization\\Denormalizer\\FieldDenormalizerInterface\:\:denormalizeField\(\) has no return typehint specified\./'
path: %currentWorkingDirectory%/src/Denormalizer/FieldDenormalizerInterface.php
9 changes: 3 additions & 6 deletions src/Accessor/AccessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@
interface AccessorInterface
{
/**
* @param object $object
* @param mixed $value
* @param mixed $value
*
* @throws DeserializerLogicException
*/
public function setValue($object, $value);
public function setValue(object $object, $value): void;

/**
* @param object $object
*
* @throws DeserializerLogicException
*
* @return mixed
*/
public function getValue($object);
public function getValue(object $object);
}
11 changes: 4 additions & 7 deletions src/Accessor/MethodAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,26 @@ public function __construct(string $property)
}

/**
* @param object $object
* @param mixed $value
* @param mixed $value
*
* @throws DeserializerLogicException
*/
public function setValue($object, $value)
public function setValue(object $object, $value): void
{
$set = 'set'.ucfirst($this->property);
if (!method_exists($object, $set)) {
throw DeserializerLogicException::createMissingMethod(get_class($object), [$set]);
}

return $object->{$set}($value);
$object->{$set}($value);
}

/**
* @param object $object
*
* @throws DeserializerLogicException
*
* @return mixed
*/
public function getValue($object)
public function getValue(object $object)
{
$get = 'get'.ucfirst($this->property);
$has = 'has'.ucfirst($this->property);
Expand Down
9 changes: 3 additions & 6 deletions src/Accessor/PropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ public function __construct(string $property)
}

/**
* @param object $object
* @param mixed $value
* @param mixed $value
*/
public function setValue($object, $value): void
public function setValue(object $object, $value): void
{
$class = $this->getClass($object);

Expand All @@ -43,11 +42,9 @@ function ($property, $value): void {
}

/**
* @param object $object
*
* @return mixed
*/
public function getValue($object)
public function getValue(object $object)
{
$class = $this->getClass($object);

Expand Down
2 changes: 1 addition & 1 deletion src/Decoder/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function getContentTypes(): array
* @throws DeserializerLogicException
* @throws DeserializerRuntimeException
*
* @return array<mixed>
* @return array<int|string, array|string|float|int|bool|null>
*/
public function decode(string $data, string $contentType): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Decoder/DecoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function getContentTypes(): array;
* @throws DeserializerLogicException
* @throws DeserializerRuntimeException
*
* @return array<mixed>
* @return array<int|string, array|string|float|int|bool|null>
*/
public function decode(string $data, string $contentType): array;
}
4 changes: 2 additions & 2 deletions src/Decoder/JsonTypeDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public function getContentType(): string
/**
* @throws DeserializerRuntimeException
*
* @return array<mixed>
* @return array<int|string, array|string|float|int|bool|null>
*/
public function decode(string $data): array
{
$decoded = $json = json_decode($data, true);
$decoded = json_decode($data, true);

if (JSON_ERROR_NONE !== json_last_error()) {
throw DeserializerRuntimeException::createNotParsable($this->getContentType(), json_last_error_msg());
Expand Down
33 changes: 8 additions & 25 deletions src/Decoder/JsonxTypeDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,22 @@
*/
final class JsonxTypeDecoder implements TypeDecoderInterface
{
public const DATATYPE_OBJECT = 'object';
public const DATATYPE_ARRAY = 'array';
public const DATATYPE_BOOLEAN = 'boolean';
public const DATATYPE_STRING = 'string';
public const DATATYPE_NUMBER = 'number';
public const DATATYPE_NULL = 'null';

/**
* @var string
*/
private $contentType;

public function __construct(string $contentType = 'application/x-jsonx')
{
if ('application/x-jsonx' === $contentType) {
@trigger_error(
'Use "application/jsonx+xml" instead of "application/x-jsonx", cause jsonx is a xml variant.',
E_USER_DEPRECATED
);
}

$this->contentType = $contentType;
}
private const DATATYPE_OBJECT = 'object';
private const DATATYPE_ARRAY = 'array';
private const DATATYPE_BOOLEAN = 'boolean';
private const DATATYPE_STRING = 'string';
private const DATATYPE_NUMBER = 'number';
private const DATATYPE_NULL = 'null';

public function getContentType(): string
{
return $this->contentType;
return 'application/jsonx+xml';
}

/**
* @throws DeserializerRuntimeException
*
* @return array<mixed>
* @return array<int|string, array|string|float|int|bool|null>
*/
public function decode(string $data): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Decoder/TypeDecoderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function getContentType(): string;
/**
* @throws DeserializerRuntimeException
*
* @return array<mixed>
* @return array<int|string, array|string|float|int|bool|null>
*/
public function decode(string $data): array;
}
9 changes: 7 additions & 2 deletions src/Decoder/UrlEncodedTypeDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function getContentType(): string
/**
* @throws DeserializerRuntimeException
*
* @return array<mixed>
* @return array<int|string, array|string|float|int|bool|null>
*/
public function decode(string $data): array
{
Expand All @@ -30,6 +30,11 @@ public function decode(string $data): array
return $this->fixValues($rawData);
}

/**
* @param array<int|string, array|string|float|int|bool|null> $rawData
*
* @return array<int|string, array|string|float|int|bool|null>
*/
private function fixValues(array $rawData): array
{
$data = [];
Expand All @@ -45,7 +50,7 @@ private function fixValues(array $rawData): array
}

/**
* @return mixed
* @return string|float|int|bool|null
*/
private function fixValue(string $value)
{
Expand Down

0 comments on commit 7c2078c

Please sign in to comment.