Skip to content

Commit

Permalink
Merge 37ba1f5 into 3408978
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Zogg committed Sep 20, 2020
2 parents 3408978 + 37ba1f5 commit 58b6049
Show file tree
Hide file tree
Showing 108 changed files with 720 additions and 3,514 deletions.
40 changes: 28 additions & 12 deletions README.md
Expand Up @@ -14,25 +14,25 @@ A simple serialization.
## Requirements

* php: ^7.2
* doctrine/inflector: ^1.0
* doctrine/inflector: ^1.0|^2.0
* psr/http-message: ^1.0.1
* psr/link: ^1.0
* psr/log: ^1.1
* psr/log: ^1.1.3

## Suggest

* chubbyphp/chubbyphp-container: ^1.0
* pimple/pimple: ^3.2.3
* chubbyphp/chubbyphp-container: ^1.1
* pimple/pimple: ^3.3
* psr/container: ^1.0
* symfony/dependency-injection: ^2.8.50|^3.4.26|^4.2.7|^5.0 (symfony integration)
* symfony/yaml: ^2.8.50|^3.4.26|^4.2.7|^5.0 (application/x-yaml support)
* symfony/dependency-injection: ^3.4.42|^4.4.10|^5.0.10 (symfony integration)
* symfony/yaml: ^3.4.42|^4.4.10|^5.0.10 (application/x-yaml support)

## Installation

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

```sh
composer require chubbyphp/chubbyphp-serialization "^2.15"
composer require chubbyphp/chubbyphp-serialization "^3.0"
```

## Usage
Expand Down Expand Up @@ -111,11 +111,20 @@ composer require chubbyphp/chubbyphp-serialization "^2.15"
* [CallableNormalizationObjectMapping][30]
* [LazyNormalizationObjectMapping][31]

### Policy

* [AndPolicy][32]
* [CallbackPolicy][33]
* [GroupPolicy][34]
* [NotPolicy][35]
* [NullPolicy][36]
* [OrPolicy][37]

### ServiceFactory

#### chubbyphp-container

* [SerializationServiceFactory][32]
* [SerializationServiceFactory][38]

#### chubbyphp-laminas-config-factory

Expand All @@ -126,7 +135,7 @@ composer require chubbyphp/chubbyphp-serialization "^2.15"

### ServiceProvider

* [SerializationServiceProvider][33]
* [SerializationServiceProvider][39]

### Serializer

Expand Down Expand Up @@ -188,7 +197,7 @@ print_r($data);
print_r($serializer->getContentTypes());
//[
// 'application/json',
// 'application/x-jsonx',
// 'application/jsonx+xml',
// 'application/x-www-form-urlencoded',
// 'application/xml',
// 'application/x-yaml'
Expand Down Expand Up @@ -252,9 +261,16 @@ Dominik Zogg 2020
[30]: doc/Mapping/CallableNormalizationObjectMapping.md
[31]: doc/Mapping/LazyNormalizationObjectMapping.md

[32]: doc/ServiceFactory/SerializationServiceFactory.md
[32]: doc/Policy/AndPolicy.md
[33]: doc/Policy/CallbackPolicy.md
[34]: doc/Policy/GroupPolicy.md
[35]: doc/Policy/NotPolicy.md
[36]: doc/Policy/NullPolicy.md
[37]: doc/Policy/OrPolicy.md

[38]: doc/ServiceFactory/SerializationServiceFactory.md

[33]: doc/ServiceProvider/SerializationServiceProvider.md
[39]: doc/ServiceProvider/SerializationServiceProvider.md

[40]: doc/ServiceFactory/EncoderFactory.md
[41]: doc/ServiceFactory/NormalizerFactory.md
Expand Down
30 changes: 15 additions & 15 deletions composer.json
Expand Up @@ -11,29 +11,29 @@
],
"require": {
"php": "^7.2",
"doctrine/inflector": "^1.0",
"doctrine/inflector": "^1.0|^2.0",
"psr/http-message": "^1.0.1",
"psr/link": "^1.0",
"psr/log": "^1.1"
"psr/log": "^1.1.3"
},
"require-dev": {
"chubbyphp/chubbyphp-container": "^1.0",
"chubbyphp/chubbyphp-container": "^1.1",
"chubbyphp/chubbyphp-dev-helper": "dev-master",
"chubbyphp/chubbyphp-laminas-config-factory": "^1.0",
"chubbyphp/chubbyphp-mock": "^1.4.2",
"chubbyphp/chubbyphp-mock": "^1.4.5",
"doctrine/persistence": "^1.3|^2.0",
"infection/infection": "^0.15",
"infection/infection": "^0.15.3|^0.16.4",
"mavimo/phpstan-junit": "^0.3",
"php-coveralls/php-coveralls": "^2.2",
"phploc/phploc": "^5.0",
"phpstan/extension-installer": "^1.0.3",
"phpstan/phpstan": "^0.12.3",
"phpunit/phpunit": "^8.5.1",
"pimple/pimple": "^3.2.3",
"phploc/phploc": "^5.0|^6.0.2",
"phpstan/extension-installer": "^1.0.4",
"phpstan/phpstan": "^0.12.32",
"phpunit/phpunit": "^8.5.8|^9.2.5",
"pimple/pimple": "^3.3",
"psr/container": "^1.0",
"symfony/config": "^2.8.50|^3.4.26|^4.2.7|^5.0",
"symfony/dependency-injection": "^2.8.50|^3.4.26|^4.2.7|^5.0",
"symfony/yaml": "^2.8.50|^3.4.26|^4.2.7|^5.0"
"symfony/config": "^3.4.42|^4.4.10|^5.0.10",
"symfony/dependency-injection": "^3.4.42|^4.4.10|^5.0.10",
"symfony/yaml": "^3.4.42|^4.4.10|^5.0.10"
},
"autoload": {
"psr-4": { "Chubbyphp\\Serialization\\": "src/" }
Expand All @@ -46,7 +46,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.15-dev"
"dev-master": "3.0-dev"
}
},
"scripts": {
Expand All @@ -63,7 +63,7 @@
],
"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=87 --verbose --coverage=build/phpunit",
"test:insights": "mkdir -p build && bash -c 'vendor/bin/phpinsights analyse -v --no-interaction --min-quality=88 --disable-security-check | tee build/phpinsights.log; if [ ${PIPESTATUS[0]} -ne \"0\" ]; then exit 1; fi'",
"test:insights": "mkdir -p build && bash -c 'vendor/bin/phpinsights analyse -v --no-interaction --min-quality=94 --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
15 changes: 9 additions & 6 deletions doc/Encoder/Encoder.md
Expand Up @@ -3,14 +3,16 @@
```php
<?php

use Chubbyphp\Deserialization\Encoder\Encoder;
use Chubbyphp\Deserialization\Encoder\JsonTypeEncoder;
use Chubbyphp\Deserialization\Encoder\UrlEncodedTypeEncoder;
use Chubbyphp\Deserialization\Encoder\XmlTypeEncoder;
use Chubbyphp\Deserialization\Encoder\YamlTypeEncoder;
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()
Expand All @@ -19,6 +21,7 @@ $encoder = new Encoder([
print_r($encoder->getContentTypes());
//[
// 'application/json',
// 'application/jsonx+xml',
// 'application/x-www-form-urlencoded',
// 'application/xml',
// 'application/x-yaml'
Expand All @@ -29,4 +32,4 @@ echo $encoder->encode(
'application/json'
);
// '{"name": "php"}'
```
```
2 changes: 1 addition & 1 deletion doc/Encoder/JsonxTypeEncoder.md
Expand Up @@ -8,7 +8,7 @@ use Chubbyphp\Serialization\Encoder\JsonxTypeEncoder;
$encoderType = new JsonxTypeEncoder();

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

echo $encoderType->encode(['name' => 'php']);
// '<json:object><json:string name="name">php</json:string></json:object>'
Expand Down
4 changes: 2 additions & 2 deletions doc/Encoder/XmlTypeEncoder.md
@@ -1,4 +1,4 @@
# XmlTypeEncoder
# XmlTypeEncoder (alias for Jsonx)

```php
<?php
Expand All @@ -11,5 +11,5 @@ echo $encoderType->getContentType();
// 'application/xml'

echo $encoderType->encode(['name' => 'php']);
// '<name type="string">php</name>'
// '<json:object><json:string name="name">php</json:string></json:object>'
```
15 changes: 10 additions & 5 deletions doc/Mapping/AdvancedNormalizationObjectMapping.md
Expand Up @@ -11,14 +11,15 @@ namespace MyProject\Serialization;

use Chubbyphp\Serialization\Accessor\PropertyAccessor;
use Chubbyphp\Serialization\Link\LinkBuilder;
use Chubbyphp\Serialization\Mapping\NormalizationFieldMappingBuilder;
use Chubbyphp\Serialization\Mapping\NormalizationFieldMappingInterface;
use Chubbyphp\Serialization\Mapping\NormalizationLinkMapping;
use Chubbyphp\Serialization\Mapping\NormalizationLinkMappingInterface;
use Chubbyphp\Serialization\Mapping\NormalizationObjectMappingInterface;
use Chubbyphp\Serialization\Normalizer\CallbackLinkNormalizer;
use Chubbyphp\Serialization\Normalizer\Relation\EmbedManyFieldNormalizer;
use Chubbyphp\Serialization\Mapping\NormalizationFieldMappingBuilder;
use Chubbyphp\Serialization\Mapping\NormalizationFieldMappingInterface;
use Chubbyphp\Serialization\Mapping\NormalizationObjectMappingInterface;
use Chubbyphp\Serialization\Normalizer\Relation\EmbedOneFieldNormalizer;
use Chubbyphp\Serialization\Policy\GroupPolicy;
use MyProject\Model\Model;

final class ModelMapping implements NormalizationObjectMappingInterface
Expand Down Expand Up @@ -48,8 +49,12 @@ final class ModelMapping implements NormalizationObjectMappingInterface
public function getNormalizationFieldMappings(string $path, string $type = null): array
{
return [
NormalizationFieldMappingBuilder::create('id')->setGroups(['baseInformation'])->getMapping(),
NormalizationFieldMappingBuilder::create('name')->setGroups(['baseInformation'])->getMapping(),
NormalizationFieldMappingBuilder::create('id')
->setPolicy(new GroupPolicy(['baseInformation']))
->getMapping(),
NormalizationFieldMappingBuilder::create('name')
->setPolicy(new GroupPolicy(['baseInformation']))
->getMapping(),
NormalizationFieldMappingBuilder::createEmbedOne('one')->getMapping(),
NormalizationFieldMappingBuilder::createEmbedMany('manies')->getMapping(),
];
Expand Down
1 change: 0 additions & 1 deletion doc/Mapping/NormalizationFieldMappingBuilder.md
Expand Up @@ -8,7 +8,6 @@ use Chubbyphp\Serialization\Normalizer\FieldNormalizer;
use Chubbyphp\Serialization\Mapping\NormalizationFieldMappingBuilder;

$fieldMapping = NormalizationFieldMappingBuilder::create('name')
->setGroups(['group1'])
->setFieldNormalizer(
new FieldNormalizer(
new PropertyAccessor('name')
Expand Down
23 changes: 11 additions & 12 deletions doc/Mapping/NormalizationLinkMappingBuilder.md
Expand Up @@ -9,19 +9,18 @@ use Chubbyphp\Serialization\Normalizer\CallbackLinkNormalizer;
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;

$fieldMapping = NormalizationLinkMappingBuilder::create(
'name',
new CallbackLinkNormalizer(
function (
string $path,
$object,
NormalizerContextInterface $context
) {
return new Link('/api/model');
}
)
'name',
new CallbackLinkNormalizer(
function (
string $path,
$object,
NormalizerContextInterface $context
) {
return new Link('/api/model');
}
)
->setGroups(['group1'])
->getMapping();
)
->getMapping();

echo $fieldMapping->getName();
// 'name'
Expand Down
2 changes: 1 addition & 1 deletion doc/Normalizer/CallbackFieldNormalizer.md
Expand Up @@ -14,7 +14,7 @@ $context = ...;
$fieldNormalizer = new CallbackFieldNormalizer(
function (
string $path,
$object,
object $object,
NormalizerContextInterface $context,
NormalizerInterface $normalizer = null
) {
Expand Down
2 changes: 1 addition & 1 deletion doc/Normalizer/CallbackLinkNormalizer.md
Expand Up @@ -14,7 +14,7 @@ $context = ...;
$fieldNormalizer = new CallbackLinkNormalizer(
function (
string $path,
$object,
object $object,
NormalizerContextInterface $context
) {
return new Link('/api/model');
Expand Down
8 changes: 4 additions & 4 deletions doc/Normalizer/NormalizerContext.md
Expand Up @@ -8,10 +8,10 @@ use Psr\Http\Message\ServerRequestInterface;

$request = ...;

$context = new NormalizerContext(['group1'], $request);

print_r($context->getGroups());
// ['group1']
$context = new NormalizerContext($request, ['key' => 'value']);

$request = $context->getRequest();

print_r($context->getAttributes());
// ['key' => 'value']
```
6 changes: 3 additions & 3 deletions doc/Normalizer/NormalizerContextBuilder.md
Expand Up @@ -9,12 +9,12 @@ use Psr\Http\Message\ServerRequestInterface;
$request = ...;

$context = NormalizerContextBuilder::create()
->setGroups(['group1'])
->setRequest($request)
->setAttributes(['key' => 'value'])
->getContext();

print_r($context->getGroups());
// ['group1']
print_r($context->getAttributes());
// ['key' => 'value']

$request = $context->getRequest();
```
24 changes: 24 additions & 0 deletions doc/Policy/AndPolicy.md
@@ -0,0 +1,24 @@
# AndPolicy

```php
<?php

use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;
use Chubbyphp\Serialization\Policy\AndPolicy;
use MyProject\Model\Model;
use MyProject\Policy\AnotherPolicy;
use MyProject\Policy\SomePolicy;

$model = new Model();

/** @var NormalizerContextInterface $context */
$context = ...;

$policy = new AndPolicy([
new SomePolicy(),
new AnotherPolicy(),
]);

echo $policy->isCompliant('path', $model, $context);
// 1
```
21 changes: 21 additions & 0 deletions doc/Policy/CallbackPolicy.md
@@ -0,0 +1,21 @@
# CallbackPolicy

```php
<?php

use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;
use Chubbyphp\Serialization\Policy\CallbackPolicy;
use MyProject\Model\Model;

$model = new Model();

/** @var NormalizerContextInterface $context */
$context = ...;

$policy = new CallbackPolicy(function (string $path, object $object, NormalizerContextInterface $context) {
return true;
});

echo $policy->isCompliant('path', $model, $context);
// 1
```

0 comments on commit 58b6049

Please sign in to comment.