TYPO3 CMS  TYPO3_7-6
InArrayValidator.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
21 {
25  protected $supportedOptions = [
26  'element' => ['', 'The name of the element', 'string', true],
27  'errorMessage' => ['', 'The error message', 'array', true],
28  'array' => ['', 'The array values from the wizard configuration (array = test1,test2)', 'string', false],
29  'array.' => ['', 'The array values from the documented configuration', 'array', false],
30  'strict' => ['', 'Compare types', 'boolean', false],
31  'ignorecase' => ['', 'Ignore cases', 'boolean', false]
32  ];
33 
37  protected $charsetConverter;
38 
47  public function __construct(array $options)
48  {
49  parent::__construct($options);
50  if (!empty($this->options['ignorecase'])) {
51  $this->charsetConverter = GeneralUtility::makeInstance(CharsetConverter::class);
52  }
53  }
54 
60  const LOCALISATION_OBJECT_NAME = 'tx_form_system_validate_inarray';
61 
68  public function isValid($value)
69  {
70  if (empty($value)) {
71  return;
72  }
73 
79  if (is_string($value)) {
80  $value = [$value];
81  }
82 
95  $allowedOptionsArray = [];
96  if (!empty($this->options['array']) && is_string($this->options['array'])) {
97  $allowedOptionsArray = GeneralUtility::trimExplode(',', $this->options['array'], true);
98  } elseif (!empty($this->options['array.']) && is_array($this->options['array.'])) {
99  $allowedOptionsArray = $this->options['array.'];
100  }
101 
102  if (!empty($this->options['ignorecase'])) {
103  foreach ($value as &$incomingArrayValue) {
104  $incomingArrayValue = $this->charsetConverter->conv_case('utf-8', $incomingArrayValue, 'toLower');
105  }
106  foreach ($allowedOptionsArray as &$option) {
107  $option = $this->charsetConverter->conv_case('utf-8', $option, 'toLower');
108  }
109  }
110 
111  foreach ($value as $incomingArrayValue) {
112  if (!in_array($incomingArrayValue, $allowedOptionsArray, !empty($this->options['strict']))) {
113  $this->addError(
114  $this->renderMessage(
115  $this->options['errorMessage'][0],
116  $this->options['errorMessage'][1],
117  'error'
118  ),
119  1442002594
120  );
121  }
122  }
123  }
124 }
addError($message, $code, array $arguments=[], $title='')
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
renderMessage($message=null, $type=null, $messageType='message')