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
|
|
|
* PropertyMetadata class. |
18
|
|
|
* |
19
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
class PropertyMetadata implements \Serializable |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $className; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $propertyName; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var \ReflectionProperty |
35
|
|
|
*/ |
36
|
|
|
protected $reflection; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ArrayHashMapInterface |
40
|
|
|
*/ |
41
|
|
|
protected $metadata; |
42
|
|
|
|
43
|
|
|
public function __construct($className, $propertyName) |
44
|
|
|
{ |
45
|
|
|
$this->className = $className; |
46
|
|
|
$this->propertyName = $propertyName; |
47
|
|
|
$this->metadata = new ArrayHashMap(); |
48
|
|
|
|
49
|
|
|
$this->reflection = new \ReflectionProperty($className, $propertyName); |
50
|
|
|
$this->reflection->setAccessible(true); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function className() |
57
|
|
|
{ |
58
|
|
|
return $this->className; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function propertyName() |
65
|
|
|
{ |
66
|
|
|
return $this->propertyName; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return \ReflectionProperty |
71
|
|
|
*/ |
72
|
|
|
public function reflection() |
73
|
|
|
{ |
74
|
|
|
return $this->reflection; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function metadata() |
81
|
|
|
{ |
82
|
|
|
return $this->metadata->toArray(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $key |
87
|
|
|
* @param mixed $value |
88
|
|
|
*/ |
89
|
|
|
public function addMetadata($key, $value) |
90
|
|
|
{ |
91
|
|
|
$this->metadata->set($key, $value); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $key |
96
|
|
|
* |
97
|
|
|
* @return mixed|null |
98
|
|
|
*/ |
99
|
|
|
public function getMetadata($key) |
100
|
|
|
{ |
101
|
|
|
return $this->metadata->get($key); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param object $obj |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function getValue($obj) |
110
|
|
|
{ |
111
|
|
|
return $this->reflection->getValue($obj); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param object $obj |
116
|
|
|
* @param string $value |
117
|
|
|
*/ |
118
|
|
|
public function setValue($obj, $value) |
119
|
|
|
{ |
120
|
|
|
$this->reflection->setValue($obj, $value); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function serialize() |
127
|
|
|
{ |
128
|
|
|
return serialize(array( |
129
|
|
|
$this->className, |
130
|
|
|
$this->propertyName, |
131
|
|
|
$this->metadata->toArray(), |
132
|
|
|
)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function unserialize($str) |
139
|
|
|
{ |
140
|
|
|
list( |
141
|
|
|
$this->className, |
142
|
|
|
$this->propertyName, |
143
|
|
|
$metadata) = unserialize($str); |
144
|
|
|
|
145
|
|
|
$this->reflection = new \ReflectionProperty($this->className, $this->propertyName); |
146
|
|
|
$this->reflection->setAccessible(true); |
147
|
|
|
|
148
|
|
|
$this->metadata = new ArrayHashMap($metadata); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.