Completed
Branch master (5acf81)
by Steevan
04:21
created

ConstraintsTrait::addConstraint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace steevanb\SymfonyFormOptionsBuilder\Behavior;
4
5
use Symfony\Component\Validator\Constraint;
6
7 View Code Duplication
trait ConstraintsTrait
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
8
{
9
    use OptionAccessorsTrait;
10
11
    /**
12
     * @param array $constraints
13
     * @return $this
14
     * @link http://symfony.com/doc/current/reference/forms/types/form.html#constraints
15
     */
16
    public function setConstraints(array $constraints)
17
    {
18
        return $this->setOption('constraints', $constraints);
19
    }
20
21
    /**
22
     * @param Constraint $constraint
23
     * @return $this
24
     * @link http://symfony.com/doc/current/reference/forms/types/form.html#constraints
25
     */
26
    public function addConstraint(Constraint $constraint)
27
    {
28
        $constraints = $this->getConstraints();
29
        if (is_array($constraints) === false) {
30
            $constraints = array();
31
        }
32
        $constraints[] = $constraint;
33
        $this->setConstraints($constraints);
34
35
        return $this;
36
    }
37
38
    /**
39
     * @return array|string
40
     * @link http://symfony.com/doc/current/reference/forms/types/form.html#constraints
41
     */
42
    public function getConstraints()
43
    {
44
        return $this->getOption('constraints');
45
    }
46
47
    /**
48
     * @return $this
49
     * @link http://symfony.com/doc/current/reference/forms/types/form.html#constraints
50
     */
51
    public function removeConstraints()
52
    {
53
        return $this->removeOption('constraints');
54
    }
55
}
56