Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Entities/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,21 @@ protected function makePrettyObjectArray($class, $property)

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
);
}
}
35 changes: 27 additions & 8 deletions src/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,37 @@
class User extends Entity
{
/**
* Try mention
* tryMention
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be great if the description were more... descriptive 😉

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

*
* @return string|null
* 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()
public function tryMention($markdown = false)
{
if ($this->username === null) {
if ($this->last_name !== null) {
return $this->first_name . ' ' . $this->last_name;
$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->first_name;
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 '@' . $this->username;
return $name;
}
}
73 changes: 73 additions & 0 deletions tests/unit/Entities/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* This file is part of the TelegramBot package.
*
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Longman\TelegramBot\Tests\Unit;

use \Longman\TelegramBot\Entities\User;

/**
* @package TelegramTest
* @author Avtandil Kikabidze <[email protected]>
* @copyright Avtandil Kikabidze <[email protected]>
* @license http://opensource.org/licenses/mit-license.php The MIT License (MIT)
* @link http://www.github.com/akalongman/php-telegram-bot
*/
class UserTest extends TestCase
{
/**
* @var \Longman\TelegramBot\Entities\User
*/
private $user;

public function testUsername()
{
$this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'jtaylor']);
$this->assertEquals('@jtaylor', $this->user->tryMention());
}

public function testFirstName()
{
$this->user = new User(['id' => 1, 'first_name' => 'John']);
$this->assertEquals('John', $this->user->tryMention());
}

public function testLastName()
{
$this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor']);
$this->assertEquals('John Taylor', $this->user->tryMention());
}

public function testStripMarkDown()
{
$this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor']);
$this->assertEquals('\`\[\*\_', $this->user->stripMarkDown('`[*_'));
}

public function testUsernameMarkdown()
{
$this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'j_taylor']);
$this->assertEquals('@j_taylor', $this->user->tryMention());
$this->assertEquals('@j\_taylor', $this->user->tryMention(true));
}

public function testFirstNameMarkdown()
{
$this->user = new User(['id' => 1, 'first_name' => 'John[']);
$this->assertEquals('John[', $this->user->tryMention());
$this->assertEquals('John\[', $this->user->tryMention(true));
}

public function testLastNameMarkdown()
{
$this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => '`Taylor`']);
$this->assertEquals('John `Taylor`', $this->user->tryMention());
$this->assertEquals('John \`Taylor\`', $this->user->tryMention(true));
}
}