Completed
Push — master ( 1e2cfa...28995d )
by Eric
04:38
created

ClassMetadata::merge()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 4
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Mapping;
13
14
/**
15
 * @author GeLo <[email protected]>
16
 */
17
class ClassMetadata implements ClassMetadataInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    private $name;
23
24
    /**
25
     * @var PropertyMetadataInterface[]
26
     */
27
    private $properties = [];
28
29
    /**
30
     * @var string|null
31
     */
32
    private $xmlRoot;
33
34
    /**
35
     * @param string $name
36
     */
37 2584
    public function __construct($name)
38
    {
39 2584
        $this->setName($name);
40 2580
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2540
    public function getName()
46
    {
47 2540
        return $this->name;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2584
    public function setName($name)
54
    {
55 2584
        if (!class_exists($name)) {
56 4
            throw new \InvalidArgumentException(sprintf('The class "%s" does not exist.', $name));
57
        }
58
59 2580
        $this->name = $name;
60 2580
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 20
    public function hasProperties()
66
    {
67 20
        return !empty($this->properties);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 1804
    public function getProperties()
74
    {
75 1804
        return $this->properties;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 1812
    public function setProperties(array $properties)
82
    {
83 1812
        $this->properties = [];
84
85 1812
        foreach ($properties as $property) {
86 1812
            $this->addProperty($property);
87 906
        }
88 1812
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 1828
    public function hasProperty($name)
94
    {
95 1828
        return isset($this->properties[$name]);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 1828
    public function getProperty($name)
102
    {
103 1828
        return $this->hasProperty($name) ? $this->properties[$name] : null;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 1828
    public function addProperty(PropertyMetadataInterface $property)
110
    {
111 1828
        $name = $property->getName();
112
113 1828
        if ($this->hasProperty($name)) {
114 8
            $this->getProperty($name)->merge($property);
115 4
        } else {
116 1828
            $this->properties[$name] = $property;
117
        }
118 1828
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 4
    public function removeProperty($name)
124
    {
125 4
        unset($this->properties[$name]);
126 4
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 808
    public function hasXmlRoot()
132
    {
133 808
        return $this->xmlRoot !== null;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 644
    public function getXmlRoot()
140
    {
141 644
        return $this->xmlRoot;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 136
    public function setXmlRoot($xmlRoot)
148
    {
149 136
        $this->xmlRoot = $xmlRoot;
150 136
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155 8
    public function merge(ClassMetadataInterface $classMetadata)
156
    {
157 8
        if ($classMetadata->hasXmlRoot()) {
158 4
            $this->setXmlRoot($classMetadata->getXmlRoot());
159 2
        }
160
161 8
        foreach ($classMetadata->getProperties() as $property) {
162 4
            $this->addProperty($property);
163 4
        }
164 8
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 4
    public function serialize()
170
    {
171 4
        return serialize([
172 4
            $this->name,
173 4
            $this->properties,
174 4
            $this->xmlRoot,
175 2
        ]);
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181 4
    public function unserialize($serialized)
182
    {
183
        list(
184 4
            $this->name,
185 4
            $this->properties,
186 4
            $this->xmlRoot
187 4
        ) = unserialize($serialized);
188 4
    }
189
}
190