Skip to content

Commit

Permalink
Merge pull request #3 from MBoretto/md_mention
Browse files Browse the repository at this point in the history
introduce md tryMention

Thanks @MBoretto !
  • Loading branch information
noplanman authored Sep 29, 2016
2 parents 2711d62 + c816458 commit c35a03f
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 8 deletions.
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
*
* @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 <akalongman@gmail.com>
*
* 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 <akalongman@gmail.com>
* @copyright Avtandil Kikabidze <akalongman@gmail.com>
* @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));
}
}

0 comments on commit c35a03f

Please sign in to comment.