AbstractRepositoryTrait ======================= * Published: 2019-07-11 * Author: Nickolas Burr .. contents:: Table of Contents :local: Related ------- * :doc:`AbstractRepositoryInterface` * :doc:`AbstractRepository` Description ----------- In constract to ``AbstractRepository``, which is an ``abstract`` class that implements ``AbstractRepositoryInterface``, you could make use of PHP traits to achieve a similar result. With regards to ``AbstractRepository``, this approach provides looser coupling by eliminating inheritance and utilizing a trait as the vehicle for satisfying the service contract enforced by ``AbstractRepositoryInterface``. Usage ----- .. code-block:: php collectionFactory = $collectionFactory; $this->searchResultsFactory = $searchResultsFactory; } ... } Source ------ .. code-block:: php getFilters() as $filter) { /** @var string $param */ $param = $filter->getConditionType() ?: 'eq'; /** @var string $field */ $field = $filter->getField(); /** @var mixed $value */ $value = $filter->getValue(); $fields[] = $field; $params[] = [ $param => $value, ]; } $collection->addFieldToFilter($fields, $params); } /** * @param string $direction * @return string */ public function getDirection( string $direction = SortOrder::SORT_DESC ): string { return $direction === SortOrder::SORT_ASC ? SortOrder::SORT_ASC : SortOrder::SORT_DESC; } /** * @param SearchCriteriaInterface $criteria * @return SearchResultsInterface */ public function getList(SearchCriteriaInterface $criteria): SearchResultsInterface { /** @var AbstractCollectionInterface $collection */ $collection = $this->collectionFactory->create(); foreach ($criteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } foreach ((array) $criteria->getSortOrders() as $sortOrder) { /** @var string $field */ $field = $sortOrder->getField(); $collection->addOrder( $field, $this->getDirection($sortOrder->getDirection()) ); } $collection->setCurPage($criteria->getCurrentPage()); $collection->setPageSize($criteria->getPageSize()); $collection->load(); /** @var SearchResultsInterface $results */ $results = $this->searchResultsFactory->create(); $results->setSearchCriteria($criteria); /** @var array $items */ $items = []; foreach ($collection as $item) { $items[] = $item; } $results->setItems($items); $results->setTotalCount($collection->getSize()); return $results; } }