/** * This method will try to extract link & transform it according to rules defined in config. * * @param $string * * @return bool|mixed $string false|string Will return title or false if extraction didn't produced any result. * @throws RegexException Supplied extraction/transform regex is invalid. */ public function extractLink($string) { $matchResult = @preg_match($this->configuration->getLinkExtractPattern(), $string, $matches); if ($matchResult === false) { throw new RegexException('Pattern was configured for ' . $this->getName() . ' aggregator to extract link', $this->configuration->getLinkExtractPattern()); } elseif ($matchResult === 0 || !isset($matches[1])) { return false; } elseif (isset($matches[2])) { $this->logger->warning('Pattern ' . $this->configuration->getLinkExtractPattern() . ' configured in ' . $this->getName() . ' aggregator to extract link resulted in ' . (count($matches) - 1) . ' matches instead of one for input string "' . $string . '". Only first one will be used.'); } $transformPattern = $this->configuration->getLinkTransformPattern(); if ($transformPattern !== null) { //Check if transformation is needed $matches[1] = @preg_replace($transformPattern[0], $transformPattern[1], $matches[1]); if ($matches[1] === null) { //PHP weirdo - null will be returned for error throw new RegexException('Pattern was configured for ' . $this->getName() . ' aggregator to transform link', "[{$transformPattern[0]}, {$transformPattern[0]}]"); } } return $matches[1]; }
/** * @dataProvider exampleLinkTransformPatternsProvider */ public function testLinkTransformPatternCanBeSetToCorrectValues($pattern) { $this->subjectUnderTest->setLinkTransformPattern($pattern); $this->assertSame($pattern, $this->subjectUnderTest->getLinkTransformPattern()); }