Passed
Push — master ( 9c7b82...f78b46 )
by Shahrad
02:20
created

KeyboardButton   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 80
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 3 1
A couldBe() 0 3 1
A subEntities() 0 5 1
A WebApp() 0 5 1
A validate() 0 4 2
A __construct() 0 7 2
A __call() 0 7 2
1
<?php
2
3
4
namespace TelegramBot\Entities;
5
6
use TelegramBot\Entity;
7
use TelegramBot\Exception\TelegramException;
8
9
/**
10
 * Class KeyboardButton
11
 *
12
 * This object represents one button of the reply keyboard. For simple text buttons String can be used instead of this object to specify text of the button. Optional fields request_contact, request_location, and request_poll are mutually exclusive.
13
 *
14
 * @link https://core.telegram.org/bots/api#keyboardbutton
15
 *
16
 * @property bool $request_contact
17
 * @property bool $request_location
18
 * @property KeyboardButtonPollType $request_poll
19
 * @property WebAppInfo $web_app
20
 *
21
 *
22
 * @method string                 getText()            	Text of the button. If none of the optional fields are used, it will be sent to the bot as a message when the button is pressed
23
 * @method bool                   getRequestContact()  	Optional. If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only
24
 * @method bool                   getRequestLocation() 	Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only
25
 * @method KeyboardButtonPollType getRequestPoll()     	Optional. If specified, the user will be asked to create a poll and send it to the bot when the button is pressed. Available in private chats only
26
 * @method WebAppInfo             getWebApp()          	Optional. If specified, the described Web App will be launched when the button is pressed. The Web App will be able to send a “web_app_data” service message. Available in private chats only.
27
 *
28
 * @method $this setText(string $text)                                	Text of the button. If none of the optional fields are used, it will be sent to the bot as a message when the button is pressed
29
 * @method $this setRequestContact(bool $request_contact)             	Optional. If True, the user's phone number will be sent as a contact when the button is pressed. Available in private chats only
30
 * @method $this setRequestLocation(bool $request_location)           	Optional. If True, the user's current location will be sent when the button is pressed. Available in private chats only
31
 * @method $this setRequestPoll(KeyboardButtonPollType $request_poll) 	Optional. If specified, the user will be asked to create a poll and send it to the bot when the button is pressed. Available in private chats only
32
 * @method $this setWebApp(WebAppInfo $web_app)                       	Optional. If specified, the described Web App will be launched when the button is pressed. The Web App will be able to send a “web_app_data” service message. Available in private chats only.
33
 */
34
class KeyboardButton extends Entity
35
{
36
37
	/**
38
	 * @param array|string $data
39
	 */
40
	public function __construct($data)
41
	{
42
		if (is_string($data)) {
43
			$data = ['text' => $data];
44
		}
45
46
		parent::__construct($data);
47
	}
48
49
	/**
50
	 * Creates instance of KeyboardButton
51
	 *
52
	 * @param string $string
53
	 * @return KeyboardButton
54
	 */
55
	public static function make(string $string): KeyboardButton
56
	{
57
		return new self($string);
58
	}
59
60
	/**
61
	 * @param string $url
62
	 * @return $this
63
	 */
64
	public function WebApp(string $url): KeyboardButton
65
	{
66
		$this->raw_data['web_app'] = new WebAppInfo(['url' => $url]);
67
68
		return $this;
69
	}
70
71
	/**
72
	 * @inheritDoc
73
	 */
74
	protected function subEntities(): array
75
	{
76
		return [
77
			'request_poll' => KeyboardButtonPollType::class,
78
			'web_app' => WebAppInfo::class,
79
		];
80
	}
81
82
	/**
83
	 * Check if the passed data array could be a KeyboardButton.
84
	 *
85
	 * @param array $data
86
	 *
87
	 * @return bool
88
	 */
89
	public static function couldBe(array $data): bool
90
	{
91
		return array_key_exists('text', $data);
92
	}
93
94
	/**
95
	 * {@inheritdoc}
96
	 */
97
	protected function validate(): void
98
	{
99
		if ($this->getProperty('text', '') === '') {
100
			throw new TelegramException('You must add some text to the button!');
101
		}
102
	}
103
104
	/**
105
	 * {@inheritdoc}
106
	 */
107
	public function __call(string $name, array $arguments): mixed
108
	{
109
		if (in_array($name, ['setRequestContact', 'setRequestLocation', 'setRequestPoll', 'setWebApp'], true)) {
110
			unset($this->request_contact, $this->request_location, $this->request_poll, $this->web_app);
111
		}
112
113
		return parent::__call($name, $arguments);
114
	}
115
116
}
117