Skip to content

Commit

Permalink
Adding count and start to collection query params
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafabarmshory committed Feb 20, 2021
1 parent ae9ffdb commit 511a076
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
24 changes: 24 additions & 0 deletions src/CollectionQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace Pluf\Core;

use Pluf\Orm\Attribute\Entity;
use Pluf\Orm\Attribute\Column;

// TODO: maso, 2020: add following constraint
// use Pluf\Orm\Attribute\IsPositive;
// use Pluf\Orm\Attribute\Max;

#[Entity]
class CollectionQuery
{

#[Column('start')]
public int $start = 0;

#[Column('count')]
public int $count = 500;

#[Column('query')]
public ?string $query = null;
}

6 changes: 3 additions & 3 deletions src/Process/Entity/ReadEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public function __construct(string $class, string $name = "entities")
*/
public function __invoke(EntityQuery $entityQuery): array
{
return $entityQuery->entity($this->class)
->mode('select')
->exec();
return $entityQuery->entity($this->class, '_e')
->mapper('_e')
->select();
}

}
Expand Down
28 changes: 21 additions & 7 deletions src/Process/Http/RequestToEntityQuery.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
<?php
namespace Pluf\Core\Process\Http;

use Pluf\Core\CollectionQuery;
use Pluf\Orm\EntityManager;
use Pluf\Orm\ObjectMapper;
use Pluf\Scion\UnitTrackerInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
* Creates an entity query based on http request
*
*
* NOTE: the class must set.
*
* @author maso
*
* @author maso
*
*/
class RequestToEntityQuery
{
public function __invoke(ServerRequestInterface $request, EntityManager $entityManager, UnitTrackerInterface $unitTracker)

public function __invoke(ServerRequestInterface $request, EntityManager $entityManager, ObjectMapper $objectMapperArray, UnitTrackerInterface $unitTracker)
{
$entityQuery = $entityManager->createQuery();
//XXX: maso, 2021: read params from the request header.
$collectionQuery = $objectMapperArray->readValue($request->getQueryParams(), CollectionQuery::class);

// TODO: maso, 2021: validate the collection query

$query = $entityManager->query()
->limit($collectionQuery->count, $collectionQuery->start);

// TODO: maso, 2021: suport collection query
// TODO: amso, 2021: suport collection query filter
// TODO: maso, 2021: suport collection query sort
// TODO: maso, 2021: suport collection query order

return $unitTracker->next([
'entityQuery' => $entityQuery
'collectionQuery' => $collectionQuery,
'entityQuery' => $query
]);
}
}
Expand Down

0 comments on commit 511a076

Please sign in to comment.