Passed
Push — master ( 208bf9...74b704 )
by
unknown
14:05
created

injectPropertyMappingConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(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
18
namespace TYPO3\CMS\Form\Mvc;
19
20
use TYPO3\CMS\Core\Utility\GeneralUtility;
21
use TYPO3\CMS\Extbase\Error\Result;
22
use TYPO3\CMS\Extbase\Property\PropertyMapper;
23
use TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration;
24
use TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator;
25
use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface;
26
27
/**
28
 * A processing Rule contains information for property mapping and validation.
29
 *
30
 * Scope: frontend
31
 * **This class is NOT meant to be sub classed by developers.**
32
 * @internal
33
 */
34
class ProcessingRule
35
{
36
37
    /**
38
     * The target data type the data should be converted to
39
     *
40
     * @var string
41
     */
42
    protected $dataType;
43
44
    protected PropertyMappingConfiguration $propertyMappingConfiguration;
45
    protected ConjunctionValidator $validator;
46
    protected Result $processingMessages;
47
    protected PropertyMapper $propertyMapper;
48
49
    /**
50
     * Constructs this processing rule
51
     * @internal
52
     */
53
    public function __construct(PropertyMapper $propertyMapper)
54
    {
55
        $this->propertyMapper = $propertyMapper;
56
        $this->propertyMappingConfiguration = GeneralUtility::makeInstance(PropertyMappingConfiguration::class);
57
        $this->validator = GeneralUtility::makeInstance(ConjunctionValidator::class);
58
        $this->processingMessages = GeneralUtility::makeInstance(Result::class);
59
    }
60
61
    /**
62
     * @return PropertyMappingConfiguration
63
     * @internal
64
     */
65
    public function getPropertyMappingConfiguration(): PropertyMappingConfiguration
66
    {
67
        return $this->propertyMappingConfiguration;
68
    }
69
70
    /**
71
     * @return string
72
     * @internal
73
     */
74
    public function getDataType(): string
75
    {
76
        return $this->dataType;
77
    }
78
79
    /**
80
     * @param string $dataType
81
     * @internal
82
     */
83
    public function setDataType(string $dataType)
84
    {
85
        $this->dataType = $dataType;
86
    }
87
88
    /**
89
     * Returns the child validators of the ConjunctionValidator that is bound to this processing rule
90
     *
91
     * @return \SplObjectStorage
92
     * @internal
93
     */
94
    public function getValidators(): \SplObjectStorage
95
    {
96
        return $this->validator->getValidators();
97
    }
98
99
    /**
100
     * @param ValidatorInterface $validator
101
     * @internal
102
     */
103
    public function addValidator(ValidatorInterface $validator)
104
    {
105
        $this->validator->addValidator($validator);
106
    }
107
108
    /**
109
     * Removes the specified validator.
110
     *
111
     * @param ValidatorInterface $validator The validator to remove
112
     * @throws \TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException
113
     * @internal
114
     */
115
    public function removeValidator(ValidatorInterface $validator)
116
    {
117
        $this->validator->removeValidator($validator);
118
    }
119
120
    /**
121
     * @param mixed $value
122
     * @return mixed
123
     * @internal
124
     */
125
    public function process($value)
126
    {
127
        if ($this->dataType !== null) {
128
            $value = $this->propertyMapper->convert($value, $this->dataType, $this->propertyMappingConfiguration);
129
            $messages = $this->propertyMapper->getMessages();
130
            $this->propertyMapper->resetMessages();
131
        } else {
132
            $messages = GeneralUtility::makeInstance(Result::class);
133
        }
134
135
        $validationResult = $this->validator->validate($value);
136
        $messages->merge($validationResult);
137
138
        $this->processingMessages->merge($messages);
139
        return $value;
140
    }
141
142
    /**
143
     * @return Result
144
     * @internal
145
     */
146
    public function getProcessingMessages(): Result
147
    {
148
        return $this->processingMessages;
149
    }
150
}
151