Skip to content

Commit

Permalink
Merge pull request #32 from ConductionNL/development
Browse files Browse the repository at this point in the history
Development to master
  • Loading branch information
rjzondervan authored Oct 21, 2024
2 parents 256bd68 + d2e4024 commit 9565e0c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
18 changes: 7 additions & 11 deletions lib/Db/ObjectEntityMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function findByRegisterAndSchema(string $register, string $schema): Objec
return $this->findEntities(query: $qb);
}

public function countAll(?array $filters = [], ?array $searchConditions = [], ?array $searchParams = []): int
public function countAll(?array $filters = [], ?string $search = null): int
{
$qb = $this->db->getQueryBuilder();

Expand All @@ -119,19 +119,12 @@ public function countAll(?array $filters = [], ?array $searchConditions = [], ?a
}
}

if (!empty($searchConditions)) {
$qb->andWhere('(' . implode(' OR ', $searchConditions) . ')');
foreach ($searchParams as $param => $value) {
$qb->setParameter($param, $value);
}
}
$qb = $this->databaseJsonService->filterJson($qb, $filters);
$qb = $this->databaseJsonService->searchJson($qb, $search);

$result = $qb->executeQuery();

$count = $result->fetchAll()[0]['count'];

return $count;
return $result->fetchAll()[0]['count'];
}

/**
Expand All @@ -144,7 +137,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 $sort = []): array
public function findAll(?int $limit = null, ?int $offset = null, ?array $filters = [], ?array $searchConditions = [], ?array $searchParams = [], array $sort = [], ?string $search = null): array
{
$qb = $this->db->getQueryBuilder();

Expand All @@ -171,8 +164,11 @@ public function findAll(?int $limit = null, ?int $offset = null, ?array $filters
}

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

// var_dump($qb->getSQL());

return $this->findEntities(query: $qb);
}

Expand Down
14 changes: 13 additions & 1 deletion lib/Service/MySQLJsonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ function orderJson(IQueryBuilder $builder, array $order = []): IQueryBuilder
return $builder;
}

function searchJson(IQueryBuilder $builder, ?string $search = null): IQueryBuilder
{
if($search !== null) {
$builder->createNamedParameter(value: "%$search%", placeHolder: ':search');
$builder->andWhere("JSON_SEARCH(object, 'one', :search) IS NOT NULL");
}

return $builder;
}

function filterJson(IQueryBuilder $builder, array $filters): IQueryBuilder
{
unset($filters['register'], $filters['schema'], $filters['updated'], $filters['created'], $filters['_queries']);
Expand Down Expand Up @@ -51,8 +61,10 @@ function filterJson(IQueryBuilder $builder, array $filters): IQueryBuilder

$builder->createNamedParameter(value: $value, placeHolder: ":value$filter");
$builder
->andWhere("json_extract(object, :path$filter) = :value$filter");
->andWhere("json_extract(object, :path$filter) = :value$filter OR json_contains(object, json_quote(:value$filter), :path$filter)");
}
// var_dump($builder->getSQL());

return $builder;
}

Expand Down
13 changes: 7 additions & 6 deletions lib/Service/ObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,30 @@ public function delete(array|\JsonSerializable $object): bool
);
}

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

return $objects;
}

public function count(array $filters = []): int
public function count(array $filters = [], ?string $search = null): int
{
if($this->getSchema() !== null && $this->getRegister() !== null) {
$filters['register'] = $this->getRegister();
$filters['schema'] = $this->getSchema();
}
$count = $this->objectEntityMapper
->countAll(filters: $filters);
->countAll(filters: $filters, search: $search);

return $count;
}
Expand Down Expand Up @@ -148,7 +149,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 $sort = []): array
public function getObjects(?string $objectType = null, ?int $register = null, ?int $schema = null, ?int $limit = null, ?int $offset = null, array $filters = [], array $sort = [], ?string $search = null): array
{
if($objectType === null && $register !== null && $schema !== null) {
$objectType = 'objectEntity';
Expand All @@ -160,7 +161,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, sort: $sort);
return $mapper->findAll(limit: $limit, offset: $offset, filters: $filters, sort: $sort, search: $search);
}

/**
Expand Down

0 comments on commit 9565e0c

Please sign in to comment.