-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from geosocio/group-resolver
Add Group Resolver
- Loading branch information
Showing
21 changed files
with
777 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/GroupResolver/RequestGroupResolverManagerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/GroupResolver/ResponseGroupResolverManagerInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.