Completed
Pull Request — master (#447)
by Ivan
07:22
created

DeserializeException::__construct()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 6
eloc 13
nc 14
nop 3
1
<?php
2
3
/*
4
 * Copyright 2015 Ivan Borzenkov <[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\Exception;
20
use JMS\Serializer\Context;
21
use JMS\Serializer\Metadata;
22
23
/**
24
 * DeserializeException for the Serializer.
25
 *
26
 * @author Ivan Borzenkov <[email protected]>
27
 */
28
class DeserializeException extends RuntimeException
29
{
30
    public $context;
31
    public $type;
32
    public $data;
33
    public $path;
34
35
    /**
36
     * @param array   $type
37
     * @param mixed   $data
38
     * @param Context $context
39
     */
40
    public function __construct($type, $data, Context $context)
41
    {
42
        $this->context = $context;
43
        $this->type = $type;
44
        $this->data = $data;
45
        $this->path = '';
46
        foreach ($context->getMetadataStack() as $element) {
47
            if ($element instanceof Metadata\IndexMetadata) {
48
                $this->path = '['.$element->index.']'.$this->path;
49
            }
50
            if ($element instanceof Metadata\PropertyMetadata) {
51
                $this->path = '.'.($element->serializedName ?: $element->name).$this->path;
52
            }
53
        }
54
        $this->path = trim($this->path, '.') ?: '.';
55
56
        $message = sprintf('Path "%s": expected %s, but got %s: %s', $this->path, $type['name'], gettype($data), json_encode($data));
57
        parent::__construct($message, 0, null);
58
    }
59
60
}
61