AbstractResource   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 92
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A makeFromArray() 0 8 1
A parseProperties() 0 6 2
A parse() 0 8 2
A isCastable() 0 4 2
A cast() 0 6 2
A setRawProperty() 0 6 1
A get() 0 8 3
A __call() 0 6 2
1
<?php
2
3
namespace ClashOfClans\Api;
4
5
use ClashOfClans\Api\Clan\Clan;
6
use ClashOfClans\Api\Location\Location;
7
8
abstract class AbstractResource
9
{
10
11
    protected $data = [];
12
13
    protected $casts = [
14
        'location' => Location::class
15
    ];
16
17
    protected function __construct()
18
    {
19
20
    }
21
22
    /**
23
     * @param array $properties
24
     * @return static
25
     */
26
    public static function makeFromArray(array $properties)
27
    {
28
        $instance = new static;
29
30
        $instance->parseProperties($properties);
31
32
        return $instance;
33
    }
34
35
    protected function parseProperties(array $properties)
36
    {
37
        foreach ($properties as $key => $value) {
38
            $this->parse($key, $value);
39
        }
40
    }
41
42
    protected function parse($key, $value)
43
    {
44
        if ($this->isCastable($key)) {
45
            return $this->cast($key, $value);
46
        }
47
48
        return $this->setRawProperty($key, $value);
49
    }
50
51
    /**
52
     * @param $key
53
     * @return bool
54
     */
55
    protected function isCastable($key)
56
    {
57
        return array_key_exists($key, $this->casts) || is_int($key);
58
    }
59
60
    /**
61
     * @param $key
62
     * @param $value
63
     * @return Clan
64
     */
65
    protected function cast($key, $value)
66
    {
67
        $class = is_int($key) ? $this->casts['all'] : $this->casts[$key];
68
69
        return $this->setRawProperty($key, $class::makeFromArray($value));
70
    }
71
72
    /**
73
     * @param $key
74
     * @param $value
75
     * @return Clan
76
     */
77
    protected function setRawProperty($key, $value)
78
    {
79
        $this->data[$key] = $value;
80
81
        return $this;
82
    }
83
84
    protected function get($key = null)
85
    {
86
        if ($key === null) {
87
            return $this->data;
88
        }
89
90
        return isset($this->data[$key]) ? $this->data[$key] : null;
91
    }
92
93
    public function __call($name, $arguments)
94
    {
95
        if ($data = $this->get($name)) {
96
            return $data;
97
        }
98
    }
99
}
100