Skip to content

Commit

Permalink
Merge pull request #1 from geosocio/group-resolver
Browse files Browse the repository at this point in the history
Add Group Resolver
  • Loading branch information
davidbarratt authored Sep 30, 2017
2 parents 6158747 + 0e4b370 commit e462bd1
Show file tree
Hide file tree
Showing 21 changed files with 777 additions and 255 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ groups annotation.
* Response Groups `GeoSocio\HttpSerializer\Annotation\ResponseGroups`
* Request & Response Groups `Symfony\Component\Serializer\Annotation\Groups`

They may also be applied with a `GroupResolver`.

## Example
```php
/**
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
},
"require": {
"symfony/http-foundation": "^3.0",
"symfony/security": "^3.0",
"symfony/http-kernel": "^3.0",
"symfony/serializer": "^3.0",
"doctrine/annotations": "^1.0",
Expand Down
22 changes: 11 additions & 11 deletions src/ArgumentResolver/ContentClassResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace GeoSocio\HttpSerializer\ArgumentResolver;

use GeoSocio\HttpSerializer\Event\DeserializeEvent;
use GeoSocio\HttpSerializer\Loader\GroupLoaderInterface;
use GeoSocio\HttpSerializer\GroupResolver\RequestGroupResolverInterface;
use GeoSocio\HttpSerializer\Exception\ConstraintViolationException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -48,11 +48,6 @@ class ContentClassResolver implements ArgumentValueResolverInterface
*/
protected $decoder;

/**
* @var GroupLoaderInterface
*/
protected $loader;

/**
* @var EventDispatcherInterface
*/
Expand All @@ -63,30 +58,35 @@ class ContentClassResolver implements ArgumentValueResolverInterface
*/
protected $validator;

/**
* @var RequestGroupResolverInterface
*/
protected $groupResolver;

/**
* Content Class Resolver
*
* @param SerializerInterface $serializer
* @param DenormalizerInterface $denormalizer
* @param DecoderInterface $decoder
* @param GroupLoaderInterface $loader
* @param EventDispatcherInterface $eventDispatcher
* @param ValidatorInterface $validator
* @param RequestGroupResolverInterface $groupResolver
*/
public function __construct(
SerializerInterface $serializer,
DenormalizerInterface $denormalizer,
DecoderInterface $decoder,
GroupLoaderInterface $loader,
EventDispatcherInterface $eventDispatcher,
ValidatorInterface $validator
ValidatorInterface $validator,
RequestGroupResolverInterface $groupResolver
) {
$this->serializer = $serializer;
$this->denormalizer = $denormalizer;
$this->decoder = $decoder;
$this->loader = $loader;
$this->eventDispatcher = $eventDispatcher;
$this->validator = $validator;
$this->groupResolver = $groupResolver;
}

/**
Expand Down Expand Up @@ -136,7 +136,7 @@ public function resolve(Request $request, ArgumentMetadata $argument)
$argument->getType(),
$request->getRequestFormat(),
[
'groups' => $this->loader->getRequestGroups($request),
'groups' => $this->groupResolver->resolve($request, $argument->getType()),
],
$request
);
Expand Down
22 changes: 10 additions & 12 deletions src/EventListener/KernelViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace GeoSocio\HttpSerializer\EventListener;

use GeoSocio\HttpSerializer\Event\SerializeEvent;
use GeoSocio\HttpSerializer\Loader\GroupLoaderInterface;
use GeoSocio\HttpSerializer\GroupResolver\ResponseGroupResolverInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -34,36 +34,36 @@ class KernelViewListener
protected $encoder;

/**
* @var GroupLoaderInterface
* @var EventDispatcherInterface
*/
protected $loader;
protected $eventDispatcher;

/**
* @var EventDispatcherInterface
* @var ResponseGroupResolverInterface
*/
protected $eventDispatcher;
protected $groupResolver;

/**
* Creates the Event Listener.
*
* @param SerializerInterface $serializer
* @param NormalizerInterface $normalizer
* @param EncoderInterface $encoder
* @param GroupLoaderInterface $loader
* @param EventDispatcherInterface $eventDispatcher
* @param ResponseGroupResolverInterface $groupResolver
*/
public function __construct(
SerializerInterface $serializer,
NormalizerInterface $normalizer,
EncoderInterface $encoder,
GroupLoaderInterface $loader,
EventDispatcherInterface $eventDispatcher
EventDispatcherInterface $eventDispatcher,
ResponseGroupResolverInterface $groupResolver
) {
$this->serializer = $serializer;
$this->normalizer = $normalizer;
$this->encoder = $encoder;
$this->loader = $loader;
$this->eventDispatcher = $eventDispatcher;
$this->groupResolver = $groupResolver;
}

/**
Expand Down Expand Up @@ -103,13 +103,11 @@ public function onKernelView(GetResponseForControllerResultEvent $event) :? Resp
break;
}

$groups = $this->loader->getResponseGroups($request);

$serializeEvent = new SerializeEvent(
$result,
$request->getRequestFormat(),
[
'groups' => $groups,
'groups' => $this->groupResolver->resolve($request, $result),
'enable_max_depth' => true,
],
$request
Expand Down
31 changes: 31 additions & 0 deletions src/GroupResolver/RequestGroupResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace GeoSocio\HttpSerializer\GroupResolver;

use Symfony\Component\HttpFoundation\Request;

/**
* Verification Interface.
*/
interface RequestGroupResolverInterface
{
/**
* Generate the groups from the object.
*
* @param Request $request
* @param string $type
*
* @return array An array of groups.
*/
public function resolve(Request $request, string $type) : array;

/**
* Deteremine if resolver supports the currect subject.
*
* @param Request $request
* @param string $type
*
* @return bool
*/
public function supports(Request $request, string $type) : bool;
}
56 changes: 56 additions & 0 deletions src/GroupResolver/RequestGroupResolverManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace GeoSocio\HttpSerializer\GroupResolver;

use Symfony\Component\HttpFoundation\Request;

/**
* Group Resolver Manager.
*/
class RequestGroupResolverManager implements RequestGroupResolverManagerInterface
{
/**
* @var RequestGroupResolverInterface[]
*/
protected $resolvers = [];

/**
* {@inheritdoc}
*/
public function resolve(Request $request, string $type) : array
{
$resolved = array_map(function ($resolver) use ($request, $type) {
if ($resolver->supports($request, $type)) {
return $resolver->resolve($request, $type);
}

return [];
}, $this->resolvers);

return array_merge(...$resolved);
}

/**
* {@inheritdoc}
*/
public function supports(Request $request, string $type) : bool
{
foreach ($this->resolvers as $resolver) {
if ($resolver->supports($request, $type)) {
return true;
}
}

return false;
}

/**
* {@inheritdoc}
*/
public function addResolver(RequestGroupResolverInterface $resolver)
{
$this->resolvers[] = $resolver;

return $this;
}
}
18 changes: 18 additions & 0 deletions src/GroupResolver/RequestGroupResolverManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace GeoSocio\HttpSerializer\GroupResolver;

/**
* Group Resolver Manager Interface.
*/
interface RequestGroupResolverManagerInterface extends RequestGroupResolverInterface
{
/**
* Adds a group resolver to the manager.
*
* @param RequestGroupResolverInterface $resolver
*
* @return self
*/
public function addResolver(RequestGroupResolverInterface $resolver);
}
30 changes: 30 additions & 0 deletions src/GroupResolver/ResponseGroupResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace GeoSocio\HttpSerializer\GroupResolver;

use Symfony\Component\HttpFoundation\Request;

/**
* Verification Interface.
*/
interface ResponseGroupResolverInterface
{
/**
* Generate the groups from the object.
*
* @param Request $request
* @param object $object
*
* @return array An array of groups.
*/
public function resolve(Request $request, $object) : array;

/**
* Deteremine if resolver supports the currect subject.
*
* @param object $object
*
* @return bool
*/
public function supports(Request $request, $object) : bool;
}
56 changes: 56 additions & 0 deletions src/GroupResolver/ResponseGroupResolverManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace GeoSocio\HttpSerializer\GroupResolver;

use Symfony\Component\HttpFoundation\Request;

/**
* Group Resolver Manager.
*/
class ResponseGroupResolverManager implements ResponseGroupResolverInterface
{
/**
* @var ResponseGroupResolverInterface[]
*/
protected $resolvers = [];

/**
* {@inheritdoc}
*/
public function resolve(Request $request, $object) : array
{
$resolved = array_map(function ($resolver) use ($request, $object) {
if ($resolver->supports($request, $object)) {
return $resolver->resolve($request, $object);
}

return [];
}, $this->resolvers);

return array_merge(...$resolved);
}

/**
* {@inheritdoc}
*/
public function supports(Request $request, $object) : bool
{
foreach ($this->resolvers as $resolver) {
if ($resolver->supports($request, $object)) {
return true;
}
}

return false;
}

/**
* {@inheritdoc}
*/
public function addResolver(ResponseGroupResolverInterface $resolver)
{
$this->resolvers[] = $resolver;

return $this;
}
}
18 changes: 18 additions & 0 deletions src/GroupResolver/ResponseGroupResolverManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace GeoSocio\HttpSerializer\GroupResolver;

/**
* Group Resolver Manager Interface.
*/
interface ResponseGroupResolverManagerInterface extends ResponseGroupResolverInterface
{
/**
* Adds a group resolver to the manager.
*
* @param ResponseGroupResolverInterface $resolver
*
* @return self
*/
public function addResolver(ResponseGroupResolverInterface $resolver);
}
Loading

0 comments on commit e462bd1

Please sign in to comment.