/** * Replaces tags in content text with their corresponding values from the provided array.<br /> * Does some limited checks to make sure everything went fine during replacing. * * This method does NOT take a reference to $mContent and as such does not change the original * text, unlike doParseRef. Therefore, you must store the returning content into a new variable * in order to use it. Also note that this method does not allow chaining of the parser object. * * @param array $aTagValues An associative array of tags and their values * @param mixed $mContent The content containing tags to be parsed * * @return mixed * @throws ParserException Invalid Content triggers a Parser Exception * @throws ParserException Invalid Tag value triggers a Parser Exception * @throws ParserException Detecting unparsed tags in the final content triggers a Parser Exception */ public function doParse($aTagValues, $mContent) { if (!$this->verifyContentArray($mContent)) { $e = new ParserException('The provided content is invalid. It needs to be a string, an array of strings, or an assoc. array of strings.'); throw $e->setContent($mContent)->setTagValues($aTagValues); } $aTags = $this->extractTags($mContent); foreach ($aTagValues as $sTag => $sValue) { if (in_array($sTag, $aTags)) { $sValue = trim($sValue); if (empty($sValue) || !is_string($sTag) || !is_numeric($sValue) && !is_string($sValue)) { $e = new ParserException('Tag ' . $sTag . ' is invalid.'); throw $e->setExtraInfo(array('content' => $mContent, 'unparsed_tags' => $aTags, 'given_values' => $aTagValues)); } if (is_string($mContent)) { $mContent = preg_replace('/\\{' . $sTag . '\\}/s', $sValue, $mContent); } else { foreach ($mContent as &$mText) { $mText = preg_replace('/\\{' . $sTag . '\\}/s', $sValue, $mText); } } } } // Check if there's any unparsed tags left $aTags = $this->extractTags($mContent); if (!empty($aTags)) { $e = new ParserException('Some unparsed tags were detected in the content after parsing.'); throw $e->setExtraInfo(array('content' => $mContent, 'unparsed_tags' => $aTags, 'given_values' => $aTagValues)); } $this->finalContentCheck($mContent); return $mContent; }