|
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)) { |
|
|
|
|
|
|
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
|
|
|
} |