ErrorClassViewHelper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 41
rs 10
c 4
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 6 1
A render() 0 22 6
A getValidationErrors() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\ViewHelpers\Validation;
13
14
use DERHANSEN\SfEventMgt\Domain\Model\Registration\Field;
15
use Psr\Http\Message\ServerRequestInterface;
16
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
17
18
/**
19
 * ViewHelper to render a given class when a field has validation errors
20
 */
21
class ErrorClassViewHelper extends AbstractViewHelper
22
{
23
    public function initializeArguments(): void
24
    {
25
        $this->registerArgument('fieldname', 'string', 'A fieldname to be checked');
26
        $this->registerArgument('registrationField', 'object', 'A registration field record');
27
        $this->registerArgument('class', 'string', 'Classname if the field has an error', false, 'error-class');
28
        parent::initializeArguments();
29
    }
30
31
    public function render(): string
32
    {
33
        $result = '';
34
        $validationErrors = $this->getValidationErrors();
35
36
        if (isset($this->arguments['fieldname'])) {
37
            $fieldname = 'registration.' . $this->arguments['fieldname'];
38
        } elseif (isset($this->arguments['registrationField']) &&
39
            $this->arguments['registrationField'] instanceof Field) {
40
            $fieldname = 'registration.fields.' . $this->arguments['registrationField']->getUid();
41
        } else {
42
            return '';
43
        }
44
45
        foreach ($validationErrors as $validationFieldName => $fieldValidationErrors) {
46
            if ($validationFieldName === $fieldname) {
47
                $result = $this->arguments['class'];
48
                break;
49
            }
50
        }
51
52
        return $result;
53
    }
54
55
    protected function getValidationErrors(): array
56
    {
57
        $request = $this->renderingContext->getAttribute(ServerRequestInterface::class);
58
        $extbaseRequestParameters = $request->getAttribute('extbase');
59
        $validationResults = $extbaseRequestParameters->getOriginalRequestMappingResults();
60
61
        return $validationResults->getFlattenedErrors();
62
    }
63
}
64