Skip to content

Commit

Permalink
Merge pull request #26 from ConductionNL/development
Browse files Browse the repository at this point in the history
Development to main
  • Loading branch information
remko48 authored Oct 18, 2024
2 parents f5efdef + b7e33b7 commit 16d3567
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/Db/ObjectEntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function countAll(?array $filters = [], ?array $searchConditions = [], ?a
* @param array $searchParams The search parameters to apply to the objects
* @return array An array of ObjectEntitys
*/
public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): array
public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = [], array $sort = []): array
{
$qb = $this->db->getQueryBuilder();

Expand All @@ -166,8 +166,9 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
$qb->setParameter($param, $value);
}
}
$qb = $this->databaseJsonService->filterJson($qb, $filters);

$qb = $this->databaseJsonService->filterJson(builder: $qb, filters: $filters);
$qb = $this->databaseJsonService->orderJson(builder: $qb, order: $sort);

return $this->findEntities(query: $qb);
}
Expand Down
38 changes: 36 additions & 2 deletions lib/Service/MySQLJsonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,52 @@

class MySQLJsonService implements IDatabaseJsonService
{
function orderJson(IQueryBuilder $builder, array $order = []): IQueryBuilder
{

foreach($order as $item=>$direction) {
$builder->createNamedParameter(value: "$.$item", placeHolder: ":path$item");
$builder->createNamedParameter(value: $direction, placeHolder: ":direction$item");

$builder->orderBy($builder->createFunction("json_unquote(json_extract(object, :path$item))"),$direction);
}

return $builder;
}

function filterJson(IQueryBuilder $builder, array $filters): IQueryBuilder
{
unset($filters['register'], $filters['schema'], $filters['updated'], $filters['created'], $filters['_queries']);

foreach($filters as $filter=>$value) {
$builder->createNamedParameter(value: $value, placeHolder: ":value$filter");

$builder->createNamedParameter(value: "$.$filter", placeHolder: ":path$filter");

if(is_array($value) === true) {
switch(array_keys($value)[0]) {
case 'after':
$builder->createNamedParameter(value: $value, type: IQueryBuilder::PARAM_STR_ARRAY, placeHolder: ":value$filter");
$builder
->andWhere("json_unquote(json_extract(object, :path$filter)) >= (:value$filter)");
break;
case 'before':
$builder->createNamedParameter(value: $value, type: IQueryBuilder::PARAM_STR_ARRAY, placeHolder: ":value$filter");
$builder
->andWhere("json_unquote(json_extract(object, :path$filter)) <= (:value$filter)");
break;
default:
$builder->createNamedParameter(value: $value, type: IQueryBuilder::PARAM_STR_ARRAY, placeHolder: ":value$filter");
$builder
->andWhere("json_unquote(json_extract(object, :path$filter)) IN (:value$filter)");
break;
}
continue;
}

$builder->createNamedParameter(value: $value, placeHolder: ":value$filter");
$builder
->andWhere("json_extract(object, :path$filter) = :value$filter");
}

return $builder;
}

Expand Down
9 changes: 5 additions & 4 deletions lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ public function delete(array|\JsonSerializable $object): bool
);
}

public function findAll(?int $limit = null, ?int $offset = null, array $filters = []): array
public function findAll(?int $limit = null, ?int $offset = null, array $filters = [], array $sort = []): array
{
$objects = $this->getObjects(
register: $this->getRegister(),
schema: $this->getSchema(),
limit: $limit,
offset: $offset,
filters: $filters
filters: $filters,
sort: $sort
);
// $data = array_map([$this, 'getDataFromObject'], $objects);

Expand Down Expand Up @@ -138,7 +139,7 @@ private function getDataFromObject(mixed $object) {
* @return array The retrieved objects.
* @throws \Exception
*/
public function getObjects(?string $objectType = null, ?int $register = null, ?int $schema = null, ?int $limit = null, ?int $offset = null, array $filters = []): array
public function getObjects(?string $objectType = null, ?int $register = null, ?int $schema = null, ?int $limit = null, ?int $offset = null, array $filters = [], array $sort = []): array
{
if($objectType === null && $register !== null && $schema !== null) {
$objectType = 'objectEntity';
Expand All @@ -150,7 +151,7 @@ public function getObjects(?string $objectType = null, ?int $register = null, ?i
$mapper = $this->getMapper($objectType);

// Use the mapper to find and return all objects of the specified type
return $mapper->findAll(limit: $limit, offset: $offset, filters: $filters);
return $mapper->findAll(limit: $limit, offset: $offset, filters: $filters, sort: $sort);
}

/**
Expand Down

0 comments on commit 16d3567

Please sign in to comment.