[FEATURE] stdWrap replacement: add optionSplit-support 98/15898/6
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Mon, 22 Oct 2012 23:28:34 +0000 (01:28 +0200)
committerStefan Neufeind <typo3.neufeind@speedpartner.de>
Mon, 29 Jul 2013 22:42:30 +0000 (00:42 +0200)
With optionSplit the "replace"-parameter can be
different depending on the occurence of the string
(first/middle/last-part, ...).

Change-Id: Ic71adfbc05e6c963098d47847475f731eb577af2
Resolves: #42287
Releases: 6.2
Reviewed-on: https://review.typo3.org/15898
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Stefan Neufeind
Tested-by: Stefan Neufeind
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index 3efe744..d8203bc 100644 (file)
@@ -4476,10 +4476,15 @@ class ContentObjectRenderer {
                        // Gets the strings
                        $search = isset($configuration['search.']) ? $this->stdWrap($configuration['search'], $configuration['search.']) : $configuration['search'];
                        $replace = isset($configuration['replace.']) ? $this->stdWrap($configuration['replace'], $configuration['replace.']) : $configuration['replace'];
-                       // Determines whether regular expression shall be used:
+                       // Determines whether regular expression shall be used
                        if (isset($configuration['useRegExp']) || $configuration['useRegExp.']) {
                                $useRegularExpression = isset($configuration['useRegExp.']) ? $this->stdWrap($configuration['useRegExp'], $configuration['useRegExp.']) : $configuration['useRegExp'];
                        }
+                       // Determines whether replace-pattern uses option-split
+                       if (isset($configuration['useOptionSplitReplace']) || isset($configuration['useOptionSplitReplace.'])) {
+                               $useOptionSplitReplace = isset($configuration['useOptionSplitReplace.']) ? $this->stdWrap($configuration['useOptionSplitReplace'], $configuration['useOptionSplitReplace.']) : $configuration['useOptionSplitReplace'];
+                       }
+
                        // Performs a replacement by preg_replace()
                        if (isset($useRegularExpression)) {
                                // Get separator-character which precedes the string and separates search-string from the modifiers
@@ -4491,9 +4496,38 @@ class ContentObjectRenderer {
                                        $modifiers = str_replace('e', '', $modifiers);
                                        $search = substr($search, 0, ($startModifiers + 1)) . $modifiers;
                                }
-                               $content = preg_replace($search, $replace, $content);
+                               if (empty($useOptionSplitReplace)) {
+                                       $content = preg_replace($search, $replace, $content);
+                               } else {
+                                       // init for replacement
+                                       $splitCount = preg_match_all($search, $content, $matches);
+                                       $replaceArray = $GLOBALS['TSFE']->tmpl->splitConfArray(array($replace), $splitCount);
+                                       $replaceCount = 0;
+
+                                       $replaceCallback = function($match) use ($replaceArray, $search, &$replaceCount) {
+                                               $replaceCount++;
+                                               return preg_replace($search, $replaceArray[$replaceCount - 1][0], $match[0]);
+                                       };
+                                       $content = preg_replace_callback($search, $replaceCallback, $content);
+                               }
                        } else {
-                               $content = str_replace($search, $replace, $content);
+                               if (empty($useOptionSplitReplace)) {
+                                       $content = str_replace($search, $replace, $content);
+                               } else {
+                                       // turn search-string into a preg-pattern
+                                       $searchPreg = '#' . preg_quote($search, '#') . '#';
+
+                                       // init for replacement
+                                       $splitCount = preg_match_all($searchPreg, $content, $matches);
+                                       $replaceArray = $GLOBALS['TSFE']->tmpl->splitConfArray(array($replace), $splitCount);
+                                       $replaceCount = 0;
+
+                                       $replaceCallback = function($match) use ($replaceArray, $search, &$replaceCount) {
+                                               $replaceCount++;
+                                               return $replaceArray[$replaceCount - 1][0];
+                                       };
+                                       $content = preg_replace_callback($searchPreg, $replaceCallback, $content);
+                               }
                        }
                }
                return $content;
index 8fef6a5..142f8ed 100644 (file)
@@ -1060,7 +1060,34 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                                        )
                                ),
                                'There is an animal, an animal and an animal around the block! Yeah!'
-                       )
+                       ),
+                       'replacement with optionSplit, normal pattern' => array(
+                               'There_is_a_cat,_a_dog_and_a_tiger_in_da_hood!_Yeah!',
+                               array(
+                                       'replacement.' => array(
+                                               '10.' => array(
+                                                       'search' => '_',
+                                                       'replace' => '1 || 2 || 3',
+                                                       'useOptionSplitReplace' => '1'
+                                               ),
+                                       )
+                               ),
+                               'There1is2a3cat,3a3dog3and3a3tiger3in3da3hood!3Yeah!'
+                       ),
+                       'replacement with optionSplit, using regex' => array(
+                               'There is a cat, a dog and a tiger in da hood! Yeah!',
+                               array(
+                                       'replacement.' => array(
+                                               '10.' => array(
+                                                       'search' => '#(a) (Cat|Dog|Tiger)#i',
+                                                       'replace' => '${1} tiny ${2} || ${1} midsized ${2} || ${1} big ${2}',
+                                                       'useOptionSplitReplace' => '1',
+                                                       'useRegExp' => '1'
+                                               )
+                                       )
+                               ),
+                               'There is a tiny cat, a midsized dog and a big tiger in da hood! Yeah!'
+                       ),
                );
                return $data;
        }
@@ -1821,4 +1848,4 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        }
 }
 
-?>
\ No newline at end of file
+?>