Move tryMention to the Entity class.

Rename stripMarkDown to escapeMarkdown to give it more sense.
parent c268c7fd
......@@ -34,32 +34,25 @@ class Chat extends Entity
{
parent::__construct($data);
if (!$this->getType()) {
if ($this->getId() > 0) {
$this->type = 'private';
} elseif ($this->getId() < 0) {
$this->type = 'group';
}
$id = $this->getId();
$type = $this->getType();
if (!$type && $id !== 0) {
$id > 0 && $this->type = 'private';
$id < 0 && $this->type = 'group';
}
}
/**
* Try mention
* Try to mention the user of this chat, else return the title
*
* @param bool $escape_markdown
*
* @return string|null
*/
public function tryMention()
public function tryMention($escape_markdown = false)
{
if ($this->isPrivateChat()) {
if ($this->username === null) {
if ($this->last_name !== null) {
return $this->first_name . ' ' . $this->last_name;
}
return $this->first_name;
}
return '@' . $this->username;
return $this->tryMention($escape_markdown);
}
return $this->getTitle();
......@@ -72,7 +65,7 @@ class Chat extends Entity
*/
public function isGroupChat()
{
return $this->type === 'group' || $this->id < 0;
return $this->getType() === 'group' || $this->getId() < 0;
}
/**
......@@ -82,7 +75,7 @@ class Chat extends Entity
*/
public function isPrivateChat()
{
return $this->type === 'private';
return $this->getType() === 'private';
}
/**
......@@ -92,7 +85,7 @@ class Chat extends Entity
*/
public function isSuperGroup()
{
return $this->type === 'supergroup';
return $this->getType() === 'supergroup';
}
/**
......@@ -102,6 +95,6 @@ class Chat extends Entity
*/
public function isChannel()
{
return $this->type === 'channel';
return $this->getType() === 'channel';
}
}
......@@ -268,14 +268,13 @@ abstract class Entity
}
/**
* stripMarkDown
* Gived a string escape special charactes used in Markdown
* Escape markdown special characters
*
* @param string $string
*
* @return string
*/
public function stripMarkDown($string)
public function escapeMarkdown($string)
{
return str_replace(
['[', '`', '*', '_',],
......@@ -283,4 +282,45 @@ abstract class Entity
$string
);
}
/**
* Try to mention the user
*
* Mention the user with the username otherwise print first and last name
* if the $escape_markdown argument is true special characters are escaped from the output
*
* @param bool $escape_markdown
*
* @return string|null
*/
public function tryMention($escape_markdown = false)
{
//TryMention only makes sense for the User and Chat entity.
if (!($this instanceof User || $this instanceof Chat)) {
return null;
}
//Try with the username first...
$username = $this->getProperty('username');
if ($username !== null) {
if ($escape_markdown) {
$username = $this->escapeMarkdown($username);
}
return '@' . $username;
}
//...otherwise try with the names
$name = $this->getProperty('first_name');
$last_name = $this->getProperty('last_name');
if ($last_name !== null) {
$name .= ' ' . $last_name;
}
if ($escape_markdown) {
$name = $this->escapeMarkdown($name);
}
return $name;
}
}
......@@ -27,38 +27,5 @@ namespace Longman\TelegramBot\Entities;
*/
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)
{
$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->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 $name;
}
}
......@@ -48,12 +48,8 @@ class UserTest extends TestCase
self::assertEquals('John Taylor', $user->tryMention());
}
public function testStripMarkDown()
public function testEscapeMarkdown()
{
// Plain stripMarkDown functionality.
$user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor']);
self::assertEquals('\`\[\*\_', $user->stripMarkDown('`[*_'));
// Username.
$user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'j_taylor']);
self::assertEquals('@j_taylor', $user->tryMention());
......@@ -68,6 +64,9 @@ class UserTest extends TestCase
$user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => '`Taylor`']);
self::assertEquals('John `Taylor`', $user->tryMention());
self::assertEquals('John \`Taylor\`', $user->tryMention(true));
// Plain escapeMarkdown functionality.
self::assertEquals('a\`b\[c\*d\_e', $user->escapeMarkdown('a`b[c*d_e'));
}
public function testGetProperties()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment