ClassMetadata::className()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the Cubiche package.
4
 *
5
 * Copyright (c) Cubiche
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cubiche\Core\Metadata;
12
13
use Cubiche\Core\Collections\ArrayCollection\ArrayHashMap;
14
use Cubiche\Core\Collections\ArrayCollection\ArrayHashMapInterface;
15
16
/**
17
 * ClassMetadata class.
18
 *
19
 * @author Ivannis Suárez Jerez <[email protected]>
20
 */
21
class ClassMetadata implements \Serializable, ClassMetadataInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $className;
27
28
    /**
29
     * @var \ReflectionClass
30
     */
31
    protected $reflection;
32
33
    /**
34
     * @var ArrayHashMapInterface
35
     */
36
    protected $methodsMetadata;
37
38
    /**
39
     * @var ArrayHashMapInterface
40
     */
41
    protected $propertiesMetadata;
42
43
    /**
44
     * @var ArrayHashMapInterface
45
     */
46
    protected $metadata;
47
48
    /**
49
     * ClassMetadata constructor.
50
     *
51
     * @param string $className
52
     */
53
    public function __construct($className)
54
    {
55
        $this->className = $className;
56
57
        $this->reflection = new \ReflectionClass($className);
58
        $this->methodsMetadata = new ArrayHashMap();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Cubiche\Core\Collec...llection\ArrayHashMap() of type object<Cubiche\Core\Coll...ollection\ArrayHashMap> is incompatible with the declared type object<Cubiche\Core\Coll...\ArrayHashMapInterface> of property $methodsMetadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
        $this->propertiesMetadata = new ArrayHashMap();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Cubiche\Core\Collec...llection\ArrayHashMap() of type object<Cubiche\Core\Coll...ollection\ArrayHashMap> is incompatible with the declared type object<Cubiche\Core\Coll...\ArrayHashMapInterface> of property $propertiesMetadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60
        $this->metadata = new ArrayHashMap();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Cubiche\Core\Collec...llection\ArrayHashMap() of type object<Cubiche\Core\Coll...ollection\ArrayHashMap> is incompatible with the declared type object<Cubiche\Core\Coll...\ArrayHashMapInterface> of property $metadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    public function className()
67
    {
68
        return $this->className;
69
    }
70
71
    /**
72
     * @return \ReflectionClass
73
     */
74
    public function reflection()
75
    {
76
        return $this->reflection;
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public function methodsMetadata()
83
    {
84
        return $this->methodsMetadata->toArray();
85
    }
86
87
    /**
88
     * @param MethodMetadata $metadata
89
     */
90
    public function addMethodMetadata(MethodMetadata $metadata)
91
    {
92
        $this->methodsMetadata->set($metadata->methodName(), $metadata);
93
    }
94
95
    /**
96
     * @param string $methodName
97
     *
98
     * @return MethodMetadata|null
99
     */
100
    public function methodMetadata($methodName)
101
    {
102
        return $this->methodsMetadata->get($methodName);
103
    }
104
105
    /**
106
     * @return array
107
     */
108
    public function propertiesMetadata()
109
    {
110
        return $this->propertiesMetadata->toArray();
111
    }
112
113
    /**
114
     * @param PropertyMetadata $metadata
115
     */
116
    public function addPropertyMetadata(PropertyMetadata $metadata)
117
    {
118
        $this->propertiesMetadata->set($metadata->propertyName(), $metadata);
119
    }
120
121
    /**
122
     * @param string $propertyName
123
     *
124
     * @return PropertyMetadata|null
125
     */
126
    public function propertyMetadata($propertyName)
127
    {
128
        return $this->propertiesMetadata->get($propertyName);
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    public function metadata()
135
    {
136
        return $this->metadata->toArray();
137
    }
138
139
    /**
140
     * @param string $key
141
     * @param mixed  $value
142
     */
143
    public function addMetadata($key, $value)
144
    {
145
        $this->metadata->set($key, $value);
146
    }
147
148
    /**
149
     * @param string $key
150
     *
151
     * @return mixed|null
152
     */
153
    public function getMetadata($key)
154
    {
155
        return $this->metadata->get($key);
156
    }
157
158
    /**
159
     * @param ClassMetadataInterface $object
160
     *
161
     * @return ClassMetadataInterface
162
     */
163
    public function merge(ClassMetadataInterface $object)
164
    {
165
        $this->className = $object->className();
166
        $this->reflection = $object->reflection();
167
168
        foreach ($object->methodsMetadata() as $methodName => $metadata) {
169
            $this->addMethodMetadata($metadata);
170
        }
171
172
        foreach ($object->propertiesMetadata() as $propertyName => $metadata) {
173
            $this->addPropertyMetadata($metadata);
174
        }
175
176
        foreach ($object->metadata() as $key => $value) {
177
            $this->addMetadata($key, $value);
178
        }
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function serialize()
185
    {
186
        return serialize(array(
187
            $this->className,
188
            $this->methodsMetadata->toArray(),
189
            $this->propertiesMetadata->toArray(),
190
            $this->metadata->toArray(),
191
        ));
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function unserialize($str)
198
    {
199
        list(
200
            $this->className,
201
            $methodsMetadata,
202
            $propertiesMetadata,
203
            $metadata
204
        ) = unserialize($str);
205
206
        $this->reflection = new \ReflectionClass($this->className);
207
        $this->methodsMetadata = new ArrayHashMap($methodsMetadata);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Cubiche\Core\Collec...shMap($methodsMetadata) of type object<Cubiche\Core\Coll...ollection\ArrayHashMap> is incompatible with the declared type object<Cubiche\Core\Coll...\ArrayHashMapInterface> of property $methodsMetadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
208
        $this->propertiesMetadata = new ArrayHashMap($propertiesMetadata);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Cubiche\Core\Collec...ap($propertiesMetadata) of type object<Cubiche\Core\Coll...ollection\ArrayHashMap> is incompatible with the declared type object<Cubiche\Core\Coll...\ArrayHashMapInterface> of property $propertiesMetadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
209
        $this->metadata = new ArrayHashMap($metadata);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Cubiche\Core\Collec...ArrayHashMap($metadata) of type object<Cubiche\Core\Coll...ollection\ArrayHashMap> is incompatible with the declared type object<Cubiche\Core\Coll...\ArrayHashMapInterface> of property $metadata.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
210
    }
211
}
212