1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MusicBrainz\Filters; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This is the abstract filter which |
7
|
|
|
* contains the constructor which all |
8
|
|
|
* filters share because the only |
9
|
|
|
* difference between each filter class |
10
|
|
|
* is the valid argument types. |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
abstract class AbstractFilter |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $validArgTypes; |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $validArgs = array(); |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $protectedArgs = array( |
27
|
|
|
'arid' |
28
|
|
|
); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $args |
32
|
|
|
*/ |
33
|
|
|
public function __construct(array $args) |
34
|
|
|
{ |
35
|
|
|
foreach ($args as $key => $value) { |
36
|
|
|
if (in_array($key, $this->validArgTypes)) { |
37
|
|
|
$this->validArgs[$key] = $value; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $params |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function createParameters(array $params = array()) |
48
|
|
|
{ |
49
|
|
|
$params = $params + array('query' => ''); |
50
|
|
|
|
51
|
|
|
if (empty($this->validArgs) || $params['query'] != '') { |
52
|
|
|
return $params; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
foreach ($this->validArgs as $key => $val) { |
56
|
|
|
if ($params['query'] != '') { |
57
|
|
|
$params['query'] .= '+AND+'; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!in_array($key, $this->protectedArgs)) { |
61
|
|
|
// Lucene escape characters |
62
|
|
|
$val = urlencode( |
63
|
|
|
preg_replace('/([\+\-\!\(\)\{\}\[\]\^\~\*\?\:\\\\])/', '\\\\$1', $val) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
// If the search string contains a space, wrap it in brackets/quotes |
67
|
|
|
// This isn't always wanted, but for the searches required in this |
68
|
|
|
// library, I'm going to do it. |
69
|
|
|
if (preg_match('/[\+]/', $val)) { |
70
|
|
|
$val = '(' . $val . ')'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$params['query'] .= $key . ':' . $val; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $params; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|