Constraint   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 61
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configureOptions() 0 2 1
A configure() 0 8 1
A __construct() 0 6 2
A normalize() 0 5 1
1
<?php
2
3
namespace JBen87\ParsleyBundle\Constraint;
4
5
use JBen87\ParsleyBundle\Exception\ConstraintException;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
8
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9
10
abstract class Constraint implements NormalizableInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $message = 'Invalid.';
16
17
    /**
18
     * @param array $options
19
     */
20 57
    public function __construct(array $options = [])
21
    {
22 57
        $options = $this->configure($options);
23
24 36
        if (array_key_exists('message', $options)) {
25 17
            $this->message = $options['message'];
26
        }
27 36
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32 18
    public function normalize(NormalizerInterface $normalizer, $format = null, array $context = []): array
33
    {
34
        return [
35 18
            $this->getAttribute() => $this->getValue(),
36 17
            sprintf('%s-message', $this->getAttribute()) => $this->message,
37
        ];
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    abstract protected function getAttribute(): string;
44
45
    /**
46
     * @return string
47
     * @throws ConstraintException
48
     */
49
    abstract protected function getValue(): string;
50
51
    /**
52
     * @param OptionsResolver $resolver
53
     */
54 12
    protected function configureOptions(OptionsResolver $resolver): void
55
    {
56 12
    }
57
58
    /**
59
     * @param array $options
60
     *
61
     * @return array
62
     */
63 57
    private function configure(array $options): array
64
    {
65 57
        $resolver = new OptionsResolver();
66 57
        $resolver->setDefined('message');
67
68 57
        $this->configureOptions($resolver);
69
70 57
        return $resolver->resolve($options);
71
    }
72
}
73