Skip to content

Commit

Permalink
Merge pull request FluidTYPO3#320 from NamelessCoder/regularexpression
Browse files Browse the repository at this point in the history
[FEATURE] Format / RegularExpression ViewHelper
  • Loading branch information
cedricziel committed Sep 12, 2013
2 parents e2bd6e2 + ad21895 commit 67cd6a8
Show file tree
Hide file tree
Showing 3 changed files with 274 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Classes/ViewHelpers/Format/RegularExpressionViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/***************************************************************
* Copyright notice
*
* (c) 2013 Claus Due <claus@wildside.dk>, Wildside A/S
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/**
* Formats or gets detected substrings by regular expression.
* Returns matches if `return="TRUE"`, otherwise replaces any
* occurrences of `$pattern`.
* simply returns
*
* @author Claus Due <claus@wildside.dk>, Wildside A/S
* @package Vhs
* @subpackage ViewHelpers\Format
*/
class Tx_Vhs_ViewHelpers_Format_RegularExpressionViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {

/**
* @param string $pattern The regular expression pattern to search for
* @param string $replacement The desired value to insert instead of detected matches
* @param string $subject The subject in which to perform replacements/detection; taken from tag content if not specified.
* @param boolean $return
* @return mixed
*/
public function render($pattern, $replacement, $subject = NULL, $return = FALSE) {
if (NULL === $subject) {
$subject = $this->renderChildren();
}
if (TRUE === $return) {
$returnValue = array();
preg_match($pattern, $subject, $returnValue);
} else {
$returnValue = preg_replace($pattern, $replacement, $subject);
}
return $returnValue;
}

}
141 changes: 141 additions & 0 deletions Tests/Unit/ViewHelpers/AbstractViewHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
/***************************************************************
* Copyright notice
*
* (c) 2013 Claus Due <claus@wildside.dk>
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

/**
* @author Claus Due <claus@wildside.dk>
* @package Vhs
*/
abstract class Tx_Vhs_ViewHelpers_AbstractViewHelperTest extends Tx_Extbase_Tests_Unit_BaseTestCase {

/**
* @return string
*/
protected function getViewHelperClassName() {
$class = get_class($this);
return substr($class, 0, -4);
}

/**
* @param string $type
* @param mixed $value
* @return Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface
*/
protected function createNode($type, $value) {
/** @var Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface $node */
$className = 'Tx_Fluid_Core_Parser_SyntaxTree_' . $type . 'Node';
$node = new $className($value);
return $node;
}

/**
* @return Tx_Fluid_Core_ViewHelper_AbstractViewHelper
*/
protected function createInstance() {
$className = $this->getViewHelperClassName();
/** @var Tx_Fluid_Core_ViewHelper_AbstractViewHelper $instance */
$instance = $this->objectManager->get($className);
if (TRUE === method_exists($instance, 'injectConfigurationManager')) {
$cObject = new tslib_cObj();
$cObject->start(Tx_Flux_Tests_Fixtures_Data_Records::$contentRecordWithoutParentAndWithoutChildren, 'tt_content');
/** @var Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager */
$configurationManager = $this->objectManager->get('Tx_Extbase_Configuration_ConfigurationManagerInterface');
$configurationManager->setContentObject($cObject);
$instance->injectConfigurationManager($configurationManager);
}
return $instance;
}

/**
* @param array $arguments
* @param array $variables
* @param Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface $childNode
* @param string $extensionName
* @param string $pluginName
* @return Tx_Fluid_Core_ViewHelper_AbstractViewHelper
*/
protected function buildViewHelperInstance($arguments = array(), $variables = array(), $childNode = NULL, $extensionName = NULL, $pluginName = NULL) {
$instance = $this->createInstance();
/** @var Tx_Fluid_Core_ViewHelper_TemplateVariableContainer $container */
$container = $this->objectManager->get('Tx_Fluid_Core_ViewHelper_TemplateVariableContainer');
/** @var Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer $viewHelperContainer */
$viewHelperContainer = $this->objectManager->get('Tx_Fluid_Core_ViewHelper_ViewHelperVariableContainer');
foreach ($variables as $name => $value) {
$container->add($name, $value);
}
$node = new Tx_Fluid_Core_Parser_SyntaxTree_ViewHelperNode($instance, $arguments);
/** @var Tx_Extbase_MVC_Web_Routing_UriBuilder $uriBuilder */
$uriBuilder = $this->objectManager->get('Tx_Extbase_MVC_Web_Routing_UriBuilder');
/** @var Tx_Extbase_Mvc_Web_Request $request */
$request = $this->objectManager->get('Tx_Extbase_Mvc_Web_Request');
if (NULL !== $extensionName) {
$request->setControllerExtensionName($extensionName);
}
if (NULL !== $pluginName) {
$request->setPluginName($pluginName);
}
/** @var Tx_Extbase_Mvc_Web_Response $response */
$response = $this->objectManager->get('Tx_Extbase_Mvc_Web_Response');
/** @var Tx_Extbase_MVC_Controller_ControllerContext $controllerContext */
$controllerContext = $this->objectManager->get('Tx_Extbase_MVC_Controller_ControllerContext');
$controllerContext->setRequest($request);
$controllerContext->setResponse($response);
$controllerContext->setUriBuilder($uriBuilder);
/** @var Tx_Fluid_Core_Rendering_RenderingContext $renderingContext */
$renderingContext = $this->objectManager->get('Tx_Fluid_Core_Rendering_RenderingContext');
$renderingContext->setControllerContext($controllerContext);
$renderingContext->injectViewHelperVariableContainer($viewHelperContainer);
$renderingContext->injectTemplateVariableContainer($container);
$instance->setArguments($arguments);
$instance->setRenderingContext($renderingContext);
if (TRUE === $instance instanceof Tx_Fluidwidget_Core_Widget_AbstractWidgetViewHelper) {
/** @var Tx_Fluid_Core_Widget_WidgetContext $widgetContext */
$widgetContext = $this->objectManager->get('Tx_Fluid_Core_Widget_WidgetContext');
Tx_Extbase_Reflection_ObjectAccess::setProperty($instance, 'widgetContext', $widgetContext, TRUE);
}
if (NULL !== $childNode) {
$node->addChildNode($childNode);
if ($instance instanceof Tx_Fluid_Core_ViewHelper_Facets_ChildNodeAccessInterface) {
$instance->setChildNodes(array($childNode));
}
}
$instance->setViewHelperNode($node);
return $instance;
}

/**
* @param array $arguments
* @param array $variables
* @param Tx_Fluid_Core_Parser_SyntaxTree_NodeInterface $childNode
* @param string $extensionName
* @param string $pluginName
* @return mixed|Tx_Fluid_Core_ViewHelper_AbstractViewHelper
*/
protected function executeViewHelper($arguments = array(), $variables = array(), $childNode = NULL, $extensionName = NULL, $pluginName = NULL) {
$instance = $this->buildViewHelperInstance($arguments, $variables, $childNode, $extensionName, $pluginName);
$output = $instance->initializeArgumentsAndRender();
return $output;
}

}
75 changes: 75 additions & 0 deletions Tests/Unit/ViewHelpers/Format/RegularExpressionViewHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/***************************************************************
* Copyright notice
*
* (c) 2013 Claus Due <claus@wildside.dk>
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

/**
* @protection off
* @author Claus Due <claus@wildside.dk>
* @package Vhs
*/
class Tx_Vhs_ViewHelpers_Format_RegularExpressionViewHelperTest extends Tx_Vhs_ViewHelpers_AbstractViewHelperTest {

/**
* @test
*/
public function canReplaceValues() {
$arguments = array(
'subject' => 'foo123bar',
'return' => FALSE,
'pattern' => '/[0-9]{3}/',
'replacement' => 'baz',
);
$test = $this->executeViewHelper($arguments);
$this->assertSame('foobazbar', $test);
}

/**
* @test
*/
public function canReturnMatches() {
$arguments = array(
'subject' => 'foo123bar',
'return' => TRUE,
'pattern' => '/[0-9]{3}/',
'replacement' => 'baz',
);
$test = $this->executeViewHelper($arguments);
$this->assertSame(array('123'), $test);
}

/**
* @test
*/
public function canTakeSubjectFromRenderChildren() {
$arguments = array(
'return' => TRUE,
'pattern' => '/[0-9]{3}/',
'replacement' => 'baz',
);
$node = $this->createNode('Text', 'foo123bar');
$test = $this->executeViewHelper($arguments, array(), $node);
$this->assertSame(array('123'), $test);
}

}

0 comments on commit 67cd6a8

Please sign in to comment.