Completed
Push — master ( 59ea22...c6592e )
by Siim
14:08
created

AbstractEntityValidator::validateField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 4
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: siim
5
 * Date: 10.02.19
6
 * Time: 16:55
7
 */
8
9
namespace Sf4\Api\EntityValidator;
10
11
use Sf4\Api\Entity\EntityInterface;
12
use Sf4\Api\Notification\NotificationInterface;
13
use Sf4\Api\Notification\Traits\ConvertViolationsToErrorMessage;
14
use Sf4\Api\Utils\Traits\TranslatorTrait;
15
use Symfony\Component\Validator\Validator\ValidatorInterface;
16
17
abstract class AbstractEntityValidator
18
{
19
    use TranslatorTrait;
20
    use ConvertViolationsToErrorMessage;
21
22
    const VALUE = 'value';
23
    const CONSTRAINTS = 'constraints';
24
    const MIN = 'min';
25
    const PATTERN = 'pattern';
26
27
    const OPTION_MESSAGE = 'message';
28
    const OPTION_MIN_MESSAGE = 'minMessage';
29
30
    /*
31
     * \p{L} - single character
32
     * \p{Ll} - a lowercase letter
33
     * \p{Lu} - an uppercase letter
34
     * \p{Zs} - any kind of space character
35
     * \p{N} -  any kind of numeric character
36
     * \X - line break characters,
37
     * \p{P} - any kind of punctuation character
38
     */
39
    const NAME_REGEX = '/^(\p{Lu})([\p{L}\p{Zs}-]+)(\p{Ll})$/um';
40
41
    /**
42
     * @param EntityInterface $entity
43
     * @return array
44
     */
45
    abstract protected function getValidationRules(EntityInterface $entity): array;
46
47
    /**
48
     * @param EntityInterface $entity
49
     * @param ValidatorInterface $validator
50
     * @param NotificationInterface $notification
51
     */
52
    public function validate(
53
        EntityInterface $entity,
54
        ValidatorInterface $validator,
55
        NotificationInterface $notification
56
    ) {
57
        $rules = $this->getValidationRules($entity);
58
        foreach ($rules as $fieldName => $rule) {
59
            $this->validateField($validator, $notification, $fieldName, $rule);
60
        }
61
    }
62
63
    /**
64
     * @param ValidatorInterface $validator
65
     * @param NotificationInterface $notification
66
     * @param string $fieldName
67
     * @param array $rule
68
     */
69
    protected function validateField(
70
        ValidatorInterface $validator,
71
        NotificationInterface $notification,
72
        string $fieldName,
73
        array $rule
74
    ) {
75
        $this->convertViolationsToErrormessage(
76
            $validator->validate(
77
                $rule[static::VALUE],
78
                $rule[static::CONSTRAINTS]
79
            ),
80
            $notification,
81
            $fieldName
82
        );
83
    }
84
85
    /**
86
     * @param $id
87
     * @param array $parameters
88
     * @param null $domain
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $domain is correct as it would always require null to be passed?
Loading history...
89
     * @param null $locale
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $locale is correct as it would always require null to be passed?
Loading history...
90
     * @return string
91
     */
92
    protected function translate($id, array $parameters = array(), $domain = null, $locale = null)
93
    {
94
        $translator = $this->getTranslator();
95
96
        return $translator->trans($id, $parameters, $domain, $locale);
97
    }
98
}
99