Completed
Push — master ( 0d7a9a...ac7eed )
by Asmir
07:43
created

PropertyMetadata::serialize()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 1

Importance

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