Passed
Push — master ( f4b5df...d5260a )
by Aimeos
06:06
created

PgSQL::getFunctionRelevance()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 1
nop 0
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2020
6
 * @package MShop
7
 * @subpackage Index
8
 */
9
10
11
namespace Aimeos\MShop\Index\Manager\Text;
12
13
14
/**
15
 * PostgreSQL based index text for searching in product tables.
16
 *
17
 * @package MShop
18
 * @subpackage Index
19
 */
20
class PgSQL
21
	extends \Aimeos\MShop\Index\Manager\Text\Standard
22
{
23
	private $searchConfig = array(
24
		'index.text:relevance' => array(
25
			'code' => 'index.text:relevance()',
26
			'internalcode' => ':site AND mindte."langid" = $1 AND CAST( mindte."content" @@ to_tsquery( $2 ) AS integer )',
27
			'label' => 'Product texts, parameter(<language ID>,<search term>)',
28
			'type' => 'float',
29
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
30
			'public' => false,
31
		),
32
		'sort:index.text:relevance' => array(
33
			'code' => 'sort:index.text:relevance()',
34
			'internalcode' => 'ts_rank(to_tsvector(mindte."content"), to_tsquery( $2 ))',
35
			'label' => 'Product text sorting, parameter(<language ID>,<search term>)',
36
			'type' => 'float',
37
			'internaltype' => \Aimeos\MW\DB\Statement\Base::PARAM_INT,
38
			'public' => false,
39
		),
40
	);
41
42
43
	/**
44
	 * Initializes the object
45
	 *
46
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
47
	 */
48
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
49
	{
50
		parent::__construct( $context );
51
52
		$level = \Aimeos\MShop\Locale\Manager\Base::SITE_ALL;
53
		$level = $context->getConfig()->get( 'mshop/index/manager/sitemode', $level );
54
55
		$name = 'index.text:relevance';
56
		$expr = $this->getSiteString( 'mindte."siteid"', $level );
57
		$this->searchConfig[$name]['internalcode'] = str_replace( ':site', $expr, $this->searchConfig[$name]['internalcode'] );
58
		$this->searchConfig[$name]['function'] = $this->getFunctionRelevance();
59
	}
60
61
62
	/**
63
	 * Returns a list of objects describing the available criterias for searching.
64
	 *
65
	 * @param bool $withsub Return also attributes of sub-managers if true
66
	 * @return \Aimeos\MW\Criteria\Attribute\Iface[] List of search attriubte items
67
	 */
68
	public function getSearchAttributes( bool $withsub = true ) : array
69
	{
70
		$list = parent::getSearchAttributes( $withsub );
71
72
		foreach( $this->searchConfig as $key => $fields ) {
73
			$list[$key] = new \Aimeos\MW\Criteria\Attribute\Standard( $fields );
74
		}
75
76
		return $list;
77
	}
78
79
80
	/**
81
	 * Returns the search function for searching by relevance
82
	 *
83
	 * @return \Closure Relevance search function
84
	 */
85
	protected function getFunctionRelevance()
86
	{
87
		return function( $source, array $params ) {
88
89
			if( isset( $params[1] ) )
90
			{
91
				$regex = '/(\&|\||\!|\-|\+|\>|\<|\(|\)|\~|\*|\:|\"|\'|\@|\\| )+/';
92
				$search = trim( preg_replace( $regex, ' ', $params[1] ), "' \t\n\r\0\x0B" );
93
94
				$str = implode( ':* & ', explode( ' ', mb_strtolower( $search ) ) );
95
				$params[1] = '\'' . $str . ':*\'';
96
			}
97
98
			return $params;
99
		};
100
	}
101
}
102