Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Support latest hydrator
Browse files Browse the repository at this point in the history
  • Loading branch information
kleijnweb committed Dec 30, 2017
1 parent 979590c commit f7b9afb
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"doctrine/collections": "^1.3",
"doctrine/cache": "^1.6",
"kleijnweb/php-api-descriptions": "^1.0.0-alpha4",
"kleijnweb/php-api-hydrator": "^1.0.0-alpha2",
"kleijnweb/php-api-hydrator": "dev-master as 1.0.0-alpha2",
"kleijnweb/php-api-middleware": "^1.0.0-alpha1",
"kleijnweb/php-api-routing-bundle": "^1.0.0-alpha1"
},
Expand Down
18 changes: 18 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,24 @@ swagger:
namespaces: [My\Bundle\Resource\Namespace]
```

To add custom processors:

```yml
swagger:
hydrator:
processors: [some.service.key]
```

Custom dateformat(s):

```yml
swagger:
date_formats: ['RFC3339_MSEC', 'Y-m-d\ H:i:s']
```
Values that correspond to `KleijnWeb\PhpApi\Hydrator\DateTimeSerializer::FORMAT_` constants are resolved to that value. Values are passed to `DateTimeSerializer` constructor.

Custom processors can be a very powerful tool. See [KleijnWeb\PhpApi\Hydrator](https://github.com/kleijnweb/php-api-hydrator) docs foor more details on date formats and custom processors.

[Back to topics](#topics)

---------------------------------------
Expand Down
20 changes: 20 additions & 0 deletions src/DependencyInjection/KleijnWebSwaggerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace KleijnWeb\SwaggerBundle\DependencyInjection;

use KleijnWeb\PhpApi\Hydrator\DateTimeSerializer;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
Expand Down Expand Up @@ -46,6 +47,25 @@ public function load(array $configs, ContainerBuilder $container)
->getDefinition('swagger.hydrator.class_name_resolver')
->replaceArgument(0, $config['hydrator']['namespaces']);

$dateTimeSerializerDefinition = $container->getDefinition('swagger.hydrator.class_name_resolver');
if (isset($config['hydrator']['date_formats'])) {
foreach ($config['hydrator']['date_formats'] as $format) {
$predefinedFormat = DateTimeSerializer::class . "::FORMAT_{$format}";
if (defined($predefinedFormat)) {
$format = constant($predefinedFormat);
}
$dateTimeSerializerDefinition->addArgument(new Reference($format));
}
}

$builderDefinition = $container->getDefinition('swagger.hydrator.processor.builder');

if (isset($config['hydrator']['processors'])) {
foreach ($config['hydrator']['processors'] as $processor) {
$builderDefinition->addMethodCall('add', new Reference($processor));
}
}

$hydrator = new Reference('swagger.hydrator');
$definition = $container->getDefinition('swagger.request.processor');
$definition->addArgument($hydrator);
Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/Request/RequestProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Validator\SchemaValidator;
use KleijnWeb\PhpApi\Descriptions\Request\RequestParameterAssembler;
use KleijnWeb\PhpApi\Hydrator\DateTimeSerializer;
use KleijnWeb\PhpApi\Hydrator\ObjectHydrator;
use KleijnWeb\PhpApi\RoutingBundle\Routing\RequestMeta;
use KleijnWeb\SwaggerBundle\Exception\MalformedContentException;
use KleijnWeb\SwaggerBundle\Exception\ValidationException;
use KleijnWeb\SwaggerBundle\Hydrator\ObjectHydrator;
use Symfony\Component\HttpFoundation\Request;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/Response/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
namespace KleijnWeb\SwaggerBundle\EventListener\Response;

use KleijnWeb\PhpApi\Descriptions\Description\Schema\Validator\SchemaValidator;
use KleijnWeb\PhpApi\Hydrator\ObjectHydrator;
use KleijnWeb\PhpApi\Middleware\Util\OkStatusResolver;
use KleijnWeb\PhpApi\RoutingBundle\Routing\RequestMeta;
use KleijnWeb\SwaggerBundle\Exception\ValidationException;
use KleijnWeb\SwaggerBundle\Hydrator\ObjectHydrator;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down
54 changes: 54 additions & 0 deletions src/Hydrator/ObjectHydrator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php declare(strict_types=1);
/*
* This file is part of the KleijnWeb\SwaggerBundle package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KleijnWeb\SwaggerBundle\Hydrator;

use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema;
use KleijnWeb\PhpApi\Hydrator\ProcessorBuilder;

/**
* Wrapper around ProcessorBuilder for compatibility
*
* @author John Kleijn <john@kleijnweb.nl>
*/
class ObjectHydrator
{
/**
* @var ProcessorBuilder
*/
private $builder;

/**
* ObjectHydrator constructor.
* @param ProcessorBuilder $builder
*/
public function __construct(ProcessorBuilder $builder)
{
$this->builder = $builder;
}

/**
* @param mixed $value
* @param Schema $schema
* @return mixed
*/
public function hydrate($value, Schema $schema)
{
return $this->builder->build($schema)->hydrate($value);
}

/**
* @param mixed $value
* @param Schema $schema
* @return mixed
*/
public function dehydrate($value, Schema $schema)
{
return $this->builder->build($schema)->dehydrate($value);
}
}
7 changes: 6 additions & 1 deletion src/Resources/config/hydrator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ services:
public: false

swagger.hydrator:
class: KleijnWeb\PhpApi\Hydrator\ObjectHydrator
class: KleijnWeb\SwaggerBundle\Hydrator\ObjectHydrator
arguments: ['@swagger.hydrator.processor.builder']
public: false

swagger.hydrator.processor.builder:
class: KleijnWeb\PhpApi\Hydrator\ProcessorBuilder
arguments: ['@swagger.hydrator.class_name_resolver', '@swagger.hydrator.date_time_serializer']
public: false
2 changes: 1 addition & 1 deletion tests/unit/EventListener/Request/RequestProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Validator\SchemaValidator;
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Validator\ValidationResult;
use KleijnWeb\PhpApi\Descriptions\Request\RequestParameterAssembler;
use KleijnWeb\PhpApi\Hydrator\ObjectHydrator;
use KleijnWeb\SwaggerBundle\Hydrator\ObjectHydrator;
use KleijnWeb\PhpApi\RoutingBundle\Routing\RequestMeta;
use KleijnWeb\SwaggerBundle\EventListener\Request\RequestProcessor;
use KleijnWeb\SwaggerBundle\Exception\MalformedContentException;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/EventListener/Response/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema;
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Validator\SchemaValidator;
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Validator\ValidationResult;
use KleijnWeb\PhpApi\Hydrator\ObjectHydrator;
use KleijnWeb\SwaggerBundle\Hydrator\ObjectHydrator;
use KleijnWeb\PhpApi\RoutingBundle\Routing\RequestMeta;
use KleijnWeb\SwaggerBundle\EventListener\Response\ResponseFactory;
use KleijnWeb\SwaggerBundle\Exception\ValidationException;
Expand Down

0 comments on commit f7b9afb

Please sign in to comment.