Completed
Push — master ( 4d7d7c...67f5f0 )
by Алексей
04:57
created

BaseType::toCamelCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
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 Illuminate\Support\Collection;
13
14
/**
15
 * https://core.telegram.org/bots/api#available-types
16
 * Class BaseType
17
 * @package Telegram\Base
18
 */
19
class BaseType
20
{
21
    /**
22
     * @var Collection
23
     */
24
    protected $attributes;
25
26
    /**
27
     * @var array
28
     */
29
    protected $map = [];
30
31
    /**
32
     * BaseType constructor.
33
     *
34
     * @param $attributes
35
     */
36 2
    public function __construct($attributes)
37
    {
38 2
        $this->attributes = $this->build($attributes);
39 2
    }
40
41
    /**
42
     * @param array $attributes
43
     *
44
     * @return Collection
45
     */
46 2
    private function build(array $attributes = [])
47
    {
48 2
        $map = $this->map;
49
50 2
        return collect($attributes)
51 2
            ->mapWithKeys(function ($item, $key) use ($map) {
52 2
                if (is_array($item) && $className = array_get($map, $key)) {
53
                    return new $className($item);
54
                }
55
56 2
                return $item;
57 2
            });
58
    }
59
60
    /**
61
     * @param $json
62
     *
63
     * @return static
64
     */
65 1
    public static function create($json)
66
    {
67 1
        return new static(is_array($json) ? $json : json_decode($json, true));
68
    }
69
}