1<?php
2
3declare(strict_types=1);
4
5/*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18namespace TYPO3\CMS\Form\Mvc;
19
20use TYPO3\CMS\Core\Utility\GeneralUtility;
21use TYPO3\CMS\Extbase\Error\Result;
22use TYPO3\CMS\Extbase\Object\ObjectManager;
23use TYPO3\CMS\Extbase\Property\PropertyMapper;
24use TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration;
25use TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator;
26use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface;
27
28/**
29 * A processing Rule contains information for property mapping and validation.
30 *
31 * Scope: frontend
32 * **This class is NOT meant to be sub classed by developers.**
33 * @internal
34 */
35class ProcessingRule
36{
37
38    /**
39     * The target data type the data should be converted to
40     *
41     * @var string
42     */
43    protected $dataType;
44
45    /**
46     * @var \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration
47     */
48    protected $propertyMappingConfiguration;
49
50    /**
51     * @var \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator
52     */
53    protected $validator;
54
55    /**
56     * @var \TYPO3\CMS\Extbase\Error\Result
57     */
58    protected $processingMessages;
59
60    /**
61     * @var \TYPO3\CMS\Extbase\Property\PropertyMapper
62     */
63    protected $propertyMapper;
64
65    /**
66     * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration $propertyMappingConfiguration
67     * @internal
68     */
69    public function injectPropertyMappingConfiguration(PropertyMappingConfiguration $propertyMappingConfiguration)
70    {
71        $this->propertyMappingConfiguration = $propertyMappingConfiguration;
72    }
73
74    /**
75     * @param \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator $validator
76     * @internal
77     */
78    public function injectConjunctionValidator(ConjunctionValidator $validator)
79    {
80        $this->validator = $validator;
81    }
82
83    /**
84     * @param \TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper
85     * @internal
86     */
87    public function injectPropertyMapper(PropertyMapper $propertyMapper)
88    {
89        $this->propertyMapper = $propertyMapper;
90    }
91
92    /**
93     * Constructs this processing rule
94     * @internal
95     */
96    public function __construct()
97    {
98        $this->processingMessages = GeneralUtility::makeInstance(ObjectManager::class)
99            ->get(Result::class);
100    }
101
102    /**
103     * @return PropertyMappingConfiguration
104     * @internal
105     */
106    public function getPropertyMappingConfiguration(): PropertyMappingConfiguration
107    {
108        return $this->propertyMappingConfiguration;
109    }
110
111    /**
112     * @return string
113     * @internal
114     */
115    public function getDataType(): string
116    {
117        return $this->dataType;
118    }
119
120    /**
121     * @param string $dataType
122     * @internal
123     */
124    public function setDataType(string $dataType)
125    {
126        $this->dataType = $dataType;
127    }
128
129    /**
130     * Returns the child validators of the ConjunctionValidator that is bound to this processing rule
131     *
132     * @return \SplObjectStorage
133     * @internal
134     */
135    public function getValidators(): \SplObjectStorage
136    {
137        return $this->validator->getValidators();
138    }
139
140    /**
141     * @param ValidatorInterface $validator
142     * @internal
143     */
144    public function addValidator(ValidatorInterface $validator)
145    {
146        $this->validator->addValidator($validator);
147    }
148
149    /**
150     * Removes the specified validator.
151     *
152     * @param ValidatorInterface $validator The validator to remove
153     * @throws \TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException
154     * @internal
155     */
156    public function removeValidator(ValidatorInterface $validator)
157    {
158        $this->validator->removeValidator($validator);
159    }
160
161    /**
162     * @param mixed $value
163     * @return mixed
164     * @internal
165     */
166    public function process($value)
167    {
168        if ($this->dataType !== null) {
169            $value = $this->propertyMapper->convert($value, $this->dataType, $this->propertyMappingConfiguration);
170            $messages = $this->propertyMapper->getMessages();
171        } else {
172            $messages = GeneralUtility::makeInstance(ObjectManager::class)
173                ->get(Result::class);
174        }
175
176        $validationResult = $this->validator->validate($value);
177        $messages->merge($validationResult);
178
179        $this->processingMessages->merge($messages);
180        return $value;
181    }
182
183    /**
184     * @return Result
185     * @internal
186     */
187    public function getProcessingMessages(): Result
188    {
189        return $this->processingMessages;
190    }
191}
192