Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
TelegramBot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
TelegramBot
Commits
a7113c99
Commit
a7113c99
authored
Aug 23, 2016
by
Armando Lüscher
Committed by
GitHub
Aug 23, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #273 from noplanman/improve_inline_keyboard_button
Improve inline keyboard button
parents
632a4d08
bb8699ef
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
10 deletions
+74
-10
InlineKeyboardButton.php
src/Entities/InlineKeyboardButton.php
+16
-10
InlineKeyboardButtonTest.php
tests/Unit/Entities/InlineKeyboardButtonTest.php
+58
-0
No files found.
src/Entities/InlineKeyboardButton.php
View file @
a7113c99
...
...
@@ -14,32 +14,38 @@ use Longman\TelegramBot\Exception\TelegramException;
class
InlineKeyboardButton
extends
Entity
{
/**#@+
* @var string
*/
protected
$text
;
protected
$url
;
protected
$callback_data
;
protected
$switch_inline_query
;
/**#@-*/
/**
* InlineKeyboardButton constructor.
*
* @todo check if only one of 'url, callback_data, switch_inline_query' fields is set, documentation states that only one of these can be used
*
* @param array $data
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
function
__construct
(
$data
=
array
()
)
public
function
__construct
(
array
$data
=
[]
)
{
$this
->
text
=
isset
(
$data
[
'text'
])
?
$data
[
'text'
]
:
null
;
if
(
empty
(
$this
->
text
))
{
throw
new
TelegramException
(
'text is empty!'
);
}
$this
->
url
=
isset
(
$data
[
'url'
])
?
$data
[
'url'
]
:
null
;
$this
->
callback_data
=
isset
(
$data
[
'callback_data'
])
?
$data
[
'callback_data'
]
:
null
;
$this
->
switch_inline_query
=
isset
(
$data
[
'switch_inline_query'
])
?
$data
[
'switch_inline_query'
]
:
null
;
if
(
$this
->
url
===
''
&&
$this
->
callback_data
===
''
&&
$this
->
switch_inline_query
===
''
)
{
throw
new
TelegramException
(
'You must use at least one of these fields: url, callback_data, switch_inline_query!'
);
$num_params
=
0
;
foreach
([
'url'
,
'callback_data'
,
'switch_inline_query'
]
as
$param
)
{
if
(
!
empty
(
$data
[
$param
]))
{
$this
->
$param
=
$data
[
$param
];
$num_params
++
;
}
}
if
(
$num_params
!==
1
)
{
throw
new
TelegramException
(
'You must use only one of these fields: url, callback_data, switch_inline_query!'
);
}
}
}
tests/Unit/Entities/InlineKeyboardButtonTest.php
0 → 100644
View file @
a7113c99
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace
Tests\Unit
;
use
Longman\TelegramBot\Entities\InlineKeyboardButton
;
use
Longman\TelegramBot\Exception\TelegramException
;
/**
* @package TelegramTest
* @author Avtandil Kikabidze <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
*/
class
InlineKeyboardButtonTest
extends
TestCase
{
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage text is empty!
*/
public
function
testInlineKeyboardButtonNoTextFail
()
{
new
InlineKeyboardButton
([]);
}
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query!
*/
public
function
testInlineKeyboardButtonNoParameterFail
()
{
new
InlineKeyboardButton
([
'text'
=>
'message'
]);
}
/**
* @expectedException \Longman\TelegramBot\Exception\TelegramException
* @expectedExceptionMessage You must use only one of these fields: url, callback_data, switch_inline_query!
*/
public
function
testInlineKeyboardButtonTooManyParametersFail
()
{
new
InlineKeyboardButton
([
'text'
=>
'message'
,
'url'
=>
'url_value'
,
'callback_data'
=>
'callback_data_value'
]);
}
public
function
testInlineKeyboardButtonSuccess
()
{
new
InlineKeyboardButton
([
'text'
=>
'message'
,
'url'
=>
'url_value'
]);
new
InlineKeyboardButton
([
'text'
=>
'message'
,
'callback_data'
=>
'callback_data_value'
]);
new
InlineKeyboardButton
([
'text'
=>
'message'
,
'switch_inline_query'
=>
'switch_inline_query_value'
]);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment