Issues (30)

src/Exception/FormValidationException.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace MediaMonks\RestApi\Exception;
4
5
use JetBrains\PhpStorm\ArrayShape;
6
use MediaMonks\RestApi\Response\Error;
7
use MediaMonks\RestApi\Util\StringUtil;
8
use Symfony\Component\Form\FormError;
9
use Symfony\Component\Form\FormInterface;
10
11
class FormValidationException extends AbstractValidationException
12
{
13
    const FIELD_ROOT = '#';
14
15
    public function __construct(
16
        private FormInterface $form,
17
        protected $message = Error::MESSAGE_FORM_VALIDATION,
18
        protected $code = Error::CODE_FORM_VALIDATION
19
    ) {
20
        parent::__construct($message, $code);
21
    }
22
23
    public function getFields(): array
24 3
    {
25
        return $this->getErrorMessages($this->form);
26
    }
27
28
    protected function getErrorMessages(FormInterface $form): array
29 3
    {
30 3
        $errors = [];
31 3
        foreach ($this->getFormErrorMessages($form) as $error) {
32
            $errors[] = $error;
33
        }
34
        foreach ($this->getFormChildErrorMessages($form) as $error) {
35
            $errors[] = $error;
36 3
        }
37
38 3
        return $errors;
39
    }
40
41
    protected function getFormErrorMessages(FormInterface $form): array
42
    {
43
        $errors = [];
44
        foreach ($form->getErrors() as $error) {
45 3
            if ($form->isRoot()) {
46
                $errors[] = $this->toErrorArray($error);
0 ignored issues
show
It seems like $error can also be of type Symfony\Component\Form\FormErrorIterator; however, parameter $error of MediaMonks\RestApi\Excep...ception::toErrorArray() does only seem to accept Symfony\Component\Form\FormError, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
                $errors[] = $this->toErrorArray(/** @scrutinizer ignore-type */ $error);
Loading history...
47 3
            } else {
48 3
                $errors[] = $this->toErrorArray($error, $form);
49 1
            }
50 3
        }
51 3
52 1
        return $errors;
53 3
    }
54
55 3
    protected function getFormChildErrorMessages(FormInterface $form): array
56
    {
57
        $errors = [];
58
        foreach ($form->all() as $child) {
59
            if ($this->shouldAddChildErrorMessage($child)) {
60
                foreach ($this->getErrorMessages($child) as $error) {
61
                    $errors[] = $error;
62 3
                }
63
            }
64 3
        }
65 3
66 1
        return $errors;
67 1
    }
68 1
69 1
    protected function shouldAddChildErrorMessage(FormInterface $child = null): bool
70
    {
71 3
        return !empty($child) && !$child->isValid();
72
    }
73 3
74
    protected function toErrorArray(FormError $error, FormInterface $form = null): array
75
    {
76
        if (is_null($form)) {
77
            $field = self::FIELD_ROOT;
78
        } else {
79
            $field = $form->getName();
80 3
        }
81
        if (!is_null($error->getCause()) && !is_null($error->getCause()->getConstraint())) {
82 3
            $code = $this->getErrorCode(StringUtil::classToSnakeCase($error->getCause()->getConstraint()));
83 3
        } else {
84 1
            $code = $this->getErrorCodeByMessage($error);
85 1
        }
86 1
87 1
        return (new ErrorField($field, $code, $error->getMessage()))->toArray();
88 1
    }
89 3
90
    protected function getErrorCodeByMessage(FormError $error): string
91 3
    {
92
        if (stristr($error->getMessage(), Error::FORM_TYPE_CSRF)) {
93
            return $this->getErrorCode(Error::FORM_TYPE_CSRF);
94
        }
95
96
        return $this->getErrorCode(Error::FORM_TYPE_GENERAL);
97
    }
98 1
99
    protected function getErrorCode(string $value): string
100 1
    {
101
        return sprintf(Error::ERROR_KEY_FORM_VALIDATION.'.%s', $value);
102
    }
103
}
104