validateInput($input); if (!$pattern = $input->getOption('grep')) { $this->filter = false; return; } if (!$this->stringIsRegex($pattern)) { $pattern = '/'.\preg_quote($pattern, '/').'/'; } if ($insensitive = $input->getOption('insensitive')) { $pattern .= 'i'; } $this->validateRegex($pattern); $this->filter = true; $this->pattern = $pattern; $this->insensitive = $insensitive; $this->invert = $input->getOption('invert'); } /** * Check whether the bound input has filter options. */ public function hasFilter(): bool { return $this->filter; } /** * Check whether a string matches the current filter options. * * @param string $string * @param array $matches */ public function match(string $string, array &$matches = null): bool { return $this->filter === false || (\preg_match($this->pattern, $string, $matches) xor $this->invert); } /** * Validate that grep, invert and insensitive input options are consistent. * * @throws RuntimeException if input is invalid * * @param InputInterface $input */ private function validateInput(InputInterface $input) { if (!$input->getOption('grep')) { foreach (['invert', 'insensitive'] as $option) { if ($input->getOption($option)) { throw new RuntimeException('--'.$option.' does not make sense without --grep'); } } } } /** * Check whether a string appears to be a regular expression. * * @param string $string */ private function stringIsRegex(string $string): bool { return \substr($string, 0, 1) === '/' && \substr($string, -1) === '/' && \strlen($string) >= 3; } /** * Validate that $pattern is a valid regular expression. * * @throws RuntimeException if pattern is invalid * * @param string $pattern */ private function validateRegex(string $pattern) { \set_error_handler([ErrorException::class, 'throwException']); try { \preg_match($pattern, ''); } catch (ErrorException $e) { throw new RuntimeException(\str_replace('preg_match(): ', 'Invalid regular expression: ', $e->getRawMessage())); } finally { \restore_error_handler(); } } }