PrefixQueryDecorator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 25
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A decorate() 0 20 3
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL-3.0-only, proprietary` license[s].
5
 *
6
 * @package maslosoft/manganel
7
 * @license AGPL-3.0-only, proprietary
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link https://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel\Decorators\QueryBuilder\QueryString;
14
15
use Maslosoft\Manganel\Interfaces\QueryBuilder\QueryStringDecoratorInterface;
16
use Maslosoft\Manganel\SearchCriteria;
17
18
/**
19
 * This is default decorator for queries, this provides better "As You Type"
20
 * experience. If it's not desired, use `QueryDecorator` instead.
21
 *
22
 * @see QueryDecorator
23
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
24
 */
25
class PrefixQueryDecorator implements QueryStringDecoratorInterface
26
{
27
28 24
	public function decorate(&$queryStringParams, SearchCriteria $criteria)
29
	{
30 24
		$q = $criteria->getSearch();
31
32
		// Add `*` only if :
33
		// - not contain wildcard on end
34
		// - not have space on end
35
		// - ends with letter
36
		// NOTE: Passing two wildcards to query will yield nothing,
37
		// thats why it is checked too.
38 24
		if (!preg_match('~\*$~u', $q))
39
		{
40
			// Add `*` only if ends with any alphabet letter (phrase_prefix)
41 23
			if (preg_match('~\p{L}$~u', $q))
42
			{
43 17
				$q .= '*';
44
			}
45
		}
46 24
		$queryStringParams['query'] = $q;
47 24
	}
48
49
}
50