Completed
Pull Request — master (#11)
by Eric
65:27
created

ClassMetadata::setXmlRoot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
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 1720
    private $xmlRoot;
33
34 1720
    /**
35 1716
     * @param string $name
36
     */
37
    public function __construct($name)
38
    {
39
        $this->setName($name);
40 1680
    }
41
42 1680
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getName()
46
    {
47
        return $this->name;
48 1720
    }
49
50 1720
    /**
51 4
     * {@inheritdoc}
52
     */
53
    public function setName($name)
54 1716
    {
55 1716
        if (!class_exists($name)) {
56
            throw new \InvalidArgumentException(sprintf('The class "%s" does not exist.', $name));
57
        }
58
59
        $this->name = $name;
60 20
    }
61
62 20
    /**
63
     * {@inheritdoc}
64
     */
65
    public function hasProperties()
66
    {
67
        return !empty($this->properties);
68 1596
    }
69
70 1596
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getProperties()
74
    {
75
        return $this->properties;
76 1412
    }
77
78 1412
    /**
79
     * {@inheritdoc}
80 1412
     */
81 1412
    public function setProperties(array $properties)
82 706
    {
83 1412
        $this->properties = [];
84
85
        foreach ($properties as $property) {
86
            $this->addProperty($property);
87
        }
88 1588
    }
89
90 1588
    /**
91
     * {@inheritdoc}
92
     */
93
    public function hasProperty($name)
94
    {
95
        return isset($this->properties[$name]);
96 1588
    }
97
98 1588
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getProperty($name)
102
    {
103
        return $this->hasProperty($name) ? $this->properties[$name] : null;
104 1428
    }
105
106 1428
    /**
107
     * {@inheritdoc}
108 1428
     */
109 8
    public function addProperty(PropertyMetadataInterface $property)
110 4
    {
111 1428
        $name = $property->getName();
112
113 1428
        if ($this->hasProperty($name)) {
114
            $this->getProperty($name)->merge($property);
115
        } else {
116
            $this->properties[$name] = $property;
117
        }
118 4
    }
119
120 4
    /**
121 4
     * {@inheritdoc}
122
     */
123
    public function removeProperty($name)
124
    {
125
        unset($this->properties[$name]);
126 8
    }
127
128 8
    /**
129 4
     * {@inheritdoc}
130 4
     */
131 8
    public function hasXmlRoot()
132
    {
133
        return $this->xmlRoot !== null;
134
    }
135
136 4
    /**
137
     * {@inheritdoc}
138 4
     */
139 4
    public function getXmlRoot()
140 4
    {
141 2
        return $this->xmlRoot;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147 4
    public function setXmlRoot($xmlRoot)
148
    {
149
        $this->xmlRoot = $xmlRoot;
150 4
    }
151 4
152 4
    /**
153 4
     * {@inheritdoc}
154
     */
155
    public function merge(ClassMetadataInterface $classMetadata)
156
    {
157
        if ($classMetadata->hasXmlRoot()) {
158
            $this->setXmlRoot($classMetadata->getXmlRoot());
159
        }
160
161
        foreach ($classMetadata->getProperties() as $property) {
162
            $this->addProperty($property);
163
        }
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169
    public function serialize()
170
    {
171
        return serialize([
172
            $this->name,
173
            $this->properties,
174
            $this->xmlRoot,
175
        ]);
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function unserialize($serialized)
182
    {
183
        list(
184
            $this->name,
185
            $this->properties,
186
            $this->xmlRoot
187
        ) = unserialize($serialized);
188
    }
189
}
190