|
8 | 8 | namespace Magento\CatalogGraphQl\Model\Category;
|
9 | 9 |
|
10 | 10 | use Magento\Catalog\Api\Data\CategoryInterface;
|
11 |
| -use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; |
12 | 11 | use Magento\Catalog\Model\ResourceModel\Category\Collection;
|
| 12 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 13 | +use Magento\Framework\Exception\InputException; |
| 14 | +use Magento\Store\Api\Data\StoreInterface; |
| 15 | +use Magento\Store\Model\ScopeInterface; |
| 16 | +use Magento\Search\Model\Query; |
13 | 17 |
|
14 | 18 | /**
|
15 | 19 | * Category filter allows to filter collection using 'id, url_key, name' from search criteria.
|
16 | 20 | */
|
17 | 21 | class CategoryFilter
|
18 | 22 | {
|
19 | 23 | /**
|
20 |
| - * @var CollectionFactory |
| 24 | + * @var ScopeConfigInterface |
21 | 25 | */
|
22 |
| - private $collectionFactory; |
| 26 | + private $scopeConfig; |
23 | 27 |
|
24 | 28 | /**
|
25 |
| - * @param CollectionFactory $collectionFactory |
| 29 | + * @param ScopeConfigInterface $scopeConfig |
26 | 30 | */
|
27 | 31 | public function __construct(
|
28 |
| - CollectionFactory $collectionFactory |
| 32 | + ScopeConfigInterface $scopeConfig |
29 | 33 | ) {
|
30 |
| - $this->collectionFactory = $collectionFactory; |
| 34 | + $this->scopeConfig = $scopeConfig; |
31 | 35 | }
|
32 | 36 |
|
33 | 37 | /**
|
34 | 38 | * Filter for filtering the requested categories id's based on url_key, ids, name in the result.
|
35 | 39 | *
|
36 | 40 | * @param array $args
|
37 | 41 | * @param Collection $categoryCollection
|
| 42 | + * @param StoreInterface $store |
| 43 | + * @throws InputException |
38 | 44 | */
|
39 |
| - public function applyFilters( |
40 |
| - array $args, |
41 |
| - Collection $categoryCollection |
42 |
| - ): void { |
| 45 | + public function applyFilters(array $args, Collection $categoryCollection, StoreInterface $store) |
| 46 | + { |
43 | 47 | $categoryCollection->addAttributeToFilter(CategoryInterface::KEY_IS_ACTIVE, ['eq' => 1]);
|
44 | 48 | foreach ($args['filters'] as $field => $cond) {
|
45 | 49 | foreach ($cond as $condType => $value) {
|
46 | 50 | if ($field === 'ids') {
|
47 | 51 | $categoryCollection->addIdFilter($value);
|
48 | 52 | } else {
|
49 |
| - $this->addAttributeFilter($categoryCollection, $field, $condType, $value); |
| 53 | + $this->addAttributeFilter($categoryCollection, $field, $condType, $value, $store); |
50 | 54 | }
|
51 | 55 | }
|
52 | 56 | }
|
53 | 57 | }
|
54 | 58 |
|
55 | 59 | /**
|
| 60 | + * Add filter to category collection |
| 61 | + * |
56 | 62 | * @param Collection $categoryCollection
|
57 | 63 | * @param string $field
|
58 | 64 | * @param string $condType
|
59 | 65 | * @param string|array $value
|
| 66 | + * @param StoreInterface $store |
| 67 | + * @throws InputException |
60 | 68 | */
|
61 |
| - private function addAttributeFilter($categoryCollection, $field, $condType, $value) |
| 69 | + private function addAttributeFilter($categoryCollection, $field, $condType, $value, $store) |
62 | 70 | {
|
63 | 71 | if ($condType === 'match') {
|
64 |
| - $this->addMatchFilter($categoryCollection, $field, $value); |
| 72 | + $this->addMatchFilter($categoryCollection, $field, $value, $store); |
65 | 73 | return;
|
66 | 74 | }
|
67 | 75 | $categoryCollection->addAttributeToFilter($field, [$condType => $value]);
|
68 | 76 | }
|
69 | 77 |
|
70 | 78 | /**
|
| 79 | + * Add match filter to collection |
71 | 80 | *
|
72 | 81 | * @param Collection $categoryCollection
|
73 | 82 | * @param string $field
|
74 | 83 | * @param string $value
|
| 84 | + * @param StoreInterface $store |
| 85 | + * @throws InputException |
75 | 86 | */
|
76 |
| - private function addMatchFilter($categoryCollection, $field, $value) |
| 87 | + private function addMatchFilter($categoryCollection, $field, $value, $store) |
77 | 88 | {
|
78 |
| - $categoryCollection->addAttributeToFilter($field, ['like' => "%{$value}%"]); |
| 89 | + $minQueryLength = $this->scopeConfig->getValue( |
| 90 | + Query::XML_PATH_MIN_QUERY_LENGTH, |
| 91 | + ScopeInterface::SCOPE_STORE, |
| 92 | + $store |
| 93 | + ); |
| 94 | + $searchValue = str_replace('%', '', $value); |
| 95 | + $matchLength = strlen($searchValue); |
| 96 | + if ($matchLength < $minQueryLength) { |
| 97 | + throw new InputException(__('Invalid match filter')); |
| 98 | + } |
| 99 | + |
| 100 | + $categoryCollection->addAttributeToFilter($field, ['like' => "%{$searchValue}%"]); |
79 | 101 | }
|
80 | 102 | }
|
0 commit comments