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
d2205167
Unverified
Commit
d2205167
authored
Oct 11, 2016
by
Armando Lüscher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move tryMention to the Entity class.
Rename stripMarkDown to escapeMarkdown to give it more sense.
parent
c268c7fd
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
61 additions
and
62 deletions
+61
-62
Chat.php
src/Entities/Chat.php
+14
-21
Entity.php
src/Entities/Entity.php
+43
-3
User.php
src/Entities/User.php
+0
-33
UserTest.php
tests/unit/Entities/UserTest.php
+4
-5
No files found.
src/Entities/Chat.php
View file @
d2205167
...
@@ -34,32 +34,25 @@ class Chat extends Entity
...
@@ -34,32 +34,25 @@ class Chat extends Entity
{
{
parent
::
__construct
(
$data
);
parent
::
__construct
(
$data
);
if
(
!
$this
->
getType
())
{
$id
=
$this
->
getId
();
if
(
$this
->
getId
()
>
0
)
{
$type
=
$this
->
getType
();
$this
->
type
=
'private'
;
if
(
!
$type
&&
$id
!==
0
)
{
}
elseif
(
$this
->
getId
()
<
0
)
{
$id
>
0
&&
$this
->
type
=
'private'
;
$this
->
type
=
'group'
;
$id
<
0
&&
$this
->
type
=
'group'
;
}
}
}
}
}
/**
/**
* Try mention
* Try to mention the user of this chat, else return the title
*
* @param bool $escape_markdown
*
*
* @return string|null
* @return string|null
*/
*/
public
function
tryMention
()
public
function
tryMention
(
$escape_markdown
=
false
)
{
{
if
(
$this
->
isPrivateChat
())
{
if
(
$this
->
isPrivateChat
())
{
if
(
$this
->
username
===
null
)
{
return
$this
->
tryMention
(
$escape_markdown
);
if
(
$this
->
last_name
!==
null
)
{
return
$this
->
first_name
.
' '
.
$this
->
last_name
;
}
return
$this
->
first_name
;
}
return
'@'
.
$this
->
username
;
}
}
return
$this
->
getTitle
();
return
$this
->
getTitle
();
...
@@ -72,7 +65,7 @@ class Chat extends Entity
...
@@ -72,7 +65,7 @@ class Chat extends Entity
*/
*/
public
function
isGroupChat
()
public
function
isGroupChat
()
{
{
return
$this
->
type
===
'group'
||
$this
->
id
<
0
;
return
$this
->
getType
()
===
'group'
||
$this
->
getId
()
<
0
;
}
}
/**
/**
...
@@ -82,7 +75,7 @@ class Chat extends Entity
...
@@ -82,7 +75,7 @@ class Chat extends Entity
*/
*/
public
function
isPrivateChat
()
public
function
isPrivateChat
()
{
{
return
$this
->
type
===
'private'
;
return
$this
->
getType
()
===
'private'
;
}
}
/**
/**
...
@@ -92,7 +85,7 @@ class Chat extends Entity
...
@@ -92,7 +85,7 @@ class Chat extends Entity
*/
*/
public
function
isSuperGroup
()
public
function
isSuperGroup
()
{
{
return
$this
->
type
===
'supergroup'
;
return
$this
->
getType
()
===
'supergroup'
;
}
}
/**
/**
...
@@ -102,6 +95,6 @@ class Chat extends Entity
...
@@ -102,6 +95,6 @@ class Chat extends Entity
*/
*/
public
function
isChannel
()
public
function
isChannel
()
{
{
return
$this
->
type
===
'channel'
;
return
$this
->
getType
()
===
'channel'
;
}
}
}
}
src/Entities/Entity.php
View file @
d2205167
...
@@ -268,14 +268,13 @@ abstract class Entity
...
@@ -268,14 +268,13 @@ abstract class Entity
}
}
/**
/**
* stripMarkDown
* Escape markdown special characters
* Gived a string escape special charactes used in Markdown
*
*
* @param string $string
* @param string $string
*
*
* @return string
* @return string
*/
*/
public
function
stripMarkD
own
(
$string
)
public
function
escapeMarkd
own
(
$string
)
{
{
return
str_replace
(
return
str_replace
(
[
'['
,
'`'
,
'*'
,
'_'
,],
[
'['
,
'`'
,
'*'
,
'_'
,],
...
@@ -283,4 +282,45 @@ abstract class Entity
...
@@ -283,4 +282,45 @@ abstract class Entity
$string
$string
);
);
}
}
/**
* Try to mention the user
*
* Mention the user with the username otherwise print first and last name
* if the $escape_markdown argument is true special characters are escaped from the output
*
* @param bool $escape_markdown
*
* @return string|null
*/
public
function
tryMention
(
$escape_markdown
=
false
)
{
//TryMention only makes sense for the User and Chat entity.
if
(
!
(
$this
instanceof
User
||
$this
instanceof
Chat
))
{
return
null
;
}
//Try with the username first...
$username
=
$this
->
getProperty
(
'username'
);
if
(
$username
!==
null
)
{
if
(
$escape_markdown
)
{
$username
=
$this
->
escapeMarkdown
(
$username
);
}
return
'@'
.
$username
;
}
//...otherwise try with the names
$name
=
$this
->
getProperty
(
'first_name'
);
$last_name
=
$this
->
getProperty
(
'last_name'
);
if
(
$last_name
!==
null
)
{
$name
.=
' '
.
$last_name
;
}
if
(
$escape_markdown
)
{
$name
=
$this
->
escapeMarkdown
(
$name
);
}
return
$name
;
}
}
}
src/Entities/User.php
View file @
d2205167
...
@@ -27,38 +27,5 @@ namespace Longman\TelegramBot\Entities;
...
@@ -27,38 +27,5 @@ namespace Longman\TelegramBot\Entities;
*/
*/
class
User
extends
Entity
class
User
extends
Entity
{
{
/**
* tryMention
*
* Mention the user with the username otherwise print first and last name
* if the $markdown arguments is true special characters are escaped from the output
*
* @param bool $markdown
*
* @return string
*/
public
function
tryMention
(
$markdown
=
false
)
{
$username
=
$this
->
getProperty
(
'username'
);
if
(
$username
!==
null
)
{
if
(
$markdown
)
{
//Escaping md special characters
//Please notice that just the _ is allowed in the username ` * [ are not allowed
return
'@'
.
$this
->
stripMarkDown
(
$this
->
username
);
}
return
'@'
.
$this
->
username
;
}
$name
=
$this
->
getProperty
(
'first_name'
);
$last_name
=
$this
->
getProperty
(
'last_name'
);
if
(
$last_name
!==
null
)
{
$name
.=
' '
.
$last_name
;
}
if
(
$markdown
)
{
//Escaping md special characters
return
$this
->
stripMarkDown
(
$name
);
}
return
$name
;
}
}
}
tests/unit/Entities/UserTest.php
View file @
d2205167
...
@@ -48,12 +48,8 @@ class UserTest extends TestCase
...
@@ -48,12 +48,8 @@ class UserTest extends TestCase
self
::
assertEquals
(
'John Taylor'
,
$user
->
tryMention
());
self
::
assertEquals
(
'John Taylor'
,
$user
->
tryMention
());
}
}
public
function
test
StripMarkD
own
()
public
function
test
EscapeMarkd
own
()
{
{
// Plain stripMarkDown functionality.
$user
=
new
User
([
'id'
=>
1
,
'first_name'
=>
'John'
,
'last_name'
=>
'Taylor'
]);
self
::
assertEquals
(
'\`\[\*\_'
,
$user
->
stripMarkDown
(
'`[*_'
));
// Username.
// Username.
$user
=
new
User
([
'id'
=>
1
,
'first_name'
=>
'John'
,
'last_name'
=>
'Taylor'
,
'username'
=>
'j_taylor'
]);
$user
=
new
User
([
'id'
=>
1
,
'first_name'
=>
'John'
,
'last_name'
=>
'Taylor'
,
'username'
=>
'j_taylor'
]);
self
::
assertEquals
(
'@j_taylor'
,
$user
->
tryMention
());
self
::
assertEquals
(
'@j_taylor'
,
$user
->
tryMention
());
...
@@ -68,6 +64,9 @@ class UserTest extends TestCase
...
@@ -68,6 +64,9 @@ class UserTest extends TestCase
$user
=
new
User
([
'id'
=>
1
,
'first_name'
=>
'John'
,
'last_name'
=>
'`Taylor`'
]);
$user
=
new
User
([
'id'
=>
1
,
'first_name'
=>
'John'
,
'last_name'
=>
'`Taylor`'
]);
self
::
assertEquals
(
'John `Taylor`'
,
$user
->
tryMention
());
self
::
assertEquals
(
'John `Taylor`'
,
$user
->
tryMention
());
self
::
assertEquals
(
'John \`Taylor\`'
,
$user
->
tryMention
(
true
));
self
::
assertEquals
(
'John \`Taylor\`'
,
$user
->
tryMention
(
true
));
// Plain escapeMarkdown functionality.
self
::
assertEquals
(
'a\`b\[c\*d\_e'
,
$user
->
escapeMarkdown
(
'a`b[c*d_e'
));
}
}
public
function
testGetProperties
()
public
function
testGetProperties
()
...
...
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