diff --git a/src/Attributes/Dto/FindBy.php b/src/Attributes/Dto/FindBy.php index 77e37d4..1c50c07 100644 --- a/src/Attributes/Dto/FindBy.php +++ b/src/Attributes/Dto/FindBy.php @@ -26,6 +26,7 @@ class FindBy implements FindInterface, DtoAttributeInterface * @param string|null $provider * @param int|null $limit * @param int|null $offset + * @param array $static * * @noinspection DuplicatedCode */ @@ -38,7 +39,8 @@ public function __construct( array $descriptions = [], string|null $provider = null, int|null $limit = null, - int|null $offset = null + int|null $offset = null, + array $static = [] ) { $this->fields = $fields; $this->orderBy = $orderBy; @@ -49,6 +51,7 @@ public function __construct( $this->provider = $provider; $this->limit = $limit; $this->offset = $offset; + $this->static = $static; } public function isCollection(): bool diff --git a/src/Attributes/Dto/FindComplex.php b/src/Attributes/Dto/FindComplex.php index e52d7a2..82f946d 100644 --- a/src/Attributes/Dto/FindComplex.php +++ b/src/Attributes/Dto/FindComplex.php @@ -56,6 +56,7 @@ class FindComplex implements FindComplexInterface, DtoAttributeInterface * @param string|null $fn * @param string|null $service * @param bool $collection + * @param array $static * * @noinspection DuplicatedCode */ @@ -71,7 +72,8 @@ public function __construct( int|null $offset = null, string|null $fn = null, string|null $service = null, - bool $collection = false + bool $collection = false, + array $static = [] ) { $this->fields = $fields; $this->orderBy = $orderBy; @@ -85,6 +87,7 @@ public function __construct( $this->fn = $fn; $this->service = $service; $this->collection = $collection; + $this->static = $static; } public function getFn(): string diff --git a/src/Attributes/Dto/FindOneBy.php b/src/Attributes/Dto/FindOneBy.php index 1c69a7a..cd6ab10 100644 --- a/src/Attributes/Dto/FindOneBy.php +++ b/src/Attributes/Dto/FindOneBy.php @@ -22,6 +22,7 @@ class FindOneBy implements FindInterface, DtoAttributeInterface * @param string|null $errorPath * @param array $descriptions * @param string|null $provider + * @param array $static * * @noinspection DuplicatedCode */ @@ -32,7 +33,8 @@ public function __construct( array $types = [], string|null $errorPath = null, array $descriptions = [], - string|null $provider = null + string|null $provider = null, + array $static = [] ) { $this->fields = $fields; $this->orderBy = $orderBy; @@ -41,6 +43,7 @@ public function __construct( $this->errorPath = $errorPath; $this->descriptions = $descriptions; $this->provider = $provider; + $this->static = $static; } public function isCollection(): bool diff --git a/src/Interfaces/Attribute/FieldInterface.php b/src/Interfaces/Attribute/FieldInterface.php index 79ac528..1dbcde3 100644 --- a/src/Interfaces/Attribute/FieldInterface.php +++ b/src/Interfaces/Attribute/FieldInterface.php @@ -25,6 +25,15 @@ public function getFields(): array; */ public function getFirstNonDynamicField(): ?string; + /** + * Returns the static fields, these will not be changing between requests and are a shortcut + * to specifying data instead of using the dynamic fields, if it's an uncommon scenario + * or data is always known + * + * @return array + */ + public function getStatic(): array; + /** * Should be a key -> DESC/ASC array or null if no sorting was specified * diff --git a/src/Service/Resolver/DtoResolverService.php b/src/Service/Resolver/DtoResolverService.php index 71f7514..134436a 100644 --- a/src/Service/Resolver/DtoResolverService.php +++ b/src/Service/Resolver/DtoResolverService.php @@ -375,7 +375,7 @@ private function processFind( foreach ($fields as $key => $v) { $fields[$key] = $mapped[$inputPaths[$key]]; } - $criteria = array_merge($fields, $dynamic); + $criteria = array_merge($find->getStatic(), $fields, $dynamic); /** @var class-string $class */ $class = $property->getFqcn(); diff --git a/src/Traits/Annotation/FieldTrait.php b/src/Traits/Annotation/FieldTrait.php index f30d786..40507dd 100644 --- a/src/Traits/Annotation/FieldTrait.php +++ b/src/Traits/Annotation/FieldTrait.php @@ -18,6 +18,13 @@ trait FieldTrait */ public array $fields = []; + /** + * This data will not change between requests and will be used to load the entity + * + * @var array + */ + public array $static = []; + /** * Order of the results * @@ -75,6 +82,11 @@ public function getFirstNonDynamicField(): ?string return null; } + public function getStatic(): array + { + return $this->static; + } + /** * @return array|null */ diff --git a/tests/Fixtures/Model/Dto/StaticDto.php b/tests/Fixtures/Model/Dto/StaticDto.php new file mode 100644 index 0000000..c2edea9 --- /dev/null +++ b/tests/Fixtures/Model/Dto/StaticDto.php @@ -0,0 +1,18 @@ + 'something_id', 'second' => 'something_second'], + types: ['id' => new Type('int')], + static: ['static' => 1551] + )] + public ?DummyModel $model = null; +} diff --git a/tests/Unit/Service/Resolver/DtoResolverService/FindResolveAndErrorsTest.php b/tests/Unit/Service/Resolver/DtoResolverService/FindResolveAndErrorsTest.php index 12e15bf..e1af6c9 100644 --- a/tests/Unit/Service/Resolver/DtoResolverService/FindResolveAndErrorsTest.php +++ b/tests/Unit/Service/Resolver/DtoResolverService/FindResolveAndErrorsTest.php @@ -7,6 +7,7 @@ use DualMedia\DtoRequestBundle\Tests\Fixtures\Model\Dto\FindWithSecondErrorDto; use DualMedia\DtoRequestBundle\Tests\Fixtures\Model\Dto\FindWithSomeSecondErrorDto; use DualMedia\DtoRequestBundle\Tests\Fixtures\Model\Dto\MultiFindDto; +use DualMedia\DtoRequestBundle\Tests\Fixtures\Model\Dto\StaticDto; use DualMedia\DtoRequestBundle\Tests\Fixtures\Model\DummyModel; use DualMedia\DtoRequestBundle\Tests\PHPUnit\KernelTestCase; use DualMedia\DtoRequestBundle\Tests\Service\Entity\DummyModelProvider; @@ -144,4 +145,44 @@ public function testForceError(): void $this->assertFalse($resolved->isValid()); } + + public function testStaticData(): void + { + $find = $this->deferCallable(function ( + array $criteria, + ?array $orderBy = null + ) { + $this->assertNull($orderBy); + $this->assertArrayHasKey('id', $criteria); + $this->assertEquals(15, $criteria['id']); + $this->assertArrayHasKey('second', $criteria); + $this->assertEquals('yeet', $criteria['second']); + $this->assertArrayHasKey('static', $criteria); + $this->assertEquals(1551, $criteria['static']); + }); + + $this->provider->expects($this->once()) + ->method('findOneBy') + ->willReturnCallback(function (...$args) use ($find) { + $find->set($args); + + return new DummyModel(); + }); + + $request = new Request([], [ + 'something_id' => 15, + 'something_second' => 'yeet', + ]); + + /** @var StaticDto $resolved */ + $resolved = $this->service->resolve( + $request, + StaticDto::class + ); + + $this->assertTrue($resolved->isValid()); + $this->assertTrue($resolved->visited('model')); + $this->assertTrue($resolved->visitedVirtualProperty('model', 'id')); + $this->assertTrue($resolved->visitedVirtualProperty('model', 'second')); + } }