BaseType::build()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 1
nop 1
crap 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: akeinhell
5
 * Date: 27.06.16
6
 * Time: 11:58
7
 */
8
9
namespace Telegram\Base;
10
11
12
use Carbon\Carbon;
13
use Illuminate\Support\Collection;
14
15
/**
16
 * https://core.telegram.org/bots/api#available-types
17
 * Class BaseType
18
 * @package Telegram\Base
19
 */
20
class BaseType
21
{
22
    /**
23
     * @var Collection
24
     */
25
    protected $attributes;
26
27
    /**
28
     * @var array
29
     */
30
    protected $map = [];
31
32
    /**
33
     * BaseType constructor.
34
     *
35
     * @param $attributes
36
     */
37 6
    public function __construct($attributes)
38
    {
39 6
        $this->attributes = $this->build($attributes);
40 6
    }
41
42
    /**
43
     * @param array $attributes
44
     *
45
     * @return Collection
46
     */
47 6
    private function build(array $attributes = [])
48
    {
49 6
        $map = $this->map;
50
51 6
        return collect($attributes)
52
            ->mapWithKeys(function ($item, $key) use ($map) {
53 5
                $keyData = array_get($map, $key);
54 5
                if (is_callable($keyData)) {
55 4
                    return [$key => call_user_func($keyData, $item)];
56
                }
57
58 5
                if (class_exists($keyData)) {
59 4
                    return [$key => new $keyData($item)];
60
                }
61
62 5
                return [$key => $item];
63 6
            });
64
    }
65
66
    /**
67
     * @param array|string $json
68
     *
69
     * @return static
70
     */
71 3
    public static function create($json)
72
    {
73 3
        return new static(is_array($json) ? $json : json_decode($json, true));
74
    }
75
76 3
    public function get($key)
77
    {
78 3
        return $this->attributes->get($key);
79
    }
80
81 1
    public function toArray()
82
    {
83 1
        return $this->attributes
84 1
            ->mapWithKeys(function ($item, $key) {
85 1
                if (is_subclass_of($item, BaseType::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Telegram\Base\BaseType::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
86
                    /** @var BaseType $item */
87 1
                    return [$key => $item->toArray()];
88
                }
89
90 1
                if (is_object($item) && (get_class($item) === Carbon::class)) {
91
                    /** @var $item Carbon */
92 1
                    return [$key => $item->timestamp];
93
                }
94
95 1
                return [$key => $item];
96 1
            })
97 1
            ->toArray();
98
    }
99
100 1
    public function toJson()
101
    {
102 1
        return json_encode($this->toArray());
103
    }
104
105 3
    public function __call($name, $arguments)
106
    {
107 3
        $prefix   = substr($name, 0, 3);
108 3
        $property = snake_case(substr($name, 3));
109 3
        if ($prefix === 'get' && $this->attributes->has($property)) {
110 2
            return $this->get($property);
111
        }
112 1
        throw new \InvalidArgumentException();
113
    }
114
}