|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright 2013 Johannes M. Schmitt <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace JMS\Serializer\Metadata; |
|
20
|
|
|
|
|
21
|
|
|
use JMS\Serializer\TypeParser; |
|
22
|
|
|
use Metadata\PropertyMetadata as BasePropertyMetadata; |
|
23
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
|
24
|
|
|
|
|
25
|
|
|
class PropertyMetadata extends BasePropertyMetadata |
|
26
|
|
|
{ |
|
27
|
|
|
const ACCESS_TYPE_PROPERTY = 'property'; |
|
28
|
|
|
const ACCESS_TYPE_PUBLIC_METHOD = 'public_method'; |
|
29
|
|
|
|
|
30
|
|
|
public $sinceVersion; |
|
31
|
|
|
public $untilVersion; |
|
32
|
|
|
public $groups; |
|
33
|
|
|
public $serializedName; |
|
34
|
|
|
public $type; |
|
35
|
|
|
public $xmlCollection = false; |
|
36
|
|
|
public $xmlCollectionInline = false; |
|
37
|
|
|
public $xmlEntryName; |
|
38
|
|
|
public $xmlEntryNamespace; |
|
39
|
|
|
public $xmlKeyAttribute; |
|
40
|
|
|
public $xmlAttribute = false; |
|
41
|
|
|
public $xmlValue = false; |
|
42
|
|
|
public $xmlNamespace; |
|
43
|
|
|
public $xmlKeyValuePairs = false; |
|
44
|
|
|
public $xmlElementCData = true; |
|
45
|
|
|
public $getter; |
|
46
|
|
|
public $setter; |
|
47
|
|
|
public $inline = false; |
|
48
|
|
|
public $readOnly = false; |
|
49
|
|
|
public $xmlAttributeMap = false; |
|
50
|
|
|
public $maxDepth = null; |
|
51
|
|
|
|
|
52
|
|
|
private static $typeParser; |
|
53
|
|
|
|
|
54
|
186 |
|
public function setAccessor($type, $getter = null, $setter = null) |
|
55
|
|
|
{ |
|
56
|
186 |
|
if (self::ACCESS_TYPE_PUBLIC_METHOD === $type) { |
|
57
|
13 |
|
$class = $this->reflection->getDeclaringClass(); |
|
58
|
|
|
|
|
59
|
13 |
|
if (empty($getter)) { |
|
60
|
9 |
|
if ($class->hasMethod('get'.$this->name) && $class->getMethod('get'.$this->name)->isPublic()) { |
|
61
|
5 |
|
$getter = 'get'.$this->name; |
|
62
|
9 |
|
} elseif ($class->hasMethod('is'.$this->name) && $class->getMethod('is'.$this->name)->isPublic()) { |
|
63
|
1 |
|
$getter = 'is'.$this->name; |
|
64
|
4 |
|
} elseif ($class->hasMethod('has'.$this->name) && $class->getMethod('has'.$this->name)->isPublic()) { |
|
65
|
1 |
|
$getter = 'has'.$this->name; |
|
66
|
1 |
|
} else { |
|
67
|
2 |
|
throw new RuntimeException(sprintf('There is neither a public %s method, nor a public %s method, nor a public %s method in class %s. Please specify which public method should be used for retrieving the value of the property %s.', 'get'.ucfirst($this->name), 'is'.ucfirst($this->name), 'has'.ucfirst($this->name), $this->class, $this->name)); |
|
68
|
|
|
} |
|
69
|
7 |
|
} |
|
70
|
|
|
|
|
71
|
11 |
|
if (empty($setter) && ! $this->readOnly) { |
|
72
|
7 |
|
if ($class->hasMethod('set'.$this->name) && $class->getMethod('set'.$this->name)->isPublic()) { |
|
73
|
6 |
|
$setter = 'set'.$this->name; |
|
74
|
6 |
|
} else { |
|
75
|
1 |
|
throw new RuntimeException(sprintf('There is no public %s method in class %s. Please specify which public method should be used for setting the value of the property %s.', 'set'.ucfirst($this->name), $this->class, $this->name)); |
|
76
|
|
|
} |
|
77
|
6 |
|
} |
|
78
|
10 |
|
} |
|
79
|
|
|
|
|
80
|
183 |
|
$this->getter = $getter; |
|
81
|
183 |
|
$this->setter = $setter; |
|
82
|
183 |
|
} |
|
83
|
|
|
|
|
84
|
132 |
|
public function getValue($obj) |
|
85
|
|
|
{ |
|
86
|
132 |
|
if (null === $this->getter) { |
|
87
|
121 |
|
return parent::getValue($obj); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
29 |
|
return $obj->{$this->getter}(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
46 |
|
public function setValue($obj, $value) |
|
94
|
|
|
{ |
|
95
|
46 |
|
if (null === $this->setter) { |
|
96
|
42 |
|
parent::setValue($obj, $value); |
|
97
|
42 |
|
return; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
6 |
|
$obj->{$this->setter}($value); |
|
101
|
6 |
|
} |
|
102
|
|
|
|
|
103
|
158 |
|
public function setType($type) |
|
104
|
|
|
{ |
|
105
|
158 |
|
if (null === self::$typeParser) { |
|
106
|
1 |
|
self::$typeParser = new TypeParser(); |
|
107
|
1 |
|
} |
|
108
|
|
|
|
|
109
|
158 |
|
$this->type = self::$typeParser->parse($type); |
|
110
|
158 |
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
public function serialize() |
|
113
|
|
|
{ |
|
114
|
1 |
|
return serialize(array( |
|
115
|
1 |
|
$this->sinceVersion, |
|
116
|
1 |
|
$this->untilVersion, |
|
117
|
1 |
|
$this->groups, |
|
118
|
1 |
|
$this->serializedName, |
|
119
|
1 |
|
$this->type, |
|
120
|
1 |
|
$this->xmlCollection, |
|
121
|
1 |
|
$this->xmlCollectionInline, |
|
122
|
1 |
|
$this->xmlEntryName, |
|
123
|
1 |
|
$this->xmlKeyAttribute, |
|
124
|
1 |
|
$this->xmlAttribute, |
|
125
|
1 |
|
$this->xmlValue, |
|
126
|
1 |
|
$this->xmlNamespace, |
|
127
|
1 |
|
$this->xmlKeyValuePairs, |
|
128
|
1 |
|
$this->xmlElementCData, |
|
129
|
1 |
|
$this->getter, |
|
130
|
1 |
|
$this->setter, |
|
131
|
1 |
|
$this->inline, |
|
132
|
1 |
|
$this->readOnly, |
|
133
|
1 |
|
$this->xmlAttributeMap, |
|
134
|
1 |
|
$this->maxDepth, |
|
135
|
1 |
|
parent::serialize(), |
|
136
|
1 |
|
'xmlEntryNamespace' => $this->xmlEntryNamespace, |
|
137
|
1 |
|
)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
public function unserialize($str) |
|
141
|
|
|
{ |
|
142
|
1 |
|
$unserialized = unserialize($str); |
|
143
|
|
|
list( |
|
144
|
1 |
|
$this->sinceVersion, |
|
145
|
1 |
|
$this->untilVersion, |
|
146
|
1 |
|
$this->groups, |
|
147
|
1 |
|
$this->serializedName, |
|
148
|
1 |
|
$this->type, |
|
149
|
1 |
|
$this->xmlCollection, |
|
150
|
1 |
|
$this->xmlCollectionInline, |
|
151
|
1 |
|
$this->xmlEntryName, |
|
152
|
1 |
|
$this->xmlKeyAttribute, |
|
153
|
1 |
|
$this->xmlAttribute, |
|
154
|
1 |
|
$this->xmlValue, |
|
155
|
1 |
|
$this->xmlNamespace, |
|
156
|
1 |
|
$this->xmlKeyValuePairs, |
|
157
|
1 |
|
$this->xmlElementCData, |
|
158
|
1 |
|
$this->getter, |
|
159
|
1 |
|
$this->setter, |
|
160
|
1 |
|
$this->inline, |
|
161
|
1 |
|
$this->readOnly, |
|
162
|
1 |
|
$this->xmlAttributeMap, |
|
163
|
1 |
|
$this->maxDepth, |
|
164
|
|
|
$parentStr |
|
165
|
1 |
|
) = $unserialized; |
|
166
|
|
|
|
|
167
|
1 |
|
if (isset($unserialized['xmlEntryNamespace'])){ |
|
168
|
|
|
$this->xmlEntryNamespace = $unserialized['xmlEntryNamespace']; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
1 |
|
parent::unserialize($parentStr); |
|
172
|
1 |
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|