Completed
Push — master ( 70cb44...646998 )
by Ivannis Suárez
05:28
created

PropertyMetadata   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 129
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getClassName() 0 4 1
A getPropertyName() 0 4 1
A getDefaultGroup() 0 4 1
A getGroup() 0 4 3
A addConstraint() 0 9 2
A addConstraints() 0 6 2
A getConstraints() 0 4 1
A getConstraintsByGroup() 0 4 2
A mergeConstraints() 0 6 2
A serialize() 0 9 1
A unserialize() 0 11 1
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
namespace Cubiche\Core\Validator\Mapping;
12
13
use Cubiche\Core\Validator\Assert;
14
use Metadata\PropertyMetadata as BasePropertyMetadata;
15
16
/**
17
 * PropertyMetadata class.
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