Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoryList.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Model;
7 
11 use Magento\Catalog\Api\Data\CategorySearchResultsInterfaceFactory;
13 use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
17 
19 {
23  private $categoryCollectionFactory;
24 
28  private $extensionAttributesJoinProcessor;
29 
33  private $categorySearchResultsFactory;
34 
38  private $categoryRepository;
39 
43  private $collectionProcessor;
44 
52  public function __construct(
53  CollectionFactory $categoryCollectionFactory,
54  JoinProcessorInterface $extensionAttributesJoinProcessor,
55  CategorySearchResultsInterfaceFactory $categorySearchResultsFactory,
56  CategoryRepositoryInterface $categoryRepository,
57  CollectionProcessorInterface $collectionProcessor = null
58  ) {
59  $this->categoryCollectionFactory = $categoryCollectionFactory;
60  $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
61  $this->categorySearchResultsFactory = $categorySearchResultsFactory;
62  $this->categoryRepository = $categoryRepository;
63  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
64  }
65 
70  {
72  $collection = $this->categoryCollectionFactory->create();
73  $this->extensionAttributesJoinProcessor->process($collection);
74 
75  $this->collectionProcessor->process($searchCriteria, $collection);
76 
77  $items = [];
78  foreach ($collection->getAllIds() as $id) {
79  $items[] = $this->categoryRepository->get($id);
80  }
81 
83  $searchResult = $this->categorySearchResultsFactory->create();
84  $searchResult->setSearchCriteria($searchCriteria);
85  $searchResult->setItems($items);
86  $searchResult->setTotalCount($collection->getSize());
87  return $searchResult;
88  }
89 
96  private function getCollectionProcessor()
97  {
98  if (!$this->collectionProcessor) {
99  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
100  'Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor'
101  );
102  }
103  return $this->collectionProcessor;
104  }
105 }
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
$id
Definition: fieldset.phtml:14
$searchCriteria
__construct(CollectionFactory $categoryCollectionFactory, JoinProcessorInterface $extensionAttributesJoinProcessor, CategorySearchResultsInterfaceFactory $categorySearchResultsFactory, CategoryRepositoryInterface $categoryRepository, CollectionProcessorInterface $collectionProcessor=null)
$items