1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Phiremock. |
4
|
|
|
* |
5
|
|
|
* Phiremock is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* Phiremock is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with Phiremock. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Mcustiel\Phiremock\Domain\Condition; |
20
|
|
|
|
21
|
|
|
use InvalidArgumentException; |
22
|
|
|
|
23
|
|
|
class Pattern extends ConditionValue |
24
|
|
|
{ |
25
|
27 |
|
public function __construct(string $pattern) |
26
|
|
|
{ |
27
|
27 |
|
$this->assertRegex($pattern); |
28
|
18 |
|
parent::__construct($pattern); |
29
|
18 |
|
} |
30
|
|
|
|
31
|
27 |
|
private function assertRegex(string $pattern): void |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* The only sane way to validate a regexp is to execute it. |
35
|
|
|
* Possible warnings or notices are suppressed. |
36
|
|
|
*/ |
37
|
27 |
|
if (false === @preg_match($pattern, null)) { |
38
|
9 |
|
throw new InvalidArgumentException( |
39
|
9 |
|
sprintf('Invalid regular expression received: `%s`, preg error #%d', $pattern, preg_last_error()) |
40
|
|
|
); |
41
|
|
|
} |
42
|
18 |
|
} |
43
|
|
|
} |
44
|
|
|
|