Completed
Push — master ( 872463...57cf72 )
by Ivannis Suárez
02:58
created

MethodMetadata::getDefaultGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 0
Metric Value
dl 4
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Core\Validator\Mapping;
13
14
use Cubiche\Core\Validator\Assert;
15
use Metadata\MethodMetadata as BaseMethodMetadata;
16
17
/**
18
 * MethodMetadata class.
19
 *
20
 * @author Ivannis Suárez Jerez <[email protected]>
21
 */
22 View Code Duplication
class MethodMetadata extends BaseMethodMetadata
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...
23
{
24
    /**
25
     * @var string
26
     */
27
    public $defaultGroup;
28
29
    /**
30
     * @var array
31
     */
32
    public $constraintsByGroup = array();
33
34
    /**
35
     * @return string
36
     */
37
    public function getClassName()
38
    {
39
        return $this->class;
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function getMethodName()
46
    {
47
        return $this->name;
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getDefaultGroup()
54
    {
55
        return $this->defaultGroup;
56
    }
57
58
    /**
59
     * @param string $group
60
     *
61
     * @return string
62
     */
63
    protected function getGroup($group = null)
64
    {
65
        return $group === null || empty($group) ? $this->defaultGroup : $group;
66
    }
67
68
    /**
69
     * @param Assert $constraint
70
     * @param string $group
71
     */
72
    public function addConstraint(Assert $constraint, $group = null)
73
    {
74
        $group = $this->getGroup($group);
75
        if (!isset($this->constraintsByGroup[$group])) {
76
            $this->constraintsByGroup[$group] = array();
77
        }
78
79
        $this->constraintsByGroup[$group][] = $constraint;
80
    }
81
82
    /**
83
     * @param array  $constraints
84
     * @param string $group
85
     */
86
    public function addConstraints(array $constraints, $group = null)
87
    {
88
        foreach ($constraints as $constraint) {
89
            $this->addConstraint($constraint, $group);
90
        }
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function getConstraints()
97
    {
98
        return $this->constraintsByGroup;
99
    }
100
101
    /**
102
     * @param $group
103
     *
104
     * @return array
105
     */
106
    public function getConstraintsByGroup($group)
107
    {
108
        return isset($this->constraintsByGroup[$group]) ? $this->constraintsByGroup[$group] : array();
109
    }
110
111
    /**
112
     * Merges the constraints of the given metadata into this object.
113
     *
114
     * @param MethodMetadata $source
115
     */
116
    public function mergeConstraints(MethodMetadata $source)
117
    {
118
        foreach ($source->getConstraints() as $group => $constraints) {
119
            $this->addConstraints($constraints, $group);
120
        }
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function serialize()
127
    {
128
        return serialize(array(
129
            $this->class,
130
            $this->name,
131
            $this->defaultGroup,
132
            $this->constraintsByGroup,
133
        ));
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function unserialize($str)
140
    {
141
        list(
142
            $this->class,
143
            $this->name,
144
            $this->defaultGroup,
145
            $this->constraintsByGroup) = unserialize($str);
146
147
        $this->reflection = new \ReflectionMethod($this->class, $this->name);
148
        $this->reflection->setAccessible(true);
149
    }
150
}
151