Completed
Push — master ( c5c117...8f19bf )
by Yann
10s
created

EnumTypeGuesser::guessRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 2
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Yokai\EnumBundle\Form\Extension;
4
5
use Symfony\Component\Form\AbstractType;
6
use Symfony\Component\Form\Extension\Validator\ValidatorTypeGuesser;
7
use Symfony\Component\Form\Guess\Guess;
8
use Symfony\Component\Form\Guess\TypeGuess;
9
use Symfony\Component\Validator\Constraint;
10
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
11
use Yokai\EnumBundle\Form\Type\EnumType;
12
use Yokai\EnumBundle\Registry\EnumRegistryInterface;
13
use Yokai\EnumBundle\Validator\Constraints\Enum;
14
15
/**
16
 * @author Yann Eugoné <[email protected]>
17
 */
18
class EnumTypeGuesser extends ValidatorTypeGuesser
19
{
20
    /**
21
     * @var EnumRegistryInterface
22
     */
23
    private $enumRegistry;
24
25
    /**
26
     * @var string
27
     */
28
    private $enumFormType;
29
30
    /**
31
     * @param MetadataFactoryInterface $metadataFactory
32
     * @param EnumRegistryInterface    $enumRegistry
33
     */
34 5
    public function __construct(MetadataFactoryInterface $metadataFactory, EnumRegistryInterface $enumRegistry)
35
    {
36 5
        parent::__construct($metadataFactory);
37
38 5
        $this->enumRegistry = $enumRegistry;
39
40 5
        if (method_exists(AbstractType::class, 'getBlockPrefix')) {
41
            $this->enumFormType = EnumType::class; //Symfony 3.x support
42
        } else {
43 5
            $this->enumFormType = 'enum'; //Symfony 2.x support
44
        }
45 5
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 2
    public function guessTypeForConstraint(Constraint $constraint)
51
    {
52 2
        if (!$constraint instanceof Enum) {
53
            return null;
54
        }
55
56 2
        return new TypeGuess(
57 2
            $this->enumFormType,
58
            [
59 2
                'enum' => $constraint->enum,
60 2
                'multiple' => $constraint->multiple,
61 2
            ],
62
            Guess::HIGH_CONFIDENCE
63 2
        );
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69 2
    public function guessRequired($class, $property)
70
    {
71 2
        return null; //override parent : not able to guess
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77 2
    public function guessMaxLength($class, $property)
78
    {
79 2
        return null; //override parent : not able to guess
80
    }
81
82
    /**
83
     * @inheritDoc
84
     */
85 2
    public function guessPattern($class, $property)
86
    {
87 2
        return null; //override parent : not able to guess
88
    }
89
}
90