1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
/** Parser Settings |
3
|
|
|
* |
4
|
|
|
* IntervalParser takes a ParserSettings object which is handy for when you want to deal with multiple intervals. |
5
|
|
|
* |
6
|
|
|
* When parsing multiple intervals if you don't supply your own settings, |
7
|
|
|
* the parser will try to find comma separated intervals within given input by default. |
8
|
|
|
* However there is no default value for $wordSeparator (actually, there is "word"). |
9
|
|
|
* |
10
|
|
|
* Example: |
11
|
|
|
* |
12
|
|
|
* $parserSettings = new ParserSettings(1, ',', 'then'); |
13
|
|
|
* # works for "2 days then 5 months then 2 hours" |
14
|
|
|
* $intervalParser = new IntervalParser($parserSettings); |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
namespace IntervalParser; |
18
|
|
|
|
19
|
|
|
class ParserSettings |
20
|
|
|
{ |
21
|
|
|
const SYMBOL = 0b00000000; |
22
|
|
|
const STRING = 0b00000001; |
23
|
|
|
|
24
|
|
|
private $multipleSeparationType; |
25
|
|
|
private $multipleSeparationSymbol; |
26
|
|
|
private $multipleSeparationWord; |
27
|
|
|
private $leadingSeparationString; |
28
|
|
|
private $keepLeadingSeparator; |
29
|
|
|
|
30
|
|
|
# Leading separator with capturing groups |
|
|
|
|
31
|
|
|
public static $leadingGroupSeparator = /* @lang text */ '/(.*)\s+(?:in)\s+(.*)/ui'; |
32
|
|
|
public static $symbolSeparator = /* @lang text */ '/(?<first>[^,]*)\s?,\s?(?<next>.*)$/ui'; |
|
|
|
|
33
|
|
|
public static $wordSeparator = /* @lang text */ '/^(?<first>.*?)\s?foo\s?(?<next>.*)$/ui'; |
|
|
|
|
34
|
|
|
|
35
|
|
|
|
36
|
22 |
|
public function __construct( |
37
|
|
|
string $leadingSeparationString = 'in', |
38
|
|
|
bool $keepLeadingSeparator = false, |
39
|
|
|
int $multipleSeparationType = self::SYMBOL, |
40
|
|
|
string $multipleSeparationSymbol = ',', |
41
|
|
|
string $multipleSeparationWord = 'foo' |
42
|
|
|
) |
43
|
|
|
{ |
44
|
22 |
|
$this->leadingSeparationString = $leadingSeparationString; |
|
|
|
|
45
|
22 |
|
$this->keepLeadingSeparator = $keepLeadingSeparator; |
|
|
|
|
46
|
22 |
|
$this->multipleSeparationType = $multipleSeparationType; |
|
|
|
|
47
|
22 |
|
$this->multipleSeparationSymbol = $multipleSeparationSymbol; |
48
|
22 |
|
$this->multipleSeparationWord = $multipleSeparationWord; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
6 |
|
public function getLeadingSeparator() : string |
52
|
|
|
{ |
53
|
6 |
|
return $this->leadingSeparationString; |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
public function getSymbolSeparator() : string |
57
|
|
|
{ |
58
|
4 |
|
return $this->multipleSeparationSymbol; |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
public function getWordSeparator() : string |
62
|
|
|
{ |
63
|
4 |
|
return $this->multipleSeparationWord; |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
public function getLeadingSeparatorExpression() : string |
67
|
|
|
{ |
68
|
4 |
|
$expression = preg_replace("/in/", $this->getLeadingSeparator(), self::$leadingGroupSeparator); |
|
|
|
|
69
|
4 |
|
return $expression; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
public function getSymbolSeparatorExpression() : string |
73
|
|
|
{ |
74
|
2 |
|
$expression = preg_replace("/,/", $this->getSymbolSeparator(), self::$symbolSeparator); |
|
|
|
|
75
|
2 |
|
return $expression; |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
public function getWordSeparatorExpression() : string |
79
|
|
|
{ |
80
|
2 |
|
$expression = preg_replace("/foo/", $this->getWordSeparator(), self::$wordSeparator); |
|
|
|
|
81
|
2 |
|
return $expression; |
82
|
|
|
} |
83
|
|
|
|
84
|
3 |
|
public function keepLeadingSeparator() : bool |
85
|
|
|
{ |
86
|
3 |
|
return $this->keepLeadingSeparator; |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
public function getSeparationType(): string |
90
|
|
|
{ |
91
|
3 |
|
return ($this->multipleSeparationType == self::STRING) ? 'string' : 'symbol'; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|