/** * @dataProvider providerContructWithValidContent */ public function testConstructWithValidContent($name, $parameters, $isCloseTag, $content) { $tag = new BBCodeTag($content); $this->assertEquals($name, $tag->getTagName()); $this->assertEquals($parameters, $tag->getParameters()); $this->assertEquals($isCloseTag, $tag->isCloseTag()); }
/** * Parses the set tags on the provided string * @param string $string String to parse * @return string Parsed string */ private function processTags($string) { $index = 0; $tokens = array(); do { $positionOpen = strpos($string, Tag::OPEN, $index); if ($positionOpen === false) { continue; } $positionClose = strpos($string, TAG::CLOSE, $positionOpen); if (!$positionClose) { continue; } $tagContent = substr($string, $positionOpen + 1, $positionClose - $positionOpen - 1); try { $tagParser = new BBCodeTag($tagContent); } catch (ZiboException $exception) { $index = $positionClose; continue; } $tagName = $tagParser->getTagName(); if ($tagParser->isCloseTag() || !array_key_exists($tagName, $this->tags)) { $index = $positionClose; continue; } $tag = $this->tags[$tagName]; if ($tag->hasCloseTag()) { $closeTag = Tag::OPEN . Tag::CLOSE_PREFIX . $tagName . Tag::CLOSE; $positionCloseTag = strpos($string, $closeTag, $positionClose); if ($positionCloseTag === false) { $index = $positionClose; continue; } $content = substr($string, $positionClose + 1, $positionCloseTag - $positionClose - 1); if ($tag->needsContentParsing()) { $content = $this->processTags($content); } try { $parsed = $tag->parseTag($content, $tagParser->getParameters()); if ($parsed === false) { throw new ZiboException('Could not parse tag ' . $tagName); } } catch (Exception $exception) { $index = $positionClose; continue; } $tokens[] = substr($string, 0, $positionOpen); $tokens[] = $parsed; $string = substr($string, $positionCloseTag + strlen($closeTag)); $index = 0; } else { try { $parsed = $tag->parseTag('', $tagParser->getParameters()); if ($parsed === false) { throw new ZiboException('Could not parse tag ' . $tagName); } } catch (Exception $exception) { $index = $positionClose; continue; } $tokens[] = substr($string, 0, $positionOpen); $tokens[] = $parsed; $string = substr($string, $positionClose); $index = 0; } } while ($positionOpen !== false && $positionClose !== false); $tokens[] = $string; return implode('', $tokens); }