|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the pinepain/php-v8-js-sandbox PHP library. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Licensed under the MIT license: http://opensource.org/licenses/MIT |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the |
|
11
|
|
|
* LICENSE file that was distributed with this source or visit |
|
12
|
|
|
* http://opensource.org/licenses/MIT |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
namespace Pinepain\JsSandbox\Extractors\PlainExtractors; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
use Pinepain\JsSandbox\Extractors\Definition\PlainExtractorDefinitionInterface; |
|
20
|
|
|
use Pinepain\JsSandbox\Extractors\ExtractorException; |
|
21
|
|
|
use Pinepain\JsSandbox\Extractors\ExtractorInterface; |
|
22
|
|
|
use V8\Context; |
|
23
|
|
|
use V8\RegExpObject; |
|
24
|
|
|
use V8\Value; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
class RegExpExtractor implements PlainExtractorInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function extract(Context $context, Value $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor) |
|
33
|
|
|
{ |
|
34
|
|
|
if ($value instanceof RegExpObject) { |
|
35
|
|
|
|
|
36
|
|
|
$regex = $value->getSource()->value(); |
|
37
|
|
|
|
|
38
|
|
|
if ($definition->getNext() && 'string' == $definition->getNext()->getName()) { |
|
39
|
|
|
return $regex; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$flags = $value->getFlags(); |
|
43
|
|
|
$regexp = '/' . preg_quote($regex, '/') . '/'; |
|
44
|
|
|
|
|
45
|
|
|
if (!$flags) { |
|
46
|
|
|
return $regexp; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if ($flags & RegExpObject::FLAG_GLOBAL) { |
|
50
|
|
|
// global flag is not supported in PHP |
|
51
|
|
|
throw new ExtractorException('Global flag is not supported'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ($flags & RegExpObject::FLAG_IGNORE_CASE) { |
|
55
|
|
|
$regexp .= 'i'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if ($flags & RegExpObject::FLAG_MULTILINE) { |
|
59
|
|
|
$regexp .= 'm'; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ($flags & RegExpObject::FLAG_STICKY) { |
|
63
|
|
|
// sticky flag is not supported in PHP |
|
64
|
|
|
throw new ExtractorException('Sticky flag is not supported'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($flags & RegExpObject::FLAG_UNICODE) { |
|
68
|
|
|
$regexp .= 'u'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($flags & RegExpObject::FLAG_DOTALL) { |
|
72
|
|
|
$regexp .= 's'; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $regexp; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
throw new ExtractorException('Value must be of the type regexp, ' . $value->typeOf()->value() . ' given'); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|