* Added format.number, format.printf and format.htmlescape view helpers with tests
authorChristopher Hlubek <hlubek@networkteam.com>
Sun, 5 Apr 2009 21:03:57 +0000 (21:03 +0000)
committerChristopher Hlubek <hlubek@networkteam.com>
Sun, 5 Apr 2009 21:03:57 +0000 (21:03 +0000)
typo3/sysext/fluid/Classes/ViewHelpers/Format/HtmlEscapeViewHelper.php [new file with mode: 0644]
typo3/sysext/fluid/Classes/ViewHelpers/Format/NumberViewHelper.php [new file with mode: 0644]
typo3/sysext/fluid/Classes/ViewHelpers/Format/PrintfViewHelper.php [new file with mode: 0644]
typo3/sysext/fluid/Tests/ViewHelpers/Format/HtmlEscapeViewHelper_testcase.php [new file with mode: 0644]
typo3/sysext/fluid/Tests/ViewHelpers/Format/NumberViewHelper_testcase.php [new file with mode: 0644]
typo3/sysext/fluid/Tests/ViewHelpers/Format/PrintfViewHelper_testcase.php [new file with mode: 0644]

diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/Format/HtmlEscapeViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/Format/HtmlEscapeViewHelper.php
new file mode 100644 (file)
index 0000000..e4fe3f3
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 2 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
+ * Public License for more details.                                       *
+ *                                                                        */
+
+/**
+ * @package Fluid
+ * @subpackage ViewHelpers
+ * @version $Id$
+ */
+
+/**
+ * A view helper for escaping HTML. Any HTML character in the body of this tag will
+ * be escaped to an HTML entity.
+ *
+ * Example:
+ * <f:format.htmlEscape><p>This will be <em>escaped</em></p></f:format.htmlEscape>
+ *
+ * Output:
+ * &lt;p&gt;This will be &lt;em&gt;escaped&lt;/em&gt;&lt;/p&gt;
+ *
+ * @package
+ * @subpackage
+ * @version $Id:$
+ */
+class Tx_Fluid_ViewHelpers_Format_HtmlEscapeViewHelper extends Tx_Fluid_Core_AbstractViewHelper {
+       /**
+        * HTML escape the content of this tag.
+        *
+        * @return string The HTML escaped body.
+        * @author Christopher Hlubek <hlubek@networkteam.com>
+        */
+       public function render() {
+               $value = $this->renderChildren();
+               return htmlspecialchars($value);
+       }
+}
+
+
+?>
\ No newline at end of file
diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/Format/NumberViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/Format/NumberViewHelper.php
new file mode 100644 (file)
index 0000000..57aaa7d
--- /dev/null
@@ -0,0 +1,63 @@
+<?php
+
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 2 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
+ * Public License for more details.                                       *
+ *                                                                        */
+
+/**
+ * @package Fluid
+ * @subpackage ViewHelpers
+ * @version $Id$
+ */
+
+/**
+ * Formats a number with custom precision, decimal point and grouped thousands.
+ *
+ * Example:
+ *
+ * (1) default parameters:
+ * <f:format.number value="423423.234" />
+ *
+ * Output:
+ * 423,423.20
+ *
+ * (2) with all parameters
+ * <f:format.number value="423423.234" decimals="1" decimalPoint="," thousandsSeparator="." />
+ *
+ * Output:
+ * 423.423,2
+ *
+ * @see http://www.php.net/manual/en/function.number-format.php
+ *
+ * @package
+ * @subpackage
+ * @version $Id:$
+ */
+class Tx_Fluid_ViewHelpers_Format_NumberViewHelper extends Tx_Fluid_Core_AbstractViewHelper {
+       /**
+        * Format the numeric value as a number with grouped thousands, decimal point and
+        * precision.
+        *
+        * @param numeric $value The value to format
+        * @param int $decimals The number of digits after the decimal point
+        * @param string $decimalPoint The decimal point character
+        * @param string $thousandsSeparator The character for grouping the thousand digits
+        * @return string The formatted number
+        * @author Christopher Hlubek <hlubek@networkteam.com>
+        */
+       public function render($value, $decimals = 2, $decimalPoint = '.', $thousandsSeparator = ',') {
+               return number_format($value, $decimals, $decimalPoint, $thousandsSeparator);
+       }
+}
+
+
+?>
\ No newline at end of file
diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/Format/PrintfViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/Format/PrintfViewHelper.php
new file mode 100644 (file)
index 0000000..b9026ef
--- /dev/null
@@ -0,0 +1,53 @@
+<?php
+
+/*                                                                        *
+ * This script is part of the TYPO3 project - inspiring people to share!  *
+ *                                                                        *
+ * TYPO3 is free software; you can redistribute it and/or modify it under *
+ * the terms of the GNU General Public License version 2 as published by  *
+ * the Free Software Foundation.                                          *
+ *                                                                        *
+ * This script is distributed in the hope that it will be useful, but     *
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
+ * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
+ * Public License for more details.                                       *
+ *                                                                        */
+
+/**
+ * @package Fluid
+ * @subpackage ViewHelpers
+ * @version $Id$
+ */
+
+/**
+ * A view helper for formatting values with printf. Either supply an array for
+ * the arguments or a single value.
+ *
+ * Example:
+ * <f:format.printf format="%.3e" arguments="{number : 362525200}" />
+ *
+ * Output:
+ * 3.625e+8
+ *
+ * @see http://www.php.net/manual/en/function.sprintf.php
+ *
+ * @package
+ * @subpackage
+ * @version $Id:$
+ */
+class Tx_Fluid_ViewHelpers_Format_PrintfViewHelper extends Tx_Fluid_Core_AbstractViewHelper {
+       /**
+        * Format the arguments with the given printf format string.
+        *
+        * @param string $format The printf format string
+        * @param array|string $arguments The arguments for printf
+        * @return string The formatted value
+        * @author Christopher Hlubek <hlubek@networkteam.com>
+        */
+       public function render($format, $arguments) {
+               return vsprintf($format, $arguments);
+       }
+}
+
+
+?>
\ No newline at end of file
diff --git a/typo3/sysext/fluid/Tests/ViewHelpers/Format/HtmlEscapeViewHelper_testcase.php b/typo3/sysext/fluid/Tests/ViewHelpers/Format/HtmlEscapeViewHelper_testcase.php
new file mode 100644 (file)
index 0000000..5371294
--- /dev/null
@@ -0,0 +1,37 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2009 Christopher Hlubek <hlubek@networkteam.com>
+*  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!
+***************************************************************/
+
+require_once(t3lib_extMgm::extPath('extbase', 'Tests/Base_testcase.php'));
+
+class Tx_Fluid_ViewHelpers_Format_HtmlEscapeViewHelper_testcase extends Tx_ExtBase_Base_testcase {
+       public function test_ViewHelperEscapesHTMLInBody() {
+               $helper = $this->getMock('Tx_Fluid_ViewHelpers_Format_HtmlEscapeViewHelper', array('renderChildren'));
+               $helper->expects($this->once())
+                       ->method('renderChildren')
+                       ->will($this->returnValue('<p class="bodytext">Test</p>'));
+               $result = $helper->render();
+               $this->assertEquals('&lt;p class=&quot;bodytext&quot;&gt;Test&lt;/p&gt;', $result);
+       }
+}
+?>
\ No newline at end of file
diff --git a/typo3/sysext/fluid/Tests/ViewHelpers/Format/NumberViewHelper_testcase.php b/typo3/sysext/fluid/Tests/ViewHelpers/Format/NumberViewHelper_testcase.php
new file mode 100644 (file)
index 0000000..b611f29
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2009 Christopher Hlubek <hlubek@networkteam.com>
+*  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!
+***************************************************************/
+
+require_once(t3lib_extMgm::extPath('extbase', 'Tests/Base_testcase.php'));
+
+class Tx_Fluid_ViewHelpers_Format_NumberViewHelper_testcase extends Tx_ExtBase_Base_testcase {
+       public function test_FormatNumberDefaultsToEnglishNotationWithTwoDecimals() {
+               $helper = new Tx_Fluid_ViewHelpers_Format_NumberViewHelper();
+               $result = $helper->render(10000.0 / 3.0);
+               $this->assertEquals('3,333.33', $result);
+       }
+
+       public function test_FormatNumberWithDecimalsDecimalPointAndSeparator() {
+               $helper = new Tx_Fluid_ViewHelpers_Format_NumberViewHelper();
+               $result = $helper->render(10000.0 / 3.0, 3, ',', '.');
+               $this->assertEquals('3.333,333', $result);
+       }
+}
+?>
\ No newline at end of file
diff --git a/typo3/sysext/fluid/Tests/ViewHelpers/Format/PrintfViewHelper_testcase.php b/typo3/sysext/fluid/Tests/ViewHelpers/Format/PrintfViewHelper_testcase.php
new file mode 100644 (file)
index 0000000..37128d1
--- /dev/null
@@ -0,0 +1,40 @@
+<?php
+/***************************************************************
+*  Copyright notice
+*
+*  (c) 2009 Christopher Hlubek <hlubek@networkteam.com>
+*  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!
+***************************************************************/
+
+require_once(t3lib_extMgm::extPath('extbase', 'Tests/Base_testcase.php'));
+
+class Tx_Fluid_ViewHelpers_Format_PrintfViewHelper_testcase extends Tx_ExtBase_Base_testcase {
+       public function test_PrintfCanUseArrayAsArgument() {
+               $helper = new Tx_Fluid_ViewHelpers_Format_PrintfViewHelper();
+               $result = $helper->render('%04d-%02d-%02d', array('year' => 2009, 'month' => 4, 'day' => 5));
+               $this->assertEquals('2009-04-05', $result);
+       }
+
+       public function test_PrintfCanUseStringAsArgument() {
+               $helper = new Tx_Fluid_ViewHelpers_Format_PrintfViewHelper();
+               $result = $helper->render('%.3e', '362525200');
+               $this->assertEquals('3.625e+8', $result);
+       }       
+}
+?>
\ No newline at end of file