Skip to content

Commit

Permalink
Allow static data in find operations (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkly authored Feb 5, 2023
1 parent d7b6a25 commit 6374bac
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/Attributes/Dto/FindBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class FindBy implements FindInterface, DtoAttributeInterface
* @param string|null $provider
* @param int|null $limit
* @param int|null $offset
* @param array<string, mixed> $static
*
* @noinspection DuplicatedCode
*/
Expand All @@ -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;
Expand All @@ -49,6 +51,7 @@ public function __construct(
$this->provider = $provider;
$this->limit = $limit;
$this->offset = $offset;
$this->static = $static;
}

public function isCollection(): bool
Expand Down
5 changes: 4 additions & 1 deletion src/Attributes/Dto/FindComplex.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class FindComplex implements FindComplexInterface, DtoAttributeInterface
* @param string|null $fn
* @param string|null $service
* @param bool $collection
* @param array<string, mixed> $static
*
* @noinspection DuplicatedCode
*/
Expand All @@ -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;
Expand All @@ -85,6 +87,7 @@ public function __construct(
$this->fn = $fn;
$this->service = $service;
$this->collection = $collection;
$this->static = $static;
}

public function getFn(): string
Expand Down
5 changes: 4 additions & 1 deletion src/Attributes/Dto/FindOneBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class FindOneBy implements FindInterface, DtoAttributeInterface
* @param string|null $errorPath
* @param array<string, string> $descriptions
* @param string|null $provider
* @param array<string, mixed> $static
*
* @noinspection DuplicatedCode
*/
Expand All @@ -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;
Expand All @@ -41,6 +43,7 @@ public function __construct(
$this->errorPath = $errorPath;
$this->descriptions = $descriptions;
$this->provider = $provider;
$this->static = $static;
}

public function isCollection(): bool
Expand Down
9 changes: 9 additions & 0 deletions src/Interfaces/Attribute/FieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
*/
public function getStatic(): array;

/**
* Should be a key -> DESC/ASC array or null if no sorting was specified
*
Expand Down
2 changes: 1 addition & 1 deletion src/Service/Resolver/DtoResolverService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions src/Traits/Annotation/FieldTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed>
*/
public array $static = [];

/**
* Order of the results
*
Expand Down Expand Up @@ -75,6 +82,11 @@ public function getFirstNonDynamicField(): ?string
return null;
}

public function getStatic(): array
{
return $this->static;
}

/**
* @return array<string, string>|null
*/
Expand Down
18 changes: 18 additions & 0 deletions tests/Fixtures/Model/Dto/StaticDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace DualMedia\DtoRequestBundle\Tests\Fixtures\Model\Dto;

use DualMedia\DtoRequestBundle\Attributes\Dto\FindOneBy;
use DualMedia\DtoRequestBundle\Attributes\Dto\Type;
use DualMedia\DtoRequestBundle\Model\AbstractDto;
use DualMedia\DtoRequestBundle\Tests\Fixtures\Model\DummyModel;

class StaticDto extends AbstractDto
{
#[FindOneBy(
fields: ['id' => 'something_id', 'second' => 'something_second'],
types: ['id' => new Type('int')],
static: ['static' => 1551]
)]
public ?DummyModel $model = null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'));
}
}

0 comments on commit 6374bac

Please sign in to comment.