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
c816458d
Commit
c816458d
authored
Sep 29, 2016
by
MBoretto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improving try mention markdown
parent
94aa39ee
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
39 deletions
+28
-39
Entity.php
src/Entities/Entity.php
+17
-0
User.php
src/Entities/User.php
+11
-33
UserTest.php
tests/unit/Entities/UserTest.php
+0
-6
No files found.
src/Entities/Entity.php
View file @
c816458d
...
...
@@ -266,4 +266,21 @@ abstract class Entity
return
$new_objects
;
}
/**
* stripMarkDown
* Gived a string escape special charactes used in Markdown
*
* @param string $string
*
* @return string
*/
public
function
stripMarkDown
(
$string
)
{
return
str_replace
(
[
'['
,
'`'
,
'*'
,
'_'
,],
[
'\['
,
'\`'
,
'\*'
,
'\_'
,],
$string
);
}
}
src/Entities/User.php
View file @
c816458d
...
...
@@ -30,24 +30,29 @@ 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
)
{
if
(
isset
(
$this
->
username
))
{
$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
->
prependAt
(
$this
->
stripMarkDown
(
$this
->
username
)
);
return
'@'
.
$this
->
stripMarkDown
(
$this
->
username
);
}
return
$this
->
prependAt
(
$this
->
username
)
;
return
'@'
.
$this
->
username
;
}
$name
=
$this
->
first_name
;
if
(
isset
(
$this
->
last_name
))
{
$name
.=
' '
.
$this
->
last_name
;
$name
=
$this
->
getProperty
(
'first_name'
);
$last_name
=
$this
->
getProperty
(
'last_name'
);
if
(
$last_name
!==
null
)
{
$name
.=
' '
.
$last_name
;
}
if
(
$markdown
)
{
...
...
@@ -56,31 +61,4 @@ class User extends Entity
}
return
$name
;
}
/**
* stripMarkDown
*
* @param string $string
*
* @return string
*/
public
function
stripMarkDown
(
$string
)
{
$string
=
str_replace
(
'['
,
'\['
,
$string
);
$string
=
str_replace
(
'`'
,
'\`'
,
$string
);
$string
=
str_replace
(
'*'
,
'\*'
,
$string
);
return
str_replace
(
'_'
,
'\_'
,
$string
);
}
/**
* prepend@
*
* @param string $string
*
* @return string
*/
public
function
prependAt
(
$string
)
{
return
'@'
.
$string
;
}
}
tests/unit/Entities/UserTest.php
View file @
c816458d
...
...
@@ -50,12 +50,6 @@ class UserTest extends TestCase
$this
->
assertEquals
(
'\`\[\*\_'
,
$this
->
user
->
stripMarkDown
(
'`[*_'
));
}
public
function
testPrependAt
()
{
$this
->
user
=
new
User
([
'id'
=>
1
,
'first_name'
=>
'John'
,
'last_name'
=>
'Taylor'
]);
$this
->
assertEquals
(
'@string'
,
$this
->
user
->
prependAt
(
'string'
));
}
public
function
testUsernameMarkdown
()
{
$this
->
user
=
new
User
([
'id'
=>
1
,
'first_name'
=>
'John'
,
'last_name'
=>
'Taylor'
,
'username'
=>
'j_taylor'
]);
...
...
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