Skip to content

Commit

Permalink
Merge pull request #206 from jbenezech/search_customization
Browse files Browse the repository at this point in the history
Allow customization of parts of the search without total rewrite
  • Loading branch information
PierreRambaud authored Sep 7, 2020
2 parents ba385aa + ca90cbe commit a578fd7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/Adapter/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ protected function getFieldMapping()
*
* @return string
*/
private function computeOrderByField(array $filterToTableMapping)
protected function computeOrderByField(array $filterToTableMapping)
{
$orderField = $this->getOrderField();

Expand Down Expand Up @@ -344,7 +344,7 @@ private function computeOrderByField(array $filterToTableMapping)
*
* @return string
*/
private function computeShowLast($orderField, $filterToTableMapping)
protected function computeShowLast($orderField, $filterToTableMapping)
{
// allow only if feature is enabled & it is main product list query
if ($this->getInitialPopulation() === null
Expand Down Expand Up @@ -396,7 +396,7 @@ private function computeShowLast($orderField, $filterToTableMapping)
*
* @return string Table Field name with an alias
*/
private function computeFieldName($fieldName, $filterToTableMapping, $sortByField = false)
protected function computeFieldName($fieldName, $filterToTableMapping, $sortByField = false)
{
if (array_key_exists($fieldName, $filterToTableMapping)
&& (
Expand Down Expand Up @@ -432,7 +432,7 @@ private function computeFieldName($fieldName, $filterToTableMapping, $sortByFiel
*
* @return array
*/
private function computeSelectFields(array $filterToTableMapping)
protected function computeSelectFields(array $filterToTableMapping)
{
$selectFields = [];
foreach ($this->getSelectFields() as $key => $selectField) {
Expand All @@ -449,7 +449,7 @@ private function computeSelectFields(array $filterToTableMapping)
*
* @return array
*/
private function computeWhereConditions(array $filterToTableMapping)
protected function computeWhereConditions(array $filterToTableMapping)
{
$whereConditions = [];
$operationIdx = 0;
Expand Down Expand Up @@ -568,7 +568,7 @@ private function computeWhereConditions(array $filterToTableMapping)
*
* @return ArrayCollection
*/
private function computeJoinConditions(array $filterToTableMapping)
protected function computeJoinConditions(array $filterToTableMapping)
{
$joinList = new ArrayCollection();

Expand Down
8 changes: 4 additions & 4 deletions src/Product/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ class Search
/**
* @var bool
*/
private $psStockManagement;
protected $psStockManagement;

/**
* @var bool
*/
private $psOrderOutOfStock;
protected $psOrderOutOfStock;

/**
* @var AbstractAdapter
*/
private $searchAdapter;
protected $searchAdapter;

/**
* @var Context
*/
private $context;
protected $context;

/**
* Search constructor.
Expand Down
38 changes: 38 additions & 0 deletions src/Product/SearchFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA <[email protected]>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
*/

namespace PrestaShop\Module\FacetedSearch\Product;

use Context;

class SearchFactory
{
/**
* Returns an instance of Search for this context
*
* @param Context $context
*
* @return Search
*/
public function build(Context $context)
{
return new Search($context);
}
}
11 changes: 9 additions & 2 deletions src/Product/SearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,21 @@ class SearchProvider implements FacetsRendererInterface, ProductSearchProviderIn
*/
private $facetsSerializer;

/**
* @var SearchFactory
*/
private $searchFactory;

public function __construct(
Ps_Facetedsearch $module,
Filters\Converter $converter,
URLSerializer $serializer
URLSerializer $serializer,
SearchFactory $searchFactory = null
) {
$this->module = $module;
$this->filtersConverter = $converter;
$this->facetsSerializer = $serializer;
$this->searchFactory = $searchFactory === null ? new SearchFactory() : $searchFactory;
}

/**
Expand Down Expand Up @@ -112,7 +119,7 @@ public function runQuery(
$facetedSearchFilters = $this->filtersConverter->createFacetedSearchFiltersFromQuery($query);

$context = $this->module->getContext();
$facetedSearch = new Search($context);
$facetedSearch = $this->searchFactory->build($context);
// init the search with the initial population associated with the current filters
$facetedSearch->initSearch($facetedSearchFilters);

Expand Down

0 comments on commit a578fd7

Please sign in to comment.