Test Failed
Push — master ( 0db9fe...ef12af )
by Vincent
13:16
created

EmailElementBuilder::errorMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Bdf\Form\Leaf\Helper;
4
5
use Bdf\Form\ElementInterface;
6
use Bdf\Form\Leaf\StringElementBuilder;
7
use Bdf\Form\Registry\RegistryInterface;
8
use Bdf\Form\Transformer\TransformerInterface;
9
use Bdf\Form\Validator\ValueValidatorInterface;
10
use Symfony\Component\Validator\Constraints\Email;
11
12
/**
13
 * Provide email constraint builder for a StringElementBuilder
14
 *
15
 * <code>
16
 * $builder->email('contact')
17
 *     ->mode(Email::VALIDATION_MODE_LOOSE)
18
 *     ->errorMessage('Invalid email address')
19
 * ;
20
 * </code>
21
 *
22
 * @see EmailElement the built element
23
 */
24
class EmailElementBuilder extends StringElementBuilder
25
{
26
    /**
27
     * @var bool
28
     */
29
    private $useConstraint = true;
30
31
    /**
32
     * @var array{message?:string,mode?:string,normalizer?:callable(string):string}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{message?:string,mo...allable(string):string} at position 12 could not be parsed: Expected '}' at position 12, but found 'callable'.
Loading history...
33
     */
34
    private $constraintOptions = [];
35
36
    /**
37
     * EmailElementBuilder constructor.
38
     *
39
     * @param RegistryInterface|null $registry
40
     */
41
    public function __construct(?RegistryInterface $registry = null)
42
    {
43
        parent::__construct($registry);
44
45
        $this->addConstraintsProvider([$this, 'createEmailConstraint']);
46
    }
47
48
    /**
49
     * The validation mode
50
     * See Email::VALIDATION_MODE_* constants
51
     *
52
     * @param string $mode
53
     *
54
     * @return $this
55
     *
56
     * @see Email::VALIDATION_MODE_HTML5
57
     * @see Email::VALIDATION_MODE_LOOSE
58
     * @see Email::VALIDATION_MODE_STRICT
59
     */
60
    public function mode(string $mode): self
61
    {
62
        $this->constraintOptions['mode'] = $mode;
63
64
        return $this;
65
    }
66
67
    /**
68
     * Define the invalid email error message
69
     *
70
     * @param string $message
71
     *
72
     * @return $this
73
     */
74
    public function errorMessage(string $message): self
75
    {
76
        $this->constraintOptions['message'] = $message;
77
78
        return $this;
79
    }
80
81
    /**
82
     * Define the normalizer for the email value
83
     *
84
     * <code>
85
     * // Add normalizer for adding an email host if not provided
86
     * $builder->email('contact')->normalizer(function (string $value) {
87
     *     if (strpos($value, '@') === false) {
88
     *         $value .= '@example.com';
89
     *     }
90
     *
91
     *     return $value;
92
     * });
93
     * </code>
94
     *
95
     * @param callable(string):string $normalizer
96
     *
97
     * @return $this
98
     */
99
    public function normalizer(callable $normalizer): self
100
    {
101
        $this->constraintOptions['normalizer'] = $normalizer;
102
103
        return $this;
104
    }
105
106
    /**
107
     * Disable the email verification constraint
108
     *
109
     * @return $this
110
     */
111
    public function disableConstraint(): self
112
    {
113
        $this->useConstraint = false;
114
115
        return $this;
116
    }
117
118
    /**
119
     * Define the email validation constraint options
120
     *
121
     * <code>
122
     * $builder->email('contact')->useConstraint(['mode' => Email::VALIDATION_MODE_HTML5, 'message' => 'my error']);
123
     * </code>
124
     *
125
     * @param array{message?:string,mode?:string,normalizer?:callable(string):string} $options
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{message?:string,mo...allable(string):string} at position 12 could not be parsed: Expected '}' at position 12, but found 'callable'.
Loading history...
126
     *
127
     * @return $this
128
     *
129
     * @see Email for list of options
130
     */
131
    public function useConstraint(array $options = []): self
132
    {
133
        $this->useConstraint = true;
134
        $this->constraintOptions = $options;
135
136
        return $this;
137
    }
138
139
    /**
140
     * @return \Symfony\Component\Validator\Constraint[]
141
     */
142
    protected function createEmailConstraint(): array
143
    {
144
        if (!$this->useConstraint) {
145
            return [];
146
        }
147
148
        return [new Email($this->constraintOptions)];
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    protected function createElement(ValueValidatorInterface $validator, TransformerInterface $transformer): ElementInterface
155
    {
156
        return new EmailElement($validator, $transformer, $this->getChoices());
157
    }
158
}
159