Skip to content

Commit ad535b1

Browse files
author
Oleksii Korshenko
committed
MAGETWO-70866: Enabling the use of looping (for in ..) into Template.php #9401
- fixed code style
1 parent 7aef080 commit ad535b1

File tree

1 file changed

+50
-40
lines changed

1 file changed

+50
-40
lines changed

lib/internal/Magento/Framework/Filter/Template.php

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ protected function filterFor($value)
176176
{
177177
if (preg_match_all(self::LOOP_PATTERN, $value, $constructions, PREG_SET_ORDER)) {
178178
foreach ($constructions as $construction) {
179-
180179
if (!$this->isValidLoop($construction)) {
181180
return $value;
182181
}
@@ -188,45 +187,7 @@ protected function filterFor($value)
188187
$loopItemVariableName = preg_replace('/\s+/', '', $construction['loopItem']);
189188

190189
if (is_array($loopData) || $loopData instanceof \Traversable) {
191-
192-
$loopText = [];
193-
$loopIndex = 0;
194-
$loopDataObject = new \Magento\Framework\DataObject();
195-
196-
foreach ($loopData as $loopItemDataObject) {
197-
// Loop item can be an array or DataObject.
198-
// If loop item is an array, convert it to DataObject
199-
// to have unified interface if the collection
200-
if (!$loopItemDataObject instanceof \Magento\Framework\DataObject) {
201-
if (!is_array($loopItemDataObject)) {
202-
continue;
203-
}
204-
$loopItemDataObject = new \Magento\Framework\DataObject($loopItemDataObject);
205-
}
206-
207-
$loopDataObject->setData('index', $loopIndex++);
208-
$this->templateVars['loop'] = $loopDataObject;
209-
$this->templateVars[$loopItemVariableName] = $loopItemDataObject;
210-
211-
if (preg_match_all(
212-
self::CONSTRUCTION_PATTERN,
213-
$loopTextToReplace,
214-
$attributes,
215-
PREG_SET_ORDER
216-
)
217-
) {
218-
219-
$subText = $loopTextToReplace;
220-
foreach ($attributes as $attribute) {
221-
$text = $this->getVariable($attribute[2], '');
222-
$subText = str_replace($attribute[0], $text, $subText);
223-
}
224-
$loopText[] = $subText;
225-
}
226-
unset($this->templateVars[$loopItemVariableName]);
227-
228-
}
229-
$replaceText = implode('', $loopText);
190+
$replaceText = $this->getLoopReplacementText($loopData, $loopItemVariableName, $loopTextToReplace);
230191
$value = str_replace($fullTextToReplace, $replaceText, $value);
231192
}
232193
}
@@ -474,4 +435,53 @@ protected function getStackArgs($stack)
474435
}
475436
return $stack;
476437
}
438+
439+
/**
440+
* Process loop text to replace.
441+
*
442+
* @param array $loopData
443+
* @param string $loopItemVariableName
444+
* @param string $loopTextToReplace
445+
* @return string
446+
*/
447+
private function getLoopReplacementText(array $loopData, $loopItemVariableName, $loopTextToReplace)
448+
{
449+
$loopText = [];
450+
$loopIndex = 0;
451+
$loopDataObject = new \Magento\Framework\DataObject();
452+
453+
foreach ($loopData as $loopItemDataObject) {
454+
// Loop item can be an array or DataObject.
455+
// If loop item is an array, convert it to DataObject
456+
// to have unified interface if the collection
457+
if (!$loopItemDataObject instanceof \Magento\Framework\DataObject) {
458+
if (!is_array($loopItemDataObject)) {
459+
continue;
460+
}
461+
$loopItemDataObject = new \Magento\Framework\DataObject($loopItemDataObject);
462+
}
463+
464+
$loopDataObject->setData('index', $loopIndex++);
465+
$this->templateVars['loop'] = $loopDataObject;
466+
$this->templateVars[$loopItemVariableName] = $loopItemDataObject;
467+
468+
if (preg_match_all(
469+
self::CONSTRUCTION_PATTERN,
470+
$loopTextToReplace,
471+
$attributes,
472+
PREG_SET_ORDER
473+
)
474+
) {
475+
$subText = $loopTextToReplace;
476+
foreach ($attributes as $attribute) {
477+
$text = $this->getVariable($attribute[2], '');
478+
$subText = str_replace($attribute[0], $text, $subText);
479+
}
480+
$loopText[] = $subText;
481+
}
482+
unset($this->templateVars[$loopItemVariableName]);
483+
}
484+
$replaceText = implode('', $loopText);
485+
return $replaceText;
486+
}
477487
}

0 commit comments

Comments
 (0)