Completed
Pull Request — master (#139)
by
unknown
04:18
created

MarketModel::toArrayRecursive()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 20
Code Lines 13

Duplication

Lines 7
Ratio 35 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 20
ccs 0
cts 20
cp 0
rs 7.7778
cc 8
eloc 13
nc 6
nop 1
crap 72
1
<?php
2
3
namespace Yandex\Market\Partner\Models;
4
5
abstract class MarketModel
6
{
7
8
    protected $mappingClasses = [];
9
10
    /**
11
     * Constructor
12
     *
13
     * @param array $data
14
     */
15
    public function __construct($data = [])
16
    {
17
        $this->fromArray($data);
18
    }
19
20
    /**
21
     * Set from array
22
     *
23
     * @param array $data
24
     * @return $this
25
     */
26
    public function fromArray($data)
27
    {
28
        foreach ($data as $key => $val) {
29
            if (is_int($key)) {
30
                if (method_exists($this, "add")) {
31
                    $this->add($val);
0 ignored issues
show
Bug introduced by
The method add() does not seem to exist on object<Yandex\Market\Partner\Models\MarketModel>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
                }
33
            }
34
        
35
            if (property_exists($this, $key)) {
36
                if (isset($this->mappingClasses[$key])) {
37
                    $this->{$key} = new $this->mappingClasses[$key]($val);
38
                    if (method_exists($this->{$key}, "getAll")) {
39
                        $this->{$key} = $this->{$key}->getAll();
40
                    }
41
                } else {
42
                    $this->{$key} = $val;
43
                }
44
            }
45
        }
46
        return $this;
47
    }
48
49
    /**
50
     * Set from json
51
     *
52
     * @param string $json
53
     * @return $this
54
     */
55
    public function fromJson($json)
56
    {
57
        $this->fromArray(json_decode($json, true));
58
        return $this;
59
    }
60
61
    /**
62
     * Get array from object
63
     *
64
     * @return array
65
     */
66
    public function toArray()
67
    {
68
        return $this->toArrayRecursive($this);
69
    }
70
71
    /**
72
     * Get array from object
73
     *
74
     * @return string
75
     */
76
    public function toJson()
77
    {
78
        return json_encode($this->toArrayRecursive($this));
79
    }
80
81
    /**
82
     * Get array from object
83
     *
84
     * @param array|object $data
85
     * @return array
86
     */
87
    protected function toArrayRecursive($data)
88
    {
89
        if (is_array($data) || is_object($data)) {
90
            $result = [];
91
            foreach ($data as $key => $value) {
92
                if ($key === "mappingClasses") {
93
                    continue;
94
                }
95 View Code Duplication
                if (is_object($value) && method_exists($value, "getAll")) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
                    $result[$key] = $this->toArrayRecursive($value->getAll());
97
                } else {
98
                    if ($value !== null) {
99
                        $result[$key] = $this->toArrayRecursive($value);
100
                    }
101
                }
102
            }
103
            return $result;
104
        }
105
        return $data;
106
    }
107
}
108