1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: akeinhell |
5
|
|
|
* Date: 20.11.16 |
6
|
|
|
* Time: 20:02 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Telegram\Entry; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Illuminate\Support\Collection; |
13
|
|
|
use Telegram\Base\BaseEntry; |
14
|
|
|
|
15
|
|
|
class ReplyKeyboardEntry extends BaseEntry |
16
|
|
|
{ |
17
|
|
|
private $rows; |
18
|
|
|
|
19
|
|
|
const KEYBOARD_KEY = 'keyboard'; |
20
|
|
|
|
21
|
1 |
|
public function __construct() |
22
|
|
|
{ |
23
|
1 |
|
parent::__construct(); |
24
|
1 |
|
$this->set(self::KEYBOARD_KEY, collect()); |
25
|
1 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param KeyboardButton[] $row |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
1 |
|
public function addRow(array $row) |
32
|
|
|
{ |
33
|
1 |
|
$rows = $this->get(self::KEYBOARD_KEY, collect())->push($row); |
34
|
|
|
|
35
|
1 |
|
return $this->set(self::KEYBOARD_KEY, $rows); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return mixed |
40
|
|
|
*/ |
41
|
|
|
public function getKeyboard() |
42
|
|
|
{ |
43
|
|
|
return $this->rows; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param bool $resize |
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
1 |
|
public function setResizeKeyboard($resize) |
51
|
|
|
{ |
52
|
1 |
|
return $this->set('resize_keyboard', $resize); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param bool $oneTime |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
1 |
|
public function setOneTimeKeyboard($oneTime) |
60
|
|
|
{ |
61
|
1 |
|
return $this->set('one_time_keyboard', $oneTime); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param bool $selective |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
1 |
|
public function setSelective($selective) |
69
|
|
|
{ |
70
|
1 |
|
return $this->set('selective', $selective); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
public function toArray() |
74
|
|
|
{ |
75
|
1 |
|
$rows = $this->get(self::KEYBOARD_KEY, collect()); |
76
|
1 |
|
if (!is_array($rows) && get_class($rows) == Collection::class) { |
77
|
1 |
|
$clone = clone $this; |
78
|
|
|
$rows = $clone |
79
|
1 |
|
->get(self::KEYBOARD_KEY, collect()) |
80
|
|
|
->mapWithKeys(function ($item, $key) { |
81
|
|
|
return [ |
82
|
1 |
|
$key => collect($item) |
83
|
1 |
|
->mapWithKeys(function ($item, $key) { |
84
|
|
|
/** @var $item Collection */ |
85
|
1 |
|
return [$key => $item->toArray()]; |
86
|
1 |
|
}), |
87
|
1 |
|
]; |
88
|
|
|
|
89
|
1 |
|
}) |
90
|
1 |
|
->toArray(); |
91
|
|
|
|
92
|
1 |
|
$clone->set(self::KEYBOARD_KEY, $rows); |
93
|
|
|
|
94
|
1 |
|
return $clone::toArray(); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
return json_encode(parent::toArray()); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.