Refactor filter_string_to_array to support both strings and arrays; simplify and reuse filter parsing logic across methods.
This commit is contained in:
@@ -377,31 +377,7 @@ trait db_object_t
|
||||
$order = $response->getRequestParameter('order') ?? null; // Get the order ( Eg. name:ASC )
|
||||
// Make the filters an array
|
||||
if ($filters) {
|
||||
$filters = explode(',', $filters);
|
||||
$temp = [];
|
||||
foreach ( $filters as $filter ) {
|
||||
$filter = explode(':', $filter);
|
||||
// Check if the filter already exists, if it does, make it an array
|
||||
if (isset($temp[$filter[0]])) {
|
||||
if (!is_array($temp[$filter[0]])) {
|
||||
$temp[$filter[0]] = [$temp[$filter[0]]];
|
||||
}
|
||||
// Check if the filter already exists within the array with the same key, if it does, skip it
|
||||
if (in_array($filter[1], $temp[$filter[0]])) {
|
||||
continue;
|
||||
}
|
||||
$temp[$filter[0]][] = $filter[1];
|
||||
continue;
|
||||
}
|
||||
$temp[$filter[0]] = $filter[1];
|
||||
}
|
||||
// Check if the value is "null", if it is, set it to null
|
||||
foreach ( $temp as $key => $value ) {
|
||||
if (!is_array($value) && strtolower($value) === 'null') {
|
||||
$temp[$key] = null;
|
||||
}
|
||||
}
|
||||
$filters = $temp;
|
||||
$filters = $this->filter_string_to_array($filters);
|
||||
}
|
||||
// Make the order an array
|
||||
if ($order) {
|
||||
@@ -716,6 +692,9 @@ trait db_object_t
|
||||
$filters = $response->getRequestParameter('filters') ?? null; // Get the filters ( Eg. department_id:1,role_id:2 OR department_id:1 )
|
||||
// If the filters are set, add the forced filters to the filters
|
||||
if ($filters) {
|
||||
if (is_array($filters)) {
|
||||
$filters = $this->array_to_filters($filters);
|
||||
}
|
||||
$filters .= ',' . $filterString;
|
||||
} else {
|
||||
$filters = $filterString;
|
||||
@@ -770,21 +749,42 @@ trait db_object_t
|
||||
|
||||
/**
|
||||
* Filter string to array
|
||||
* @param string $filterString The filter string (Eg. 'department_id:1,role_id:2')
|
||||
* @param string|array $filterString The filter string (Eg. 'department_id:1,role_id:2') or an array of filters
|
||||
* @return array The array of filters (Eg. ['department_id' => 1, 'role_id' => 2])
|
||||
*/
|
||||
public function filter_string_to_array(string $filterString): array
|
||||
public function filter_string_to_array(string|array $filterString): array
|
||||
{
|
||||
$filters = explode(',', $filterString);
|
||||
$temp = [];
|
||||
foreach ( $filters as $filter ) {
|
||||
$filter = explode(':', $filter);
|
||||
// Check if the value is "null", if it is, set it to null
|
||||
if (strtolower($filter[1]) === 'null') {
|
||||
$filter[1] = null;
|
||||
continue;
|
||||
if (is_array($filterString)) {
|
||||
$temp = $filterString;
|
||||
} else {
|
||||
$filters = explode(',', $filterString);
|
||||
$temp = [];
|
||||
foreach ( $filters as $filter ) {
|
||||
$filter = explode(':', $filter);
|
||||
if (count($filter) < 2) {
|
||||
continue;
|
||||
}
|
||||
// Check if the filter already exists, if it does, make it an array
|
||||
if (isset($temp[$filter[0]])) {
|
||||
if (!is_array($temp[$filter[0]])) {
|
||||
$temp[$filter[0]] = [$temp[$filter[0]]];
|
||||
}
|
||||
// Check if the filter already exists within the array with the same key, if it does, skip it
|
||||
if (in_array($filter[1], $temp[$filter[0]])) {
|
||||
continue;
|
||||
}
|
||||
$temp[$filter[0]][] = $filter[1];
|
||||
continue;
|
||||
}
|
||||
$temp[$filter[0]] = $filter[1];
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the value is "null", if it is, set it to null
|
||||
foreach ( $temp as $key => $value ) {
|
||||
if (is_string($value) && strtolower($value) === 'null') {
|
||||
$temp[$key] = null;
|
||||
}
|
||||
$temp[$filter[0]] = $filter[1];
|
||||
}
|
||||
return $temp;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user