1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of gpupo/search |
5
|
|
|
* |
6
|
|
|
* (c) Gilmar Pupo <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Gpupo\Search\Query; |
13
|
|
|
|
14
|
|
|
abstract class KeywordsAbstract |
15
|
|
|
{ |
16
|
|
|
protected $data = [ |
17
|
|
|
'key' => '*', |
18
|
|
|
'values' => [], |
19
|
|
|
'strict' => false, |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
public function addKeyword($string) |
23
|
|
|
{ |
24
|
|
|
if (strlen($string) > 2) { |
25
|
|
|
$this->data['values'][] = $string; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setKey($key) |
32
|
|
|
{ |
33
|
|
|
$this->data['key'] = $key; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function setStrict($bool) |
37
|
|
|
{ |
38
|
|
|
$this->data['strict'] = $bool; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setData($key, array $values, $strict = false) |
42
|
|
|
{ |
43
|
|
|
$array = [ |
44
|
|
|
'key' => $key, |
45
|
|
|
'values' => $values, |
46
|
|
|
'strict' => $strict, |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
return $this->data = $array; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getData() |
53
|
|
|
{ |
54
|
|
|
return $this->data; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getValues() |
58
|
|
|
{ |
59
|
|
|
return $this->data['values']; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getKey() |
63
|
|
|
{ |
64
|
|
|
return $this->data['key']; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Recebe a string pesquisada. |
69
|
|
|
* |
70
|
|
|
* @param string $string |
71
|
|
|
*/ |
72
|
|
|
public function readString($string) |
73
|
|
|
{ |
74
|
|
|
$string = str_replace( |
75
|
|
|
["'", '"', 'buscar', '�'], |
76
|
|
|
[' ', ' ', ' ', ' '], |
77
|
|
|
strtolower(trim($string)) |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
if (empty($string)) { |
81
|
|
|
throw new \InvalidArgumentException('Palavra chave nao pode ser vazia'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (strlen(preg_replace('/[^A-Za-z0-9?!]/', '', $string)) < 3) { |
85
|
|
|
throw new \InvalidArgumentException('Palavra chave deve ter mais que 3 caracteres'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$array = explode(' ', $string); |
89
|
|
|
|
90
|
|
|
foreach ($array as $keyword) { |
91
|
|
|
$this->addKeyword($keyword); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|