[FEATURE] Integrate preliminary PackageManager API
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Utility / FilterUtility.php
1 <?php
2 namespace TYPO3\CMS\Form\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2008-2013 Patrick Broens (patrick@patrickbroens.nl)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Static methods for filtering
29 *
30 * @author Patrick Broens <patrick@patrickbroens.nl>
31 */
32 class FilterUtility implements \TYPO3\CMS\Form\Filter\FilterInterface {
33
34 /**
35 * Array with filter objects to use
36 *
37 * @var array
38 */
39 protected $filters = array();
40
41 /**
42 * Constructor
43 * Adds the removeXSS filter by default
44 * Never remove these lines, otherwise the forms
45 * will be vulnerable for XSS attacks
46 */
47 public function __construct() {
48 $removeXssFilter = $this->makeFilter('removeXss');
49 $this->addFilter($removeXssFilter);
50 }
51
52 /**
53 * Add a filter object to the filter array
54 *
55 * @param \TYPO3\CMS\Form\Filter\FilterInterface $filter The filter
56 * @return \TYPO3\CMS\Form\Utility\FilterUtility
57 */
58 public function addFilter(\TYPO3\CMS\Form\Filter\FilterInterface $filter) {
59 $this->filters[] = $filter;
60 return $this;
61 }
62
63 /**
64 * Create a filter object according to class
65 * and sent some arguments
66 *
67 * @param string $class Name of the filter
68 * @param array $arguments Configuration of the filter
69 * @return \TYPO3\CMS\Form\Filter\FilterInterface The filter object
70 */
71 public function makeFilter($class, array $arguments = NULL) {
72 return self::createFilter($class, $arguments);
73 }
74
75 /**
76 * Go through all filters added to the array
77 *
78 * @param mixed $value
79 * @return mixed
80 */
81 public function filter($value) {
82 if (!empty($this->filters)) {
83 /** @var $filter \TYPO3\CMS\Form\Filter\FilterInterface */
84 foreach ($this->filters as $filter) {
85 $value = $filter->filter($value);
86 }
87 }
88 return $value;
89 }
90
91 /**
92 * Call filter through this class with automatic instantiation of filter
93 *
94 * @param string $class
95 * @param mixed $value
96 * @param array $arguments
97 * @return mixed
98 */
99 static public function get($class, $value, array $arguments = array()) {
100 return self::createFilter($class, $arguments)->filter($value);
101 }
102
103 /**
104 * Create a filter object according to class
105 * and sent some arguments
106 *
107 * @param string $class Name of the filter
108 * @param array $arguments Configuration of the filter
109 * @return \TYPO3\CMS\Form\Filter\FilterInterface The filter object
110 */
111 static public function createFilter($class, array $arguments = NULL) {
112 $class = strtolower((string) $class);
113 $className = 'TYPO3\\CMS\\Form\\Filter\\' . ucfirst($class) . 'Filter';
114 if (is_null($arguments)) {
115 $filter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($className);
116 } else {
117 $filter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($className, $arguments);
118 }
119 return $filter;
120 }
121
122 }