Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
engine.php
1<?php
7/*
8\Bitrix\Main\Loader::includeModule('iblock');
9
10$arFields = array(
11 "IBLOCK_ID" => 422,
12 "NAME" => "Product name",
13 "CODE" => "main code",
14 "IBLOCK_SECTION_ID" => 12744,
15 "PROPERTY_VALUES" => array(
16 "CML2_ARTICLE" => "XX-SM6-XXXXXXXXX",
17 ),
18);
19$element = new \Bitrix\Iblock\Template\Entity\Element(0);
20$element->setFields($arFields);
21echo "<pre>";//print_r($element);
22$arSkuCollection = array();
23for ($i = 0; $i < 5; $i++)
24{
25 $arSkuCollection[$i] = array(
26 "IBLOCK_ID" => 423,
27 "NAME" => null,
28 "CODE" => "code($i)",
29 "PROPERTY_VALUES" => array(
30 "CML2_LINK" => $element,
31 "1377" => "0x0555555".$i,
32 "1875" => 1045,
33 ),
34 );
35 $sku = new \Bitrix\Iblock\Template\Entity\Element(0);
36 $sku->setFields($arSkuCollection[$i]);//print_r($sku);
37 $arSkuCollection[$i]["NAME"] = \Bitrix\Iblock\Template\Engine::process(
38 $sku
39 //,'{=this.property.cml2_link.name}: {=this.property.cml2_link.property.CML2_ARTICUL}, {=this.property.cml2_bar_code}'
40 //,'Q{=this.code}Q'
41 //,'Q{=this.property}Q'
42 ,'Q{=this.property.1377}Q{=this.property.1875}Q'
43 //,'Q{=this.property.CML2_LINK}Q'
44 //,'Q{=this.property.CML2_LINK.name}Q'
45 //,'Q{=this.property.CML2_LINK.code}Q'
46 //,'Q{=this.property.CML2_LINK.property.CML2_ARTICLE}Q'
47 //,'Q{=this.property.CML2_LINK.parent.property.str}Q'
48 //,'Q{=this.catalog.price.base}Q'
49 //,'Q{=this.catalog.weight}Q'
50 //,'Q{=this.catalog.measure}Q'
51 //,'Q{=this.catalog.store.1.name}Q'
52 //,'Q{=catalog.store}Q'
53 );
54}
55echo "<pre>",htmlspecialcharsEx(print_r($arSkuCollection,1)),"</pre>";
56*/
63class Engine
64{
80 public static function process(Entity\Base $entity, $template)
81 {
82 $rootNode = self::parseTemplateTree($template, new NodeRoot);
83 return $rootNode->process($entity);
84 }
85
94 protected static function parseTemplateTree($template, NodeRoot $parent)
95 {
96 list($template, $modifiers) = Helper::splitTemplate($template);
97 if ($modifiers != "")
98 $parent->setModifiers($modifiers);
99
100 $parsedTemplate = preg_split('/({=|})/', $template, -1, PREG_SPLIT_DELIM_CAPTURE);
101 while (($token = array_shift($parsedTemplate)) !== null)
102 {
103 $node = null;
104
105 if ($token === "{=")
106 {
107 $node = self::parseFormula($parsedTemplate);
108 }
109 elseif ($token !== "")
110 {
111 $node = new NodeText($token);
112 }
113
114 if ($node)
115 $parent->addChild($node);
116 }
117 return $parent;
118 }
119
129 protected static function parseFormula(array &$parsedTemplate)
130 {
131 $node = null;
132 if (($token = array_shift($parsedTemplate)) !== null)
133 {
134 if (preg_match("/^([a-zA-Z0-9_]+\\.[a-zA-Z0-9_.]+)\\s*\$/", $token, $match))
135 {
136 $node = new NodeEntityField($match[1]);
137 }
138 elseif (preg_match("/^([a-zA-Z0-9_]+)(.*)\$/", $token, $match))
139 {
140 $node = new NodeFunction($match[1]);
141 self::parseFunctionArguments($match[2], $parsedTemplate, $node);
142 }
143 }
144 //Eat up to the formula end
145 while (($token = array_shift($parsedTemplate)) !== null)
146 {
147 if ($token === "}")
148 break;
149 }
150 return $node;
151 }
152
165 protected static function parseFunctionArguments($token, array &$parsedTemplate, NodeFunction $function)
166 {
167 $token = ltrim($token, " \t\n\r");
168
169 if ($token !== "")
170 self::explodeFunctionArgument($token, $function);
171
172 while (($token = array_shift($parsedTemplate)) !== null)
173 {
174 if ($token === "}")
175 {
176 array_unshift($parsedTemplate, $token);
177 break;
178 }
179 elseif ($token === "{=")
180 {
181 $node = self::parseFormula($parsedTemplate);
182 if ($node)
183 $function->addParameter($node);
184 }
185 elseif ($token !== "")
186 {
187 self::explodeFunctionArgument($token, $function);
188 }
189 }
190 }
191
201 protected static function explodeFunctionArgument($token, NodeFunction $function)
202 {
203 if (preg_match_all("/
204 (
205 [a-zA-Z0-9_]+\\.[a-zA-Z0-9_.]+
206 |[0-9]+
207 |\"[^\"]*\"
208 )
209 /x", $token, $wordList)
210 )
211 {
212 foreach ($wordList[0] as $word)
213 {
214 if ($word !== "")
215 {
216 if (preg_match("/^([a-zA-Z0-9_]+\\.[a-zA-Z0-9_.]+)\\s*\$/", $word, $match))
217 {
218 $node = new NodeEntityField($match[1]);
219 }
220 else
221 {
222 $node = new NodeText(trim($word, '"'));
223 }
224 $function->addParameter($node);
225 }
226 }
227 }
228 }
229}
230
236abstract class NodeBase
237{
246 abstract public function process(Entity\Base $entity);
247}
248
255class NodeRoot extends NodeBase
256{
258 protected $children = array();
259 protected $modifiers = array();
260
268 public function addChild(NodeBase $child)
269 {
270 $this->children[] = $child;
271 }
272
280 public function setModifiers($modifiers)
281 {
282 $this->modifiers = array();
283 foreach(Helper::splitModifiers($modifiers) as $mod)
284 {
285 if ($mod == "l")
286 $modifierFunction = Functions\Fabric::createInstance("lower");
287 else
288 $modifierFunction = Functions\Fabric::createInstance("translit", array(
289 "replace_space" => mb_substr($mod, 1),
290 ));
291 $this->modifiers[] = $modifierFunction;
292 }
293 }
294
303 public function process(Entity\Base $entity)
304 {
305 $content = "";
307 foreach ($this->children as $child)
308 {
309 $childContent = $child->process($entity);
310 if (is_array($childContent))
311 $content .= implode(" ", $childContent);
312 else
313 $content .= $childContent;
314 }
316 foreach ($this->modifiers as $modifier)
317 {
318 $node = new NodeText($content);
319 $arguments = $modifier->onPrepareParameters($entity, array($node));
320 $content = $modifier->calculate($arguments);
321 }
322 return $content;
323 }
324}
325
332class NodeText extends NodeBase
333{
334 protected $content = "";
335
341 function __construct($content = "")
342 {
343 $this->content = $content;
344 }
345
353 public function process(Entity\Base $entity)
354 {
355 return $this->content;
356 }
357}
358
367{
368 protected $entityField = "";
369
377 {
378 $this->entityField = mb_strtolower($entityField);
379 }
380
390 public function process(Entity\Base $entity)
391 {
392 $entityObject = $entity;
393 $pathToField = explode(".", $this->entityField);
394 for ($i = 0, $c = count($pathToField)-1; $i < $c; $i++)
395 {
396 $entityObject = $entityObject->resolve($pathToField[$i]);
397 }
398 if ($entityObject)
399 return $entityObject->getField($pathToField[$c]);
400 else
401 return "";
402 }
403}
404
412{
413 protected $functionName = "";
414 protected $parameters = array();
415
421 public function __construct($functionName = "")
422 {
423 $this->functionName = mb_strtolower($functionName);
424 }
425
433 public function addParameter(NodeBase $parameter)
434 {
435 $this->parameters[] = $parameter;
436 }
437
447 public function process(Entity\Base $entity)
448 {
449 $functionObject = Functions\Fabric::createInstance($this->functionName);
450 if ($functionObject instanceof Functions\FunctionBase)
451 {
452 $arguments = $functionObject->onPrepareParameters($entity, $this->parameters);
453 return $functionObject->calculate($arguments);
454 }
455 else
456 {
457 return "";
458 }
459 }
460}
static explodeFunctionArgument($token, NodeFunction $function)
Definition engine.php:201
static parseFormula(array &$parsedTemplate)
Definition engine.php:129
static parseFunctionArguments($token, array &$parsedTemplate, NodeFunction $function)
Definition engine.php:165
static process(Entity\Base $entity, $template)
Definition engine.php:80
static parseTemplateTree($template, NodeRoot $parent)
Definition engine.php:94
static splitModifiers($modifiers)
Definition helper.php:42
static splitTemplate($template)
Definition helper.php:23
process(Entity\Base $entity)
addParameter(NodeBase $parameter)
Definition engine.php:433
process(Entity\Base $entity)
Definition engine.php:447
addChild(NodeBase $child)
Definition engine.php:268
process(Entity\Base $entity)
Definition engine.php:353