Constraint::configureOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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