Completed
Pull Request — master (#787)
by
unknown
03:32
created

PropertyMetadata::setValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * Copyright 2016 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\Exception\RuntimeException;
22
use JMS\Serializer\TypeParser;
23
use Metadata\PropertyMetadata as BasePropertyMetadata;
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 $deserializeType;
36
    public $xmlCollection = false;
37
    public $xmlCollectionInline = false;
38
    public $xmlCollectionSkipWhenEmpty = true;
39
    public $xmlEntryName;
40
    public $xmlEntryNamespace;
41
    public $xmlKeyAttribute;
42
    public $xmlAttribute = false;
43
    public $xmlValue = false;
44
    public $xmlNamespace;
45
    public $xmlKeyValuePairs = false;
46
    public $xmlElementCData = true;
47
    public $getter;
48
    public $setter;
49
    public $inline = false;
50
    public $skipWhenEmpty = false;
51
    public $readOnly = false;
52
    public $xmlAttributeMap = false;
53
    public $maxDepth = null;
54
    public $excludeIf = null;
55
56
    private static $typeParser;
57
58 283
    public function setAccessor($type, $getter = null, $setter = null)
59
    {
60 283
        if (self::ACCESS_TYPE_PUBLIC_METHOD === $type) {
61 13
            $class = $this->reflection->getDeclaringClass();
62
63 13
            if (empty($getter)) {
64 9
                if ($class->hasMethod('get' . $this->name) && $class->getMethod('get' . $this->name)->isPublic()) {
65 5
                    $getter = 'get' . $this->name;
66 9
                } elseif ($class->hasMethod('is' . $this->name) && $class->getMethod('is' . $this->name)->isPublic()) {
67 1
                    $getter = 'is' . $this->name;
68 4
                } elseif ($class->hasMethod('has' . $this->name) && $class->getMethod('has' . $this->name)->isPublic()) {
69 1
                    $getter = 'has' . $this->name;
70 1
                } else {
71 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));
72
                }
73 7
            }
74
75 11
            if (empty($setter) && !$this->readOnly) {
76 7
                if ($class->hasMethod('set' . $this->name) && $class->getMethod('set' . $this->name)->isPublic()) {
77 6
                    $setter = 'set' . $this->name;
78 6
                } else {
79 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));
80
                }
81 6
            }
82 10
        }
83
84 280
        $this->getter = $getter;
85 280
        $this->setter = $setter;
86 280
    }
87
88 199
    public function getValue($obj)
89
    {
90 199
        if (null === $this->getter) {
91 188
            return parent::getValue($obj);
92
        }
93
94 38
        return $obj->{$this->getter}();
95
    }
96
97 55
    public function setValue($obj, $value)
98
    {
99 55
        if (null === $this->setter) {
100 50
            parent::setValue($obj, $value);
101 50
            return;
102
        }
103
104 7
        $obj->{$this->setter}($value);
105 7
    }
106
107 243
    public function setType($type)
108
    {
109 243
        if (null === self::$typeParser) {
110 1
            self::$typeParser = new TypeParser();
111 1
        }
112
113 243
        $this->type = self::$typeParser->parse($type);
114 243
    }
115
116 11
    public function setDeserializeType($type)
117
    {
118 11
        if (null === self::$typeParser) {
119
            self::$typeParser = new TypeParser();
120
        }
121
122 11
        $this->deserializeType = self::$typeParser->parse($type);
123 11
    }
124
125 1
    public function serialize()
126
    {
127 1
        return serialize(array(
128 1
            $this->sinceVersion,
129 1
            $this->untilVersion,
130 1
            $this->groups,
131 1
            $this->serializedName,
132 1
            $this->type,
133 1
            $this->deserializeType,
134 1
            $this->xmlCollection,
135 1
            $this->xmlCollectionInline,
136 1
            $this->xmlEntryName,
137 1
            $this->xmlKeyAttribute,
138 1
            $this->xmlAttribute,
139 1
            $this->xmlValue,
140 1
            $this->xmlNamespace,
141 1
            $this->xmlKeyValuePairs,
142 1
            $this->xmlElementCData,
143 1
            $this->getter,
144 1
            $this->setter,
145 1
            $this->inline,
146 1
            $this->readOnly,
147 1
            $this->xmlAttributeMap,
148 1
            $this->maxDepth,
149 1
            parent::serialize(),
150 1
            'xmlEntryNamespace' => $this->xmlEntryNamespace,
151 1
            'xmlCollectionSkipWhenEmpty' => $this->xmlCollectionSkipWhenEmpty,
152 1
            'excludeIf' => $this->excludeIf,
153 1
            'skipWhenEmpty' => $this->skipWhenEmpty,
154 1
        ));
155
    }
156
157 1
    public function unserialize($str)
158
    {
159 1
        $unserialized = unserialize($str);
160
        list(
161 1
            $this->sinceVersion,
162 1
            $this->untilVersion,
163 1
            $this->groups,
164 1
            $this->serializedName,
165 1
            $this->type,
166 1
            $this->deserializeType,
167 1
            $this->xmlCollection,
168 1
            $this->xmlCollectionInline,
169 1
            $this->xmlEntryName,
170 1
            $this->xmlKeyAttribute,
171 1
            $this->xmlAttribute,
172 1
            $this->xmlValue,
173 1
            $this->xmlNamespace,
174 1
            $this->xmlKeyValuePairs,
175 1
            $this->xmlElementCData,
176 1
            $this->getter,
177 1
            $this->setter,
178 1
            $this->inline,
179 1
            $this->readOnly,
180 1
            $this->xmlAttributeMap,
181 1
            $this->maxDepth,
182
            $parentStr
183 1
            ) = $unserialized;
184
185 1
        if (isset($unserialized['xmlEntryNamespace'])) {
186
            $this->xmlEntryNamespace = $unserialized['xmlEntryNamespace'];
187
        }
188 1
        if (isset($unserialized['xmlCollectionSkipWhenEmpty'])) {
189 1
            $this->xmlCollectionSkipWhenEmpty = $unserialized['xmlCollectionSkipWhenEmpty'];
190 1
        }
191 1
        if (isset($unserialized['excludeIf'])) {
192
            $this->excludeIf = $unserialized['excludeIf'];
193
        }
194 1
        if (isset($unserialized['skipWhenEmpty'])) {
195 1
            $this->skipWhenEmpty = $unserialized['skipWhenEmpty'];
196 1
        }
197
198 1
        parent::unserialize($parentStr);
199 1
    }
200
}
201