-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
235 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/* | ||
* This file is part of the StfalconApiBundle. | ||
* | ||
* (c) Stfalcon LLC <stfalcon.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StfalconStudio\ApiBundle\Request\Filter; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
|
||
/** | ||
* FilterExtractorInterface. | ||
*/ | ||
interface FilterExtractorInterface | ||
{ | ||
/** | ||
* @param Request $request | ||
* | ||
* @return FilterInterface | ||
*/ | ||
public function extractFilterFromRequest(Request $request): FilterInterface; | ||
|
||
/** | ||
* @param Request $request | ||
* @param ArgumentMetadata $argument | ||
* | ||
* @return bool | ||
*/ | ||
public function supports(Request $request, ArgumentMetadata $argument): 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,20 @@ | ||
<?php | ||
/* | ||
* This file is part of the StfalconApiBundle. | ||
* | ||
* (c) Stfalcon LLC <stfalcon.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StfalconStudio\ApiBundle\Request\Filter; | ||
|
||
/** | ||
* FilterInterface. | ||
*/ | ||
interface FilterInterface | ||
{ | ||
} |
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,64 @@ | ||
<?php | ||
/* | ||
* This file is part of the StfalconApiBundle. | ||
* | ||
* (c) Stfalcon LLC <stfalcon.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StfalconStudio\ApiBundle\Request\ValueResolver; | ||
|
||
use StfalconStudio\ApiBundle\Request\Filter\FilterExtractorInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
|
||
/** | ||
* FilterValueResolver. | ||
*/ | ||
class FilterValueResolver implements ValueResolverInterface | ||
{ | ||
/** | ||
* @param iterable<FilterExtractorInterface> $filterExtractors | ||
*/ | ||
public function __construct(private readonly iterable $filterExtractors) | ||
{ | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* @param ArgumentMetadata $argument | ||
* | ||
* @return iterable | ||
*/ | ||
public function resolve(Request $request, ArgumentMetadata $argument): iterable | ||
{ | ||
$extractor = $this->getSupportedExtractor($request, $argument); | ||
if (!$extractor instanceof FilterExtractorInterface) { | ||
return []; | ||
} | ||
|
||
yield $extractor->extractFilterFromRequest($request); | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* @param ArgumentMetadata $argument | ||
* | ||
* @return ?FilterExtractorInterface | ||
*/ | ||
private function getSupportedExtractor(Request $request, ArgumentMetadata $argument): ?FilterExtractorInterface | ||
{ | ||
foreach ($this->filterExtractors as $extractor) { | ||
if ($extractor->supports($request, $argument)) { | ||
return $extractor; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StfalconStudio\ApiBundle\Tests\Request\Filter; | ||
|
||
use StfalconStudio\ApiBundle\Request\Filter\FilterExtractorInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
|
||
class DummyFilterExtractor implements FilterExtractorInterface | ||
{ | ||
public function extractFilterFromRequest(Request $request): DummyFilterModel | ||
{ | ||
return new DummyFilterModel('foo'); | ||
} | ||
|
||
public function supports(Request $request, ArgumentMetadata $argument): bool | ||
{ | ||
return DummyFilterModel::class === $argument->getType(); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StfalconStudio\ApiBundle\Tests\Request\Filter; | ||
|
||
use StfalconStudio\ApiBundle\Request\Filter\FilterInterface; | ||
|
||
class DummyFilterModel implements FilterInterface | ||
{ | ||
public function __construct(private readonly string $foo) | ||
{ | ||
} | ||
|
||
public function getFoo() | ||
{ | ||
return $this->foo; | ||
} | ||
} |
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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace StfalconStudio\ApiBundle\Tests\Request\ValueResolver; | ||
|
||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use StfalconStudio\ApiBundle\Request\ValueResolver\FilterValueResolver; | ||
use StfalconStudio\ApiBundle\Tests\Request\Filter\DummyFilterExtractor; | ||
use StfalconStudio\ApiBundle\Tests\Request\Filter\DummyFilterModel; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
|
||
class FilterValueResolverTest extends TestCase | ||
{ | ||
private FilterValueResolver $filterValueResolver; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->argument = self::createMock(ArgumentMetadata::class); | ||
|
||
$filterValueResolver = new FilterValueResolver([new DummyFilterExtractor()]); | ||
|
||
$this->filterValueResolver = $filterValueResolver; | ||
} | ||
|
||
public function testFilterExtractorFound(): void | ||
{ | ||
/** @var Request|MockObject $request */ | ||
$request = self::createMock(Request::class); | ||
/** @var ArgumentMetadata|MockObject $request */ | ||
$argument = self::createMock(ArgumentMetadata::class); | ||
|
||
$argument | ||
->method('getType') | ||
->willReturn(DummyFilterModel::class); | ||
|
||
foreach ($this->filterValueResolver->resolve($request, $argument) as $item) { | ||
self::assertInstanceOf(DummyFilterModel::class, $item); | ||
self::assertSame('foo', $item->getFoo()); | ||
} | ||
} | ||
|
||
public function testFilterExtractorNotFound(): void | ||
{ | ||
/** @var Request|MockObject $request */ | ||
$request = self::createMock(Request::class); | ||
/** @var ArgumentMetadata|MockObject $request */ | ||
$argument = self::createMock(ArgumentMetadata::class); | ||
|
||
$argument | ||
->method('getType') | ||
->willReturn('Foo\\Bar'); | ||
|
||
$numberOfFoundModels = 0; | ||
foreach ($this->filterValueResolver->resolve($request, $argument) as $item) { | ||
++$numberOfFoundModels; | ||
} | ||
|
||
self::assertEquals(0, $numberOfFoundModels); | ||
} | ||
} |