-
-
Notifications
You must be signed in to change notification settings - Fork 828
/
AbstractRegexGambit.php
58 lines (50 loc) · 1.41 KB
/
AbstractRegexGambit.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Search;
abstract class AbstractRegexGambit implements GambitInterface
{
/**
* The regex pattern to match the bit against.
*
* @var string
*/
protected $pattern;
/**
* {@inheritdoc}
*/
public function apply(AbstractSearch $search, $bit)
{
if ($matches = $this->match($bit)) {
list($negate) = array_splice($matches, 1, 1);
$this->conditions($search, $matches, (bool) $negate);
}
return (bool) $matches;
}
/**
* Match the bit against this gambit.
*
* @param string $bit
* @return array
*/
protected function match($bit)
{
if (preg_match('/^(-?)'.$this->pattern.'$/i', $bit, $matches)) {
return $matches;
}
}
/**
* Apply conditions to the search, given that the gambit was matched.
*
* @param AbstractSearch $search The search object.
* @param array $matches An array of matches from the search bit.
* @param bool $negate Whether or not the bit was negated, and thus whether
* or not the conditions should be negated.
* @return mixed
*/
abstract protected function conditions(AbstractSearch $search, array $matches, $negate);
}