|
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
|
115 |
|
public static function validate($data) |
|
37
|
|
|
{ |
|
38
|
115 |
|
if (count(array_intersect_key(array_flip(static::$requiredParams), $data)) === count(static::$requiredParams)) { |
|
39
|
89 |
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
26 |
|
throw new InvalidArgumentException(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
88 |
|
/** |
|
46
|
|
|
* @param array $data |
|
47
|
88 |
|
* @return void |
|
48
|
88 |
|
*/ |
|
49
|
88 |
|
public function map($data) |
|
50
|
88 |
|
{ |
|
51
|
88 |
|
foreach (static::$map as $key => $item) { |
|
52
|
88 |
|
if (isset($data[$key]) && (!is_array($data[$key]) || !empty($data[$key]))) { |
|
53
|
28 |
|
$method = 'set' . self::toCamelCase($key); |
|
54
|
|
|
if ($item === true) { |
|
55
|
88 |
|
$this->$method($data[$key]); |
|
56
|
88 |
|
} else { |
|
57
|
88 |
|
$this->$method($item::fromResponse($data[$key])); |
|
58
|
|
|
} |
|
59
|
102 |
|
} |
|
60
|
|
|
} |
|
61
|
102 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
16 |
|
* @param string $str |
|
65
|
|
|
* @return string |
|
66
|
16 |
|
*/ |
|
67
|
|
|
protected static function toCamelCase($str) |
|
68
|
16 |
|
{ |
|
69
|
16 |
|
return str_replace(' ', '', ucwords(str_replace('_', ' ', $str))); |
|
70
|
16 |
|
} |
|
71
|
16 |
|
|
|
72
|
5 |
|
/** |
|
73
|
5 |
|
* @param bool $inner |
|
74
|
5 |
|
* @return array|string |
|
75
|
5 |
|
*/ |
|
76
|
5 |
|
public function toJson($inner = false) |
|
77
|
5 |
|
{ |
|
78
|
5 |
|
$output = []; |
|
79
|
14 |
|
|
|
80
|
|
|
foreach (static::$map as $key => $item) { |
|
81
|
16 |
|
$property = lcfirst(self::toCamelCase($key)); |
|
82
|
16 |
|
if (!is_null($this->$property)) { |
|
83
|
|
|
if (is_array($this->$property)) { |
|
84
|
16 |
|
$output[$key] = array_map( |
|
85
|
|
|
/** |
|
86
|
|
|
* @param mixed $v |
|
87
|
113 |
|
* @return array|false|string |
|
88
|
|
|
*/ |
|
89
|
113 |
|
function ($v) { |
|
90
|
|
|
return is_object($v) ? $v->toJson(true) : $v; |
|
91
|
|
|
}, |
|
92
|
|
|
$this->$property |
|
93
|
113 |
|
); |
|
94
|
88 |
|
} else { |
|
95
|
88 |
|
$output[$key] = $item === true ? $this->$property : $this->$property->toJson(true); |
|
96
|
|
|
} |
|
97
|
88 |
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $inner === false ? json_encode($output) : $output; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param array $data |
|
105
|
|
|
* @return static |
|
106
|
|
|
* @throws InvalidArgumentException |
|
107
|
|
|
*/ |
|
108
|
|
|
public static function fromResponse($data) |
|
109
|
|
|
{ |
|
110
|
|
|
self::validate($data); |
|
111
|
|
|
/** @psalm-suppress UnsafeInstantiation */ |
|
112
|
|
|
$instance = new static(); |
|
113
|
|
|
$instance->map($data); |
|
114
|
|
|
|
|
115
|
|
|
return $instance; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|