Code Duplication    Length = 129-129 lines in 2 locations

Mapping/MethodMetadata.php 1 location

@@ 22-150 (lines=129) @@
19
 *
20
 * @author Ivannis Suárez Jerez <[email protected]>
21
 */
22
class MethodMetadata extends BaseMethodMetadata
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

Mapping/PropertyMetadata.php 1 location

@@ 21-149 (lines=129) @@
18
 *
19
 * @author Ivannis Suárez Jerez <[email protected]>
20
 */
21
class PropertyMetadata extends BasePropertyMetadata
22
{
23
    /**
24
     * @var string
25
     */
26
    public $defaultGroup;
27
28
    /**
29
     * @var array
30
     */
31
    public $constraintsByGroup = array();
32
33
    /**
34
     * @return string
35
     */
36
    public function getClassName()
37
    {
38
        return $this->class;
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getPropertyName()
45
    {
46
        return $this->name;
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    public function getDefaultGroup()
53
    {
54
        return $this->defaultGroup;
55
    }
56
57
    /**
58
     * @param string $group
59
     *
60
     * @return string
61
     */
62
    protected function getGroup($group = null)
63
    {
64
        return $group === null || empty($group) ? $this->defaultGroup : $group;
65
    }
66
67
    /**
68
     * @param Assert $constraint
69
     * @param string $group
70
     */
71
    public function addConstraint(Assert $constraint, $group = null)
72
    {
73
        $group = $this->getGroup($group);
74
        if (!isset($this->constraintsByGroup[$group])) {
75
            $this->constraintsByGroup[$group] = array();
76
        }
77
78
        $this->constraintsByGroup[$group][] = $constraint;
79
    }
80
81
    /**
82
     * @param array  $constraints
83
     * @param string $group
84
     */
85
    public function addConstraints(array $constraints, $group = null)
86
    {
87
        foreach ($constraints as $constraint) {
88
            $this->addConstraint($constraint, $group);
89
        }
90
    }
91
92
    /**
93
     * @return array
94
     */
95
    public function getConstraints()
96
    {
97
        return $this->constraintsByGroup;
98
    }
99
100
    /**
101
     * @param $group
102
     *
103
     * @return array
104
     */
105
    public function getConstraintsByGroup($group)
106
    {
107
        return isset($this->constraintsByGroup[$group]) ? $this->constraintsByGroup[$group] : array();
108
    }
109
110
    /**
111
     * Merges the constraints of the given metadata into this object.
112
     *
113
     * @param PropertyMetadata $source
114
     */
115
    public function mergeConstraints(PropertyMetadata $source)
116
    {
117
        foreach ($source->getConstraints() as $group => $constraints) {
118
            $this->addConstraints($constraints, $group);
119
        }
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function serialize()
126
    {
127
        return serialize(array(
128
            $this->class,
129
            $this->name,
130
            $this->defaultGroup,
131
            $this->constraintsByGroup,
132
        ));
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function unserialize($str)
139
    {
140
        list(
141
            $this->class,
142
            $this->name,
143
            $this->defaultGroup,
144
            $this->constraintsByGroup) = unserialize($str);
145
146
        $this->reflection = new \ReflectionProperty($this->class, $this->name);
147
        $this->reflection->setAccessible(true);
148
    }
149
}
150