diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index a59f12836..64b9247b7 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -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 + ); + } } diff --git a/src/Entities/User.php b/src/Entities/User.php index a4079d959..31304aa96 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -28,18 +28,37 @@ class User extends Entity { /** - * Try mention + * tryMention * - * @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; } } diff --git a/tests/unit/Entities/UserTest.php b/tests/unit/Entities/UserTest.php new file mode 100644 index 000000000..8099cfcf1 --- /dev/null +++ b/tests/unit/Entities/UserTest.php @@ -0,0 +1,73 @@ + + * + * 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 + * @copyright Avtandil Kikabidze + * @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)); + } +}