CCrmUrlTemplate::Prepare

  1. Bitrix24 API (v. 23.675.0)
  2. crm
  3. CCrmUrlTemplate
  4. Prepare
  • Модуль: crm
  • Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_url_util.php
  • Класс: \CCrmUrlTemplate
  • Вызов: CCrmUrlTemplate::Prepare
private function Prepare()
{
	if($this->isReady)
	{
		return;
	}

	$this->nodes = array();

	$this->template = preg_replace('/[\r\n]/', '', $this->template);
	$result = preg_match_all('/\[\s*\/?\s*[a-z0-9_]+\s*\/?\s*\]/i'.BX_UTF_PCRE_MODIFIER,
		$this->template,
		$matches,
		PREG_SET_ORDER|PREG_OFFSET_CAPTURE
	);

	if(!(is_int($result) && $result > 0))
	{
		return;
	}

	$curNode = null;
	$lastNode = null;
	$offset = 0;
	foreach($matches as &$match)
	{
		$m = &$match[0];
		if(!(is_array($m) && count($m) === 2))
		{
			continue;
		}

		$tag = $m[0];
		$tagLength = mb_strlen($tag);
		$tagName = trim(mb_substr($tag, 1, $tagLength - 2));
		$slashPos = mb_strpos($tagName, '/');
		$isEnd = $slashPos === 0;
		$isSelfClosing = $slashPos === mb_strlen($tagName) - 1;

		if($isEnd)
		{
			$tagName = trim(mb_substr($tagName, 1));
		}
		elseif($isSelfClosing)
		{
			$tagName = trim(mb_substr($tagName, 0, mb_strlen($tagName) - 1));
		}

		if(!$isSelfClosing && !self::IsChildNodesSupported($tagName))
		{
			$isSelfClosing = true;
		}

		$node = array(
			'nodeType' => 1, //object
			'name' => mb_strtoupper($tagName),
			'offset' => intval($m[1]),
			'length' => $tagLength,
			'isEnd' => $isEnd,
			'isSelfClosing' => $isSelfClosing,
			'parent' => null,
			'nodes' => array(),
			'end' => null
		);

		$lastNode = &$node;

		if($node['offset'] > $offset)
		{
			$textNode = array(
				'nodeType' => 2, //text
				'content' => mb_substr($this->template, $offset, $node['offset'] - $offset)
			);

			if($curNode)
			{
				$curNode['nodes'][] = &$textNode;
				$textNode['parent'] = &$curNode;
			}
			else
			{
				$this->nodes[] = &$textNode;
			}
		}
		$offset = $node['offset'] + $node['length'];

		if(!$isEnd)
		{
			if($curNode)
			{
				$curNode['nodes'][] = &$node;
				$node['parent'] = &$curNode;
			}
			else
			{
				$this->nodes[] = &$node;
			}

			if(!$isSelfClosing)
			{
				unset($curNode);
				$curNode = &$node;
			}
		}
		else
		{
			//End tags without opened tag will be ignored.
			$parent = &$curNode;
			while($parent)
			{
				if($parent['name'] === $node['name'])
				{
					$parent['end'] = &$node;
					$node['parent'] = &$parent;
					break;
				}

				$parent = &$parent['parent'];
			}
			unset($parent);
		}
		unset($node, $textNode);
	}
	unset($match, $m, $curNode);

	if($lastNode)
	{
		$endPos = $lastNode['offset'] + $lastNode['length'];
		if($endPos < (mb_strlen($this->template) - 1))
		{
			$this->nodes[] = array(
				'nodeType' => 2, //text
				'content' => mb_substr($this->template, $endPos)
			);
		}
	}
	unset($lastNode);

	$this->isReady = true;
}

Добавить комментарий