• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FluidTYPO3 / vhs / 13566190336

27 Feb 2025 12:18PM UTC coverage: 72.127% (-0.6%) from 72.746%
13566190336

push

github

NamelessCoder
[TER] 7.1.0

5649 of 7832 relevant lines covered (72.13%)

20.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/Classes/ViewHelpers/Condition/Form/HasValidatorViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Condition\Form;
3

4
/*
5
 * This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10

11
use TYPO3\CMS\Core\Utility\GeneralUtility;
12
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
13
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
14
use TYPO3\CMS\Extbase\Reflection\ReflectionService;
15
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
16
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractConditionViewHelper;
17

18
/**
19
 * ### Form: Field Has Validator?
20
 *
21
 * Takes a property (dotted path supported) and renders the
22
 * then-child if the property at the given path has any
23
 * @validate annotation.
24
 */
25
class HasValidatorViewHelper extends AbstractConditionViewHelper
26
{
27
    public function initializeArguments(): void
28
    {
29
        parent::initializeArguments();
14✔
30
        $this->registerArgument(
14✔
31
            'property',
14✔
32
            'string',
14✔
33
            'The property name, dotted path supported, to determine required.',
14✔
34
            true
14✔
35
        );
14✔
36
        $this->registerArgument(
14✔
37
            'validatorName',
14✔
38
            'string',
14✔
39
            'The name of the validator that must exist for the condition to be true.',
14✔
40
            true
14✔
41
        );
14✔
42
        $this->registerArgument(
14✔
43
            'object',
14✔
44
            DomainObjectInterface::class,
14✔
45
            'Optional object - if not specified, grabs the associated form object.',
14✔
46
            true
14✔
47
        );
14✔
48
    }
49

50
    public static function verdict(array $arguments, RenderingContextInterface $renderingContext): bool
51
    {
52
        /** @var ReflectionService $reflectionService */
53
        $reflectionService = GeneralUtility::makeInstance(ReflectionService::class);
42✔
54

55
        /** @var string $property */
56
        $property = $arguments['property'];
42✔
57
        $validatorName = $arguments['validatorName'] ?? null;
42✔
58
        /** @var array|object $object */
59
        $object = $arguments['object'] ?? null;
42✔
60

61
        $path = null;
42✔
62
        if (strpos($property, '.') !== false) {
42✔
63
            $parts = explode('.', $property);
28✔
64
            $property = array_pop($parts);
28✔
65
            $path = implode('.', $parts);
28✔
66
            $object = ObjectAccess::getPropertyPath($object, $path);
28✔
67
        }
68

69
        if (!is_object($object)) {
42✔
70
            return false;
28✔
71
        }
72

73
        if (!method_exists($reflectionService, 'getPropertyTagValues')) {
14✔
74
            // TYPO3 version no longer contains the raw property tag value extractor. Instead, we can check for a given
75
            // validator by extracting the validators and analysing those.
76
            $validators = $reflectionService->getClassSchema($object)->getProperty($property)->getValidators();
14✔
77
            foreach ($validators as $validatorConfiguration) {
14✔
78
                if ($validatorConfiguration['name'] === $validatorName) {
×
79
                    return true;
×
80
                }
81
            }
82
            return false;
14✔
83
        }
84

85
        $className = get_class($object);
×
86
        $annotations = $reflectionService->getPropertyTagValues($className, $property, 'validate');
×
87
        if (empty($annotations)) {
×
88
            // We tried looking for the legacy validator name but found none. Retry with the new way. We have to do this
89
            // as a retry, because we cannot assume that any site using TYPO3 9.1+ will also be using the modern
90
            // annotations. Hence we cannot change the validator name until we've also looked for the legacy ones (which
91
            // will take priority if found).
92
            $annotations = $reflectionService->getPropertyTagValues($className, $property, 'Extbase\\Validate');
×
93
        }
94
        return (count($annotations) && (!$validatorName || in_array($validatorName, $annotations)));
×
95
    }
96
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc