|
3 | 3 | * Diglin GmbH - Switzerland. |
4 | 4 | * |
5 | 5 | * @author Sylvain Rayé <support at diglin.com> |
6 | | - * |
7 | 6 | * @category SyliusApiClient |
8 | | - * |
9 | 7 | * @copyright 2020 - Diglin (https://www.diglin.com) |
10 | 8 | */ |
11 | 9 |
|
| 10 | +declare(strict_types=1); |
| 11 | + |
12 | 12 | namespace Diglin\Sylius\ApiClient\Filter; |
13 | 13 |
|
| 14 | +use Diglin\Sylius\ApiClient\ExpressionLanguage\ExpressionLanguageProvider; |
| 15 | +use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
| 16 | + |
14 | 17 | class Filter implements FilterInterface |
15 | 18 | { |
16 | | - /** @var string */ |
17 | 19 | private $nameOfCriterion; |
18 | | - /** @var string */ |
19 | | - private $searchPhrase; |
20 | | - /** @var string */ |
21 | | - private $searchOption; |
| 20 | + private $value; |
22 | 21 |
|
| 22 | + /** |
| 23 | + * If $nameOfCriterion is an array, $value will be ignored |
| 24 | + */ |
23 | 25 | public function __construct( |
24 | | - string $nameOfCriterion = 'search', |
25 | | - string $searchOption = SearchOptions::CONTAINS, |
26 | | - string $searchPhrase = '' |
27 | | - ) { |
| 26 | + $nameOfCriterion, |
| 27 | + $value = '' |
| 28 | + ) |
| 29 | + { |
28 | 30 | $this->nameOfCriterion = $nameOfCriterion; |
29 | | - $this->searchPhrase = $searchPhrase; |
30 | | - $this->searchOption = $searchOption; |
| 31 | + $this->value = $value; |
31 | 32 | } |
32 | 33 |
|
33 | 34 | public function getCriteria(): array |
34 | 35 | { |
| 36 | + if (is_string($this->nameOfCriterion) && strpos($this->nameOfCriterion, 'criteria') === false) { |
| 37 | + $this->nameOfCriterion = sprintf('criteria[%s]', $this->nameOfCriterion); |
| 38 | + } else if (is_array($this->nameOfCriterion)) { |
| 39 | + $interpreter = new ExpressionLanguage(null, [new ExpressionLanguageProvider()]); |
| 40 | + $this->nameOfCriterion = $interpreter->evaluate('build_filter_criteria(input)', ['input' => $this->nameOfCriterion]); |
| 41 | + list($this->nameOfCriterion, $this->value) = explode('=', $this->nameOfCriterion ); |
| 42 | + } |
| 43 | + |
35 | 44 | return [ |
36 | | - printf('criteria[%s][type]', $this->nameOfCriterion) => $this->searchOption, |
37 | | - printf('criteria[%s][value]', $this->nameOfCriterion) => $this->searchPhrase, |
| 45 | + $this->nameOfCriterion => $this->value, |
38 | 46 | ]; |
39 | 47 | } |
| 48 | + |
40 | 49 | } |
0 commit comments