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. * * @return bool */ public function hasFilter() { return $this->filter; } /** * Check whether a string matches the current filter options. * * @param string $string * @param array $matches * * @return bool */ public function match($string, array &$matches = null) { return $this->filter === false || (preg_match($this->pattern, $string, $matches) xor $this->invert); } /** * Validate that grep, invert and insensitive input options are consistent. * * @param InputInterface $input * * @return bool */ private function validateInput(InputInterface $input) { if (!$input->getOption('grep')) { foreach (array('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 * * @return bool */ private function stringIsRegex($string) { return substr($string, 0, 1) === '/' && substr($string, -1) === '/' && strlen($string) >= 3; } /** * Validate that $pattern is a valid regular expression. * * @param string $pattern * * @return bool */ private function validateRegex($pattern) { set_error_handler(array('Psy\Exception\ErrorException', 'throwException')); try { preg_match($pattern, ''); } catch (ErrorException $e) { throw new RuntimeException(str_replace('preg_match(): ', 'Invalid regular expression: ', $e->getRawMessage())); } restore_error_handler(); } }