regexp = $regexp; $this->isGlobal = $isGlobal; } /** * Return this regexp as a string * * @return string */ public function __toString() { return $this->regexp; } /** * {@inheritdoc} */ public function asConfig() { return $this; } /** * {@inheritdoc} */ public function filterConfig($target) { return ($target === 'JS') ? new Code($this->getJS()) : (string) $this; } /** * Return the name of each capture in this regexp * * @return string[] */ public function getCaptureNames() { return RegexpParser::getCaptureNames($this->regexp); } /** * Return this regexp's JavaScript representation * * @return string */ public function getJS() { if (!isset($this->jsRegexp)) { $this->jsRegexp = RegexpConvertor::toJS($this->regexp, $this->isGlobal); } return $this->jsRegexp; } /** * Return all the named captures with a standalone regexp that matches them * * @return array Array of [capture name => regexp] */ public function getNamedCaptures() { $captures = []; $regexpInfo = RegexpParser::parse($this->regexp); // Prepare the start/end of the regexp and ensure that we use the D modifier $start = $regexpInfo['delimiter'] . '^'; $end = '$' . $regexpInfo['delimiter'] . $regexpInfo['modifiers']; if (strpos($regexpInfo['modifiers'], 'D') === false) { $end .= 'D'; } foreach ($this->getNamedCapturesExpressions($regexpInfo['tokens']) as $name => $expr) { $captures[$name] = $start . $expr . $end; } return $captures; } /** * Return the expression used in each named capture * * @param array[] $tokens * @return array */ protected function getNamedCapturesExpressions(array $tokens) { $exprs = []; foreach ($tokens as $token) { if ($token['type'] !== 'capturingSubpatternStart' || !isset($token['name'])) { continue; } $expr = $token['content']; if (strpos($expr, '|') !== false) { $expr = '(?:' . $expr . ')'; } $exprs[$token['name']] = $expr; } return $exprs; } /** * Set this regexp's JavaScript representation * * @param string $jsRegexp * @return void */ public function setJS($jsRegexp) { $this->jsRegexp = $jsRegexp; } }