Completed
Push — master ( d8a83b...76c960 )
by Eric
02:56
created

ClassMetadata::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
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
     * @param string $name
31
     */
32
    public function __construct($name)
33
    {
34
        $this->setName($name);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getName()
41
    {
42
        return $this->name;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function setName($name)
49
    {
50
        if (!class_exists($name)) {
51
            throw new \InvalidArgumentException(sprintf('The class "%s" does not exist.', $name));
52
        }
53
54
        $this->name = $name;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function hasProperties()
61
    {
62
        return !empty($this->properties);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getProperties()
69
    {
70
        return $this->properties;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function setProperties(array $properties)
77
    {
78
        $this->properties = [];
79
80
        foreach ($properties as $property) {
81
            $this->addProperty($property);
82
        }
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function hasProperty($name)
89
    {
90
        return isset($this->properties[$name]);
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getProperty($name)
97
    {
98
        return $this->hasProperty($name) ? $this->properties[$name] : null;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function addProperty(PropertyMetadataInterface $property)
105
    {
106
        $name = $property->getName();
107
108
        if ($this->hasProperty($name)) {
109
            $this->getProperty($name)->merge($property);
110
        } else {
111
            $this->properties[$name] = $property;
112
        }
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function removeProperty($name)
119
    {
120
        unset($this->properties[$name]);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function merge(ClassMetadataInterface $classMetadata)
127
    {
128
        foreach ($classMetadata->getProperties() as $property) {
129
            $this->addProperty($property);
130
        }
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function serialize()
137
    {
138
        return serialize([
139
            $this->name,
140
            $this->properties,
141
        ]);
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function unserialize($serialized)
148
    {
149
        list(
150
            $this->name,
151
            $this->properties
152
        ) = unserialize($serialized);
153
    }
154
}
155