Completed
Push — master ( e1c4bf...7b4b35 )
by Johannes
10s
created

PropertyMetadata   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 97.92%

Importance

Changes 12
Bugs 2 Features 3
Metric Value
wmc 23
c 12
b 2
f 3
lcom 2
cbo 3
dl 0
loc 155
ccs 94
cts 96
cp 0.9792
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
C setAccessor() 0 29 13
A getValue() 0 8 2
A setValue() 0 9 2
A setType() 0 8 2
B serialize() 0 28 1
B unserialize() 0 37 3
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 $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 $readOnly = false;
50
    public $xmlAttributeMap = false;
51
    public $maxDepth = null;
52
53
    private static $typeParser;
54
55 195
    public function setAccessor($type, $getter = null, $setter = null)
56
    {
57 195
        if (self::ACCESS_TYPE_PUBLIC_METHOD === $type) {
58 13
            $class = $this->reflection->getDeclaringClass();
59
60 13
            if (empty($getter)) {
61 9
                if ($class->hasMethod('get'.$this->name) && $class->getMethod('get'.$this->name)->isPublic()) {
62 5
                    $getter = 'get'.$this->name;
63 9
                } elseif ($class->hasMethod('is'.$this->name) && $class->getMethod('is'.$this->name)->isPublic()) {
64 1
                    $getter = 'is'.$this->name;
65 4
                } elseif ($class->hasMethod('has'.$this->name) && $class->getMethod('has'.$this->name)->isPublic()) {
66 1
                    $getter = 'has'.$this->name;
67 1
                } else {
68 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));
69
                }
70 7
            }
71
72 11
            if (empty($setter) && ! $this->readOnly) {
73 7
                if ($class->hasMethod('set'.$this->name) && $class->getMethod('set'.$this->name)->isPublic()) {
74 6
                    $setter = 'set'.$this->name;
75 6
                } else {
76 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));
77
                }
78 6
            }
79 10
        }
80
81 192
        $this->getter = $getter;
82 192
        $this->setter = $setter;
83 192
    }
84
85 138
    public function getValue($obj)
86
    {
87 138
        if (null === $this->getter) {
88 127
            return parent::getValue($obj);
89
        }
90
91 29
        return $obj->{$this->getter}();
92
    }
93
94 46
    public function setValue($obj, $value)
95
    {
96 46
        if (null === $this->setter) {
97 42
            parent::setValue($obj, $value);
98 42
            return;
99
        }
100
101 6
        $obj->{$this->setter}($value);
102 6
    }
103
104 167
    public function setType($type)
105
    {
106 167
        if (null === self::$typeParser) {
107 1
            self::$typeParser = new TypeParser();
108 1
        }
109
110 167
        $this->type = self::$typeParser->parse($type);
111 167
    }
112
113 1
    public function serialize()
114
    {
115 1
        return serialize(array(
116 1
            $this->sinceVersion,
117 1
            $this->untilVersion,
118 1
            $this->groups,
119 1
            $this->serializedName,
120 1
            $this->type,
121 1
            $this->xmlCollection,
122 1
            $this->xmlCollectionInline,
123 1
            $this->xmlEntryName,
124 1
            $this->xmlKeyAttribute,
125 1
            $this->xmlAttribute,
126 1
            $this->xmlValue,
127 1
            $this->xmlNamespace,
128 1
            $this->xmlKeyValuePairs,
129 1
            $this->xmlElementCData,
130 1
            $this->getter,
131 1
            $this->setter,
132 1
            $this->inline,
133 1
            $this->readOnly,
134 1
            $this->xmlAttributeMap,
135 1
            $this->maxDepth,
136 1
            parent::serialize(),
137 1
            'xmlEntryNamespace' => $this->xmlEntryNamespace,
138 1
            'xmlCollectionSkipWhenEmpty' => $this->xmlCollectionSkipWhenEmpty,
139 1
        ));
140
    }
141
142 1
    public function unserialize($str)
143
    {
144 1
        $unserialized = unserialize($str);
145
        list(
146 1
            $this->sinceVersion,
147 1
            $this->untilVersion,
148 1
            $this->groups,
149 1
            $this->serializedName,
150 1
            $this->type,
151 1
            $this->xmlCollection,
152 1
            $this->xmlCollectionInline,
153 1
            $this->xmlEntryName,
154 1
            $this->xmlKeyAttribute,
155 1
            $this->xmlAttribute,
156 1
            $this->xmlValue,
157 1
            $this->xmlNamespace,
158 1
            $this->xmlKeyValuePairs,
159 1
            $this->xmlElementCData,
160 1
            $this->getter,
161 1
            $this->setter,
162 1
            $this->inline,
163 1
            $this->readOnly,
164 1
            $this->xmlAttributeMap,
165 1
            $this->maxDepth,
166
            $parentStr
167 1
        ) = $unserialized;
168
169 1
        if (isset($unserialized['xmlEntryNamespace'])){
170
            $this->xmlEntryNamespace = $unserialized['xmlEntryNamespace'];
171
        }
172 1
        if (isset($unserialized['xmlCollectionSkipWhenEmpty'])){
173 1
            $this->xmlCollectionSkipWhenEmpty = $unserialized['xmlCollectionSkipWhenEmpty'];
174 1
        }
175
        
176
177 1
        parent::unserialize($parentStr);
178 1
    }
179
}
180