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

MySQL::getFunctionRelevance()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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