Passed
Push — master ( d6f838...71e9c7 )
by
unknown
45s
created

BaseType::toJson()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 17
cts 17
cp 1
rs 6.9811
c 0
b 0
f 0
cc 7
eloc 13
nc 10
nop 1
crap 7
1
<?php
2
3
namespace TelegramBot\Api;
4
5
/**
6
 * Class BaseType
7
 * Base class for Telegram Types
8
 *
9
 * @package TelegramBot\Api
10
 */
11
abstract class BaseType
12
{
13
    /**
14
     * Array of required data params for type
15
     *
16
     * @var array
17
     */
18
    protected static $requiredParams = [];
19
20
    /**
21
     * Map of input data
22
     *
23
     * @var array
24
     */
25
    protected static $map = [];
26
27
    /**
28
     * Validate input data
29
     *
30
     * @param array $data
31
     *
32
     * @return bool
33
     *
34
     * @throws InvalidArgumentException
35
     */
36 99
    public static function validate($data)
37
    {
38 99
        if (count(array_intersect_key(array_flip(static::$requiredParams), $data)) === count(static::$requiredParams)) {
39 75
            return true;
40
        }
41
42 24
        throw new InvalidArgumentException();
43
    }
44
45 74
    public function map($data)
46
    {
47 74
        foreach (static::$map as $key => $item) {
48 74
            if (isset($data[$key]) && (!is_array($data[$key]) || (is_array($data[$key]) && !empty($data[$key])))) {
49 74
                $method = 'set' . self::toCamelCase($key);
50 74
                if ($item === true) {
51 74
                    $this->$method($data[$key]);
52 73
                } else {
53 25
                    $this->$method($item::fromResponse($data[$key]));
54
                }
55 73
            }
56 73
        }
57 73
    }
58
59 86
    protected static function toCamelCase($str)
60
    {
61 86
        return str_replace(" ", "", ucwords(str_replace("_", " ", $str)));
62
    }
63
64 14
    public function toJson($inner = false)
65
    {
66 14
        $output = [];
67
68 14
        foreach (static::$map as $key => $item) {
69 14
            $property = lcfirst(self::toCamelCase($key));
70 14
            if (!is_null($this->$property)) {
71 14
                if (is_array($this->$property)) {
72 5
                    $output[$key] = array_map(
73 5
                        function ($v) {
74 5
                            return is_object($v) ? $v->toJson(true) : $v;
75 5
                        },
76 5
                        $this->$property
77 5
                    );
78 5
                } else {
79 12
                    $output[$key] = $item === true ? $this->$property : $this->$property->toJson(true);
80
                }
81 14
            }
82 14
        }
83
84 14
        return $inner === false ? json_encode($output) : $output;
85
    }
86
87 97
    public static function fromResponse($data)
88
    {
89 97
        self::validate($data);
90 74
        $instance = new static();
91 74
        $instance->map($data);
92
93 73
        return $instance;
94
    }
95
}
96