Completed
Pull Request — master (#11)
by Eric
21:21 queued 18:00
created

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