|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace Lof\ElasticsuiteBlog\Block\Post; |
| 5 | + |
| 6 | +use Magento\Framework\View\Element\Template\Context as TemplateContext; |
| 7 | +use Magento\Search\Model\QueryFactory; |
| 8 | +use Lof\ElasticsuiteBlog\Model\ResourceModel\Post\Fulltext\CollectionFactory as PostCollectionFactory; |
| 9 | + |
| 10 | +class Result extends \Magento\Framework\View\Element\Template |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var QueryFactory |
| 14 | + */ |
| 15 | + private $queryFactory; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var \Lof\ElasticsuiteBlog\Model\ResourceModel\Post\Fulltext\Collection |
| 19 | + */ |
| 20 | + private $postCollection; |
| 21 | + |
| 22 | + /** |
| 23 | + * Suggest constructor. |
| 24 | + * |
| 25 | + * @param TemplateContext $context Template contexte. |
| 26 | + * @param QueryFactory $queryFactory Query factory. |
| 27 | + * @param PageCollectionFactory $pageCollectionFactory Page collection factory. |
| 28 | + * @param array $data Data. |
| 29 | + */ |
| 30 | + public function __construct( |
| 31 | + TemplateContext $context, |
| 32 | + QueryFactory $queryFactory, |
| 33 | + PostCollectionFactory $postCollectionFactory, |
| 34 | + array $data = [] |
| 35 | + ) { |
| 36 | + parent::__construct($context, $data); |
| 37 | + |
| 38 | + $this->queryFactory = $queryFactory; |
| 39 | + $this->postCollection = $this->initPostCollection($postCollectionFactory); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Returns blog bost collection. |
| 44 | + * |
| 45 | + * @return \Lof\ElasticsuiteBlog\Model\ResourceModel\Post\Fulltext\Collection |
| 46 | + */ |
| 47 | + public function getPostCollection() |
| 48 | + { |
| 49 | + return $this->postCollection; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns collection size. |
| 54 | + * |
| 55 | + * @return int|null |
| 56 | + */ |
| 57 | + public function getResultCount() |
| 58 | + { |
| 59 | + return $this->getPostCollection()->getSize(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns query. |
| 64 | + * |
| 65 | + * @return \Magento\Search\Model\Query |
| 66 | + */ |
| 67 | + public function getQuery() |
| 68 | + { |
| 69 | + return $this->queryFactory->get(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns query text. |
| 74 | + * |
| 75 | + * @return string |
| 76 | + */ |
| 77 | + public function getQueryText() |
| 78 | + { |
| 79 | + return $this->getQuery()->getQueryText(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Init blog post collection. |
| 84 | + * |
| 85 | + * @param postCollectionFactory $collectionFactory Blog post collection. |
| 86 | + * |
| 87 | + * @return mixed |
| 88 | + */ |
| 89 | + private function initPostCollection($collectionFactory) |
| 90 | + { |
| 91 | + $postCollection = $collectionFactory->create(); |
| 92 | + |
| 93 | + $queryText = $this->getQueryText(); |
| 94 | + $postCollection->addSearchFilter($queryText); |
| 95 | + |
| 96 | + return $postCollection; |
| 97 | + } |
| 98 | +} |
0 commit comments