Completed
Push — development ( d0ebd5...329660 )
by Romain
10s
created

ValidatorResolver::injectReflectionService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * 2017 Romain CANON <[email protected]>
4
 *
5
 * This file is part of the TYPO3 Configuration Object project.
6
 * It is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU General Public License, either
8
 * version 3 of the License, or any later version.
9
 *
10
 * For the full copyright and license information, see:
11
 * http://www.gnu.org/licenses/gpl-3.0.html
12
 */
13
14
namespace Romm\ConfigurationObject\Validation;
15
16
use Romm\ConfigurationObject\Core\Core;
17
use Romm\ConfigurationObject\Reflection\ReflectionService;
18
use Romm\ConfigurationObject\Service\Items\MixedTypes\MixedTypesInterface;
19
use Romm\ConfigurationObject\Validation\Validator\Internal\MixedTypeCollectionValidator;
20
use Romm\ConfigurationObject\Validation\Validator\Internal\MixedTypeObjectValidator;
21
use TYPO3\CMS\Extbase\Reflection\ReflectionService as ExtbaseReflectionService;
22
use TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator;
23
use TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator;
24
25
/**
26
 * Customized validator resolver, which it mostly used to support the mixed
27
 * types.
28
 *
29
 * When an instance of validator is created, we check if the type of this
30
 * validator is `CollectionValidator`: in this case we use a custom one instead:
31
 * `MixedTypeCollectionValidator` which will support the mixed types feature.
32
 */
33
class ValidatorResolver extends \TYPO3\CMS\Extbase\Validation\ValidatorResolver
34
{
35
36
    /**
37
     * @var array
38
     */
39
    protected $baseValidatorConjunctionsWithChecks = [];
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function createValidator($validatorType, array $validatorOptions = [])
45
    {
46
        if (CollectionValidator::class === $validatorType) {
47
            $result = parent::createValidator(MixedTypeCollectionValidator::class);
48
        } else {
49
            $result = parent::createValidator($validatorType, $validatorOptions);
50
        }
51
52
        return $result;
53
    }
54
55
    /**
56
     * If the given class implements the interface `MixedTypesInterface`, a
57
     * custom conjunction validator is used instead of the default one (from the
58
     * parent class).
59
     *
60
     * @param string $targetClassName
61
     * @return ConjunctionValidator
62
     */
63
    public function getBaseValidatorConjunctionWithMixedTypesCheck($targetClassName)
64
    {
65
        if (false === array_key_exists($targetClassName, $this->baseValidatorConjunctionsWithChecks)) {
66
            $this->baseValidatorConjunctionsWithChecks[$targetClassName] = $this->buildBaseValidatorConjunctionWithMixedTypesCheck($targetClassName);
67
        }
68
69
        return $this->baseValidatorConjunctionsWithChecks[$targetClassName];
70
    }
71
72
    /**
73
     * @param string $targetClassName
74
     * @return ConjunctionValidator
75
     */
76
    protected function buildBaseValidatorConjunctionWithMixedTypesCheck($targetClassName)
77
    {
78
        $interfaces = class_implements($targetClassName);
79
80
        if (true === isset($interfaces[MixedTypesInterface::class])) {
81
            $conjunctionValidator = new ConjunctionValidator();
82
            $newValidator = new MixedTypeObjectValidator();
83
            $conjunctionValidator->addValidator($newValidator);
84
        } else {
85
            $conjunctionValidator = $this->getBaseValidatorConjunction($targetClassName);
86
        }
87
88
        return $conjunctionValidator;
89
    }
90
91
    /**
92
     * @param ExtbaseReflectionService $reflectionService
93
     */
94
    public function injectReflectionService(ExtbaseReflectionService $reflectionService)
95
    {
96
        $this->reflectionService = Core::get()->getObjectManager()->get(ReflectionService::class);
97
    }
98
}
99