Completed
Push — master ( b7fbe3...8fe22f )
by Peter
02:11
created

RegexMatcher::matches()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.1406

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 9
cts 12
cp 0.75
rs 9.4285
cc 3
eloc 13
nc 3
nop 2
crap 3.1406
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL, Commercial license.
5
 *
6
 * @package maslosoft/addendum
7
 * @licence AGPL, Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]> (Meta container, further improvements, bugfixes)
9
 * @copyright Copyright (c) Maslosoft (Meta container, further improvements, bugfixes)
10
 * @copyright Copyright (c) Jan Suchal (Original version, builder, parser)
11
 * @link http://maslosoft.com/addendum/ - maslosoft addendum
12
 * @link https://code.google.com/p/addendum/ - original addendum project
13
 */
14
15
namespace Maslosoft\Addendum\Matcher;
16
17
use Maslosoft\Addendum\Exceptions\MatcherException;
18
use Maslosoft\Addendum\Matcher\Traits\PluginsTrait;
19
20
class RegexMatcher implements \Maslosoft\Addendum\Interfaces\Matcher\MatcherInterface
21
{
22
	use PluginsTrait;
23
24
	protected $regex;
25
26 51
	public function __construct($regex)
27
	{
28 51
		$this->regex = $regex;
29 51
	}
30
31 51
	public function matches($string, &$value)
32
	{
33 51
		$matches = [];
34 51
		$matched = preg_match("/^{$this->regex}/", $string, $matches);
35 51
		if ($matched)
36
		{
37 51
			$value = $this->process($matches);
38 51
			return strlen($matches[0]);
39
		}
40 51
		if (false === $matched)
41
		{
42
			$params = [
43
				$this->regex,
44
				$string
45
			];
46
			throw new MatcherException(vsprintf('Could not interpret matcher regex: `%s`, When processing "%s". ', $params), preg_last_error());
47
		}
48 51
		$value = false;
49 51
		return false;
50
	}
51
52 51
	protected function process($matches)
53
	{
54 51
		return $matches[0];
55
	}
56
57
}
58