akaunting/app/Traits/SearchString.php

50 lines
1.1 KiB
PHP
Raw Normal View History

2021-01-23 12:16:58 +03:00
<?php
namespace App\Traits;
trait SearchString
{
/**
* Get the value of a name in search string
* Example: search=type:customer year:2020 account_id:20
* Example: issued_at>=2021-02-01 issued_at<=2021-02-10 account_id:49
2021-01-23 12:16:58 +03:00
*
* @return string|array
2021-01-23 12:16:58 +03:00
*/
public function getSearchStringValue($name, $default = '', $input = null)
{
$value = $default;
if (is_null($input)) {
$input = request('search');
}
// $manager = $this->getSearchStringManager();
// $parsed = $manager->parse($input);
2021-01-23 12:16:58 +03:00
$columns = explode(' ', $input);
foreach ($columns as $column) {
$variable = preg_split('/:|>?<?=/', $column);
2021-01-23 12:16:58 +03:00
if (empty($variable[0]) || ($variable[0] != $name) || empty($variable[1])) {
continue;
}
if (strpos($column, ':')) {
$value = $variable[1];
2021-01-23 12:16:58 +03:00
break;
}
if (!is_array($value)) {
$value = [];
}
2021-01-23 12:16:58 +03:00
$value[] = $variable[1];
2021-01-23 12:16:58 +03:00
}
return $value;
}
}