From 94aa39ee842b3eabfc5e87b71e861f82ba266c70 Mon Sep 17 00:00:00 2001 From: MBoretto Date: Thu, 29 Sep 2016 00:32:03 +0200 Subject: [PATCH 1/2] introduce md tryMention --- src/Entities/User.php | 57 +++++++++++++++++++---- tests/unit/Entities/UserTest.php | 79 ++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 tests/unit/Entities/UserTest.php diff --git a/src/Entities/User.php b/src/Entities/User.php index a4079d959..65c7c137a 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -28,18 +28,59 @@ class User extends Entity { /** - * Try mention + * tryMention * - * @return string|null + * @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; + if (isset($this->username)) { + 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->first_name; + return $this->prependAt($this->username); + } + + $name = $this->first_name; + if (isset($this->last_name)) { + $name .= ' ' . $this->last_name; } - return '@' . $this->username; + + if ($markdown) { + //Escaping md special characters + return $this->stripMarkDown($name); + } + 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; } } diff --git a/tests/unit/Entities/UserTest.php b/tests/unit/Entities/UserTest.php new file mode 100644 index 000000000..2374ac8d5 --- /dev/null +++ b/tests/unit/Entities/UserTest.php @@ -0,0 +1,79 @@ + + * + * 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 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']); + $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)); + } +} From c816458d1c7e525e67f91e183a0ba6a624582b60 Mon Sep 17 00:00:00 2001 From: MBoretto Date: Thu, 29 Sep 2016 18:04:48 +0200 Subject: [PATCH 2/2] improving try mention markdown --- src/Entities/Entity.php | 17 ++++++++++++ src/Entities/User.php | 44 ++++++++------------------------ tests/unit/Entities/UserTest.php | 6 ----- 3 files changed, 28 insertions(+), 39 deletions(-) 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 65c7c137a..31304aa96 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -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 @@ public function tryMention($markdown = false) } 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; - } } diff --git a/tests/unit/Entities/UserTest.php b/tests/unit/Entities/UserTest.php index 2374ac8d5..8099cfcf1 100644 --- a/tests/unit/Entities/UserTest.php +++ b/tests/unit/Entities/UserTest.php @@ -50,12 +50,6 @@ public function testStripMarkDown() $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']);