ekinhbayar /
IntervalParser
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php declare(strict_types = 1); |
||
| 2 | /** |
||
| 3 | * Finds intervals inside strings |
||
| 4 | * |
||
| 5 | * PHP version 7+ |
||
| 6 | * |
||
| 7 | * @category IntervalParser |
||
| 8 | * @author Ekin H. Bayar <[email protected]> |
||
| 9 | * @version 0.2.0 |
||
| 10 | */ |
||
| 11 | namespace IntervalParser; |
||
| 12 | |||
| 13 | class IntervalFinder |
||
| 14 | { |
||
| 15 | # Leading separator |
||
| 16 | const LEADING_SEPARATOR = "(?<leadingSeparator>\s?(?:in)\s?)"; |
||
| 17 | |||
| 18 | # Regex to match a valid interval and any trailing string, holds the interval in $matches['interval'], the rest in $matches['trailing'] |
||
| 19 | const INTERVAL_WITH_TRAILING_DATA = "^(?<interval>(?&timepart)++)(?<trailing>.+)*?$/uix"; |
||
| 20 | |||
| 21 | # Regex to handle an input that may have multiple intervals along with leading and/or trailing data |
||
| 22 | const MULTIPLE_INTERVALS = <<<'REGEX' |
||
| 23 | ^(?<leading>.*?)? |
||
| 24 | (?<sep>(?&leadingSeparator))? |
||
| 25 | (?<interval>(?&timepart)++) |
||
| 26 | (?<trailing>.*) |
||
| 27 | /uix |
||
| 28 | REGEX; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var ParserSettings |
||
| 32 | */ |
||
| 33 | private $settings; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var Normalizer |
||
| 37 | */ |
||
| 38 | private $normalizer; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * IntervalFinder constructor. |
||
| 42 | * |
||
| 43 | * Default settings are : |
||
| 44 | * |
||
| 45 | * string $symbolSeparator = ',', |
||
| 46 | * string $wordSeparator = null |
||
| 47 | * |
||
| 48 | * @param \IntervalParser\ParserSettings $settings |
||
| 49 | * @param \IntervalParser\Normalizer $normalizer |
||
| 50 | */ |
||
| 51 | 4 | public function __construct(ParserSettings $settings, Normalizer $normalizer) |
|
| 52 | { |
||
| 53 | 4 | $this->settings = $settings; |
|
| 54 | 4 | $this->normalizer = $normalizer; |
|
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Looks for a valid interval along with leading and/or trailing data IF the respective flags are set. |
||
| 59 | * TimeInterval is essentially DateInterval with extra information such as interval offset & length, leading/trailing data. |
||
| 60 | * |
||
| 61 | * @param string $input |
||
| 62 | * @param int $flags |
||
| 63 | * @return TimeInterval|array |
||
| 64 | * @throws InvalidFlagException |
||
| 65 | * @throws FormatException |
||
| 66 | */ |
||
| 67 | 4 | public function find(string $input, int $flags = IntervalFlags::INTERVAL_ONLY) |
|
| 68 | { |
||
| 69 | if ($flags |
||
| 70 | 4 | & ~IntervalFlags::INTERVAL_ONLY |
|
| 71 | 4 | & ~IntervalFlags::REQUIRE_TRAILING |
|
| 72 | 4 | & ~IntervalFlags::REQUIRE_LEADING |
|
| 73 | 4 | & ~IntervalFlags::MULTIPLE_INTERVALS |
|
| 74 | ) { throw new InvalidFlagException("You have tried to use an invalid flag combination."); } |
||
| 75 | |||
| 76 | 4 | if ($flags & IntervalFlags::INTERVAL_ONLY) { |
|
| 77 | |||
| 78 | $input = $this->normalizer->normalize($input); |
||
| 79 | |||
| 80 | $definition = Pattern::DEFINE . Pattern::INTEGER . Pattern::TIME_PART . ')'; |
||
| 81 | $expression = $definition . Pattern::INTERVAL_ONLY; |
||
| 82 | |||
| 83 | if (preg_match($expression, $input)) { |
||
| 84 | $intervalOffset = 0; |
||
| 85 | $intervalLength = strlen($input); |
||
| 86 | |||
| 87 | # create and return the interval object |
||
| 88 | $interval = \DateInterval::createFromDateString($input); |
||
| 89 | return new TimeInterval($interval, $intervalOffset, $intervalLength); |
||
| 90 | } |
||
| 91 | |||
| 92 | throw new FormatException("Given input is not a valid interval."); |
||
| 93 | } |
||
| 94 | |||
| 95 | 4 | if ($flags == (IntervalFlags::REQUIRE_LEADING | IntervalFlags::REQUIRE_TRAILING)) { |
|
| 96 | |||
| 97 | 1 | $expression = $this->settings->getLeadingSeparatorExpression(); |
|
| 98 | |||
| 99 | 1 | $leadingSeparation = preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE); |
|
| 100 | 1 | if (!$leadingSeparation) { |
|
| 101 | throw new FormatException("Allowing leading data requires using a separator. Ie. foo in <interval>"); |
||
| 102 | } |
||
| 103 | |||
| 104 | 1 | $leadingData = $matches[1][0] ?? null; |
|
| 105 | 1 | $intervalAndTrailingData = $matches[2][0] ?? null; |
|
| 106 | |||
| 107 | # throw early for missing parts |
||
| 108 | 1 | if (!$leadingData) { |
|
| 109 | throw new FormatException("Given input does not contain a valid leading data."); |
||
| 110 | } |
||
| 111 | 1 | if (!$intervalAndTrailingData) { |
|
| 112 | throw new FormatException("Given input does not contain a valid interval and/or trailing data."); |
||
| 113 | } |
||
| 114 | |||
| 115 | 1 | $intervalOffset = $matches[2][1] ?? null; |
|
| 116 | |||
| 117 | # If interval contains non-strtotime-compatible abbreviations, replace 'em |
||
| 118 | 1 | $intervalAndTrailingData = $this->normalizer->normalize($intervalAndTrailingData); |
|
| 119 | |||
| 120 | 1 | $definition = Pattern::DEFINE . Pattern::INTEGER . Pattern::TIME_PART . ')'; |
|
| 121 | 1 | $expression = $definition . self::INTERVAL_WITH_TRAILING_DATA; |
|
| 122 | |||
| 123 | 1 | if (preg_match($expression, $intervalAndTrailingData, $parts)) { |
|
| 124 | |||
| 125 | 1 | $interval = $parts['interval']; |
|
| 126 | 1 | $trailingData = $parts['trailing']; |
|
| 127 | 1 | $intervalLength = strlen($interval); |
|
| 128 | |||
| 129 | # create and return the interval object |
||
| 130 | 1 | $interval = \DateInterval::createFromDateString($interval); |
|
| 131 | 1 | return new TimeInterval($interval, $intervalOffset, $intervalLength, $leadingData, $trailingData); |
|
| 132 | } |
||
| 133 | |||
| 134 | throw new FormatException("Given input does not contain a valid interval and/or trailing data."); |
||
| 135 | } |
||
| 136 | |||
| 137 | 3 | if ($flags & IntervalFlags::REQUIRE_LEADING) { |
|
| 138 | |||
| 139 | 1 | $expression = $this->settings->getLeadingSeparatorExpression(); |
|
| 140 | |||
| 141 | 1 | $leadingSeparation = preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE); |
|
| 142 | 1 | if (!$leadingSeparation) { |
|
| 143 | throw new FormatException("Allowing leading data requires using a separator. Ie. foo in <interval>"); |
||
| 144 | } |
||
| 145 | |||
| 146 | 1 | $leadingData = $matches[1][0] ?? null; |
|
| 147 | 1 | $intervalAndPossibleTrailingData = $matches[2][0] ?? null; |
|
| 148 | |||
| 149 | 1 | if (!$leadingData) { |
|
| 150 | throw new FormatException("Could not find any valid leading data."); |
||
| 151 | } |
||
| 152 | |||
| 153 | 1 | if (!$intervalAndPossibleTrailingData) { |
|
| 154 | throw new FormatException("Could not find any valid interval and/or leading data."); |
||
| 155 | } |
||
| 156 | |||
| 157 | 1 | $intervalOffset = $matches[2][1] ?? null; |
|
| 158 | |||
| 159 | # If interval contains non-strtotime-compatible abbreviations, replace 'em |
||
| 160 | 1 | $safeInterval = $this->normalizer->normalize($intervalAndPossibleTrailingData); |
|
| 161 | |||
| 162 | # since above normalization is expected to not return any trailing data, only check for a valid interval |
||
| 163 | 1 | $definition = Pattern::DEFINE . Pattern::INTEGER . Pattern::TIME_PART . ')'; |
|
| 164 | 1 | $expression = $definition . Pattern::INTERVAL_ONLY; |
|
| 165 | |||
| 166 | 1 | if (preg_match($expression, $safeInterval, $parts)) { |
|
| 167 | 1 | $interval = $parts['interval']; |
|
| 168 | 1 | $intervalLength = strlen($interval); |
|
| 169 | |||
| 170 | # create the interval object |
||
| 171 | 1 | $interval = \DateInterval::createFromDateString($interval); |
|
| 172 | 1 | return new TimeInterval($interval, $intervalOffset, $intervalLength, $leadingData); |
|
| 173 | } |
||
| 174 | |||
| 175 | throw new FormatException("Given input does not contain a valid interval. Keep in mind trailing data is not allowed with current flag."); |
||
| 176 | } |
||
| 177 | |||
| 178 | 2 | if ($flags & IntervalFlags::REQUIRE_TRAILING) { |
|
| 179 | |||
| 180 | 2 | $definition = Pattern::DEFINE . Pattern::INTEGER . Pattern::TIME_PART . ')'; |
|
| 181 | 2 | $expression = $definition . self::INTERVAL_WITH_TRAILING_DATA; |
|
| 182 | |||
| 183 | # If interval contains non-strtotime-compatible abbreviations, replace 'em |
||
| 184 | 2 | $safeInterval = $this->normalizer->normalize($input); |
|
| 185 | |||
| 186 | # Separate interval from trailing data |
||
| 187 | 2 | if (preg_match($expression, $safeInterval, $parts)) { |
|
| 188 | 2 | $trailingData = $parts['trailing'] ?? null; |
|
| 189 | 2 | $interval = $parts['interval'] ?? null; |
|
| 190 | |||
| 191 | 2 | if (!$interval) { |
|
| 192 | throw new FormatException("Could not find any valid interval."); |
||
| 193 | } |
||
| 194 | |||
| 195 | 2 | if (!$trailingData) { |
|
| 196 | throw new FormatException("Could not find any valid trailing data."); |
||
| 197 | } |
||
| 198 | |||
| 199 | 2 | $intervalLength = strlen($interval); |
|
| 200 | 2 | $intervalOffset = 0; # since we don't allow leading data here |
|
| 201 | |||
| 202 | # create the interval object |
||
| 203 | 2 | $interval = \DateInterval::createFromDateString($interval); |
|
| 204 | 2 | return new TimeInterval($interval, $intervalOffset, $intervalLength, null, $trailingData); |
|
| 205 | } |
||
| 206 | |||
| 207 | throw new FormatException("Given input does not contain a valid interval. Keep in mind leading data is not allowed with current flag."); |
||
| 208 | } |
||
| 209 | |||
| 210 | if ($flags & IntervalFlags::MULTIPLE_INTERVALS) { |
||
| 211 | |||
| 212 | $payload = []; |
||
| 213 | $separator = ($this->settings->getSeparationType() == 'symbol') |
||
| 214 | ? $this->settings->getSymbolSeparator() |
||
| 215 | : $this->settings->getWordSeparator(); |
||
| 216 | |||
| 217 | $expression = "/(?J)\b(?:(?<match>.*?)\s?{$separator})\s?|\b(?<match>.*)/ui"; |
||
|
0 ignored issues
–
show
|
|||
| 218 | |||
| 219 | if (preg_match_all($expression, $input, $intervals, PREG_SET_ORDER)) { |
||
| 220 | |||
| 221 | $intervalSet = array_filter(array_map(function($set) { |
||
| 222 | foreach ($iter = new IntervalIterator($set) as $key => $interval) { |
||
| 223 | if ($iter->key() === 'match') { |
||
| 224 | return $interval; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | }, $intervals)); |
||
| 228 | |||
| 229 | foreach ($intervalSet as $key => $interval) { |
||
| 230 | |||
| 231 | $definition = Pattern::DEFINE . self::LEADING_SEPARATOR . Pattern::INTEGER . Pattern::TIME_PART . ')'; |
||
| 232 | $expression = $definition . self::MULTIPLE_INTERVALS; |
||
| 233 | |||
| 234 | preg_match($expression, $interval, $matches); |
||
| 235 | $matches = array_filter($matches); |
||
| 236 | |||
| 237 | $leadingData = $matches['leading'] ?? null; |
||
| 238 | $leadingSep = $matches['sep'] ?? null; |
||
| 239 | $interval = $matches['interval'] ?? null; |
||
| 240 | $trailing = $matches['trailing'] ?? null; |
||
| 241 | |||
| 242 | if (!$leadingData) $leadingData = $leadingSep ?? ""; |
||
| 243 | |||
| 244 | $intervalOffset = (!$leadingSep) ? 0 : strlen($leadingData) + strlen($leadingSep); |
||
| 245 | |||
| 246 | # If interval contains non-strtotime-compatible abbreviations, replace them |
||
| 247 | $safeInterval = $this->normalizer->normalize($interval . $trailing); |
||
| 248 | |||
| 249 | # Separate intervals from trailing data |
||
| 250 | if (preg_match($expression, $safeInterval, $parts)) { |
||
| 251 | $trailingData = $parts['trailing'] ?? null; |
||
| 252 | $interval = $parts['interval'] ?? null; |
||
| 253 | if (!$interval) continue; |
||
| 254 | |||
| 255 | $intervalLength = strlen($interval); |
||
| 256 | # create the interval object |
||
| 257 | $interval = \DateInterval::createFromDateString($interval); |
||
| 258 | $payload[] = new TimeInterval($interval, $intervalOffset, $intervalLength, $leadingData, $trailingData); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 262 | if ($payload) return $payload; |
||
|
0 ignored issues
–
show
The expression
$payload of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||
| 263 | } |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.