first commit
This commit is contained in:
169
Classes/Domain/Search/ResultSet/Sorting/Sorting.php
Normal file
169
Classes/Domain/Search/ResultSet/Sorting/Sorting.php
Normal file
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\Domain\Search\ResultSet\Sorting;
|
||||
|
||||
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\SearchResultSet;
|
||||
|
||||
/**
|
||||
* Class Sorting
|
||||
*/
|
||||
class Sorting
|
||||
{
|
||||
const DIRECTION_DESC = 'desc';
|
||||
|
||||
const DIRECTION_ASC = 'asc';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected static $validDirections = [self::DIRECTION_DESC, self::DIRECTION_ASC];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $name = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $field = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $direction = self::DIRECTION_ASC;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $label = '';
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $selected = false;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $isResetOption = false;
|
||||
|
||||
/**
|
||||
* @param SearchResultSet $resultSet
|
||||
* @param string $name
|
||||
* @param string $field
|
||||
* @param string $direction
|
||||
* @param string $label
|
||||
* @param boolean $selected
|
||||
* @param boolean $isResetOption
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __construct(SearchResultSet $resultSet, $name, $field, $direction, $label, $selected, $isResetOption)
|
||||
{
|
||||
if (!self::getIsValidDirection($direction)) {
|
||||
throw new \InvalidArgumentException("Invalid sorting direction");
|
||||
}
|
||||
$this->name = $name;
|
||||
$this->direction = $direction;
|
||||
$this->field = $field;
|
||||
$this->label = $label;
|
||||
$this->selected = $selected;
|
||||
$this->isResetOption = $isResetOption;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDirection()
|
||||
{
|
||||
return $this->direction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsAscDirection()
|
||||
{
|
||||
return $this->direction === self::DIRECTION_ASC;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getIsDescDirection()
|
||||
{
|
||||
return $this->direction === self::DIRECTION_DESC;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the opposite direction of the current assigned direction.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getOppositeDirection()
|
||||
{
|
||||
return self::getOppositeDirectionFromDirection($this->direction);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getField()
|
||||
{
|
||||
return $this->field;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLabel()
|
||||
{
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function getSelected()
|
||||
{
|
||||
return $this->selected;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $direction
|
||||
* @return bool
|
||||
*/
|
||||
public static function getIsValidDirection($direction)
|
||||
{
|
||||
return in_array($direction, self::$validDirections);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $direction
|
||||
* @return string
|
||||
*/
|
||||
public static function getOppositeDirectionFromDirection($direction)
|
||||
{
|
||||
if ($direction === self::DIRECTION_ASC) {
|
||||
return self::DIRECTION_DESC;
|
||||
} else {
|
||||
return self::DIRECTION_ASC;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function getIsResetOption()
|
||||
{
|
||||
return $this->isResetOption;
|
||||
}
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\Domain\Search\ResultSet\Sorting;
|
||||
|
||||
use WapplerSystems\Meilisearch\System\Data\AbstractCollection;
|
||||
|
||||
/**
|
||||
* Class SortingCollection
|
||||
*/
|
||||
class SortingCollection extends AbstractCollection
|
||||
{
|
||||
/**
|
||||
* @var Sorting
|
||||
*/
|
||||
protected $selected;
|
||||
|
||||
/**
|
||||
* @param Sorting $sorting
|
||||
*/
|
||||
public function addSorting(Sorting $sorting)
|
||||
{
|
||||
if ($sorting->getSelected()) {
|
||||
$this->selected = $sorting;
|
||||
}
|
||||
|
||||
$this->data[$sorting->getName()] = $sorting;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*/
|
||||
public function setData($data)
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Sorting $selected
|
||||
*/
|
||||
protected function setSelected(Sorting $selected)
|
||||
{
|
||||
$this->selected = $selected;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Sorting
|
||||
*/
|
||||
public function getSelected()
|
||||
{
|
||||
return $this->selected;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function getHasSelected()
|
||||
{
|
||||
return $this->selected !== null;
|
||||
}
|
||||
}
|
82
Classes/Domain/Search/ResultSet/Sorting/SortingHelper.php
Normal file
82
Classes/Domain/Search/ResultSet/Sorting/SortingHelper.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace WapplerSystems\Meilisearch\Domain\Search\ResultSet\Sorting;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2015-2018 Timo Hund <timo.hund@dkd.de>
|
||||
* All rights reserved
|
||||
*
|
||||
* This script is part of the TYPO3 project. The TYPO3 project is
|
||||
* free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* The GNU General Public License can be found at
|
||||
* http://www.gnu.org/copyleft/gpl.html.
|
||||
*
|
||||
* This script is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
|
||||
|
||||
/**
|
||||
* Class SortingHelper
|
||||
*/
|
||||
class SortingHelper {
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $configuration = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param array $sortingConfiguration Raw configuration from plugin.tx_meilisearch.search.sorting.options
|
||||
*/
|
||||
public function __construct(array $sortingConfiguration)
|
||||
{
|
||||
$this->configuration = $sortingConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes the tx_meilisearch[sort] URL parameter containing the option names and
|
||||
* directions to sort by and resolves it to the actual sort fields and
|
||||
* directions as configured through TypoScript. Makes sure that only
|
||||
* configured sorting options get applied to the query.
|
||||
*
|
||||
* @param string $urlParameters tx_meilisearch[sort] URL parameter.
|
||||
* @return string The actual index field configured to sort by for the given sort option name
|
||||
* @throws \InvalidArgumentException if the given sort option is not configured
|
||||
*/
|
||||
public function getSortFieldFromUrlParameter($urlParameters)
|
||||
{
|
||||
$sortFields = [];
|
||||
$sortParameters = GeneralUtility::trimExplode(',', $urlParameters);
|
||||
|
||||
$removeTsKeyDot = function($sortingKey) { return trim($sortingKey, '.'); };
|
||||
$configuredSortingName = array_map($removeTsKeyDot, array_keys($this->configuration));
|
||||
|
||||
foreach ($sortParameters as $sortParameter) {
|
||||
list($sortOption, $sortDirection) = explode(' ', $sortParameter);
|
||||
|
||||
if (!in_array($sortOption, $configuredSortingName)) {
|
||||
throw new \InvalidArgumentException('No sorting configuration found for option name ' . $sortOption, 1316187644);
|
||||
}
|
||||
|
||||
$sortField = $this->configuration[$sortOption . '.']['field'];
|
||||
$sortFields[] = $sortField . ' ' . $sortDirection;
|
||||
}
|
||||
|
||||
return implode(', ', $sortFields);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user