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 ...@@ -34,32 +34,25 @@ class Chat extends Entity
{ {
parent::__construct($data); parent::__construct($data);
if (!$this->getType()) { $id = $this->getId();
if ($this->getId() > 0) { $type = $this->getType();
$this->type = 'private'; if (!$type && $id !== 0) {
} elseif ($this->getId() < 0) { $id > 0 && $this->type = 'private';
$this->type = 'group'; $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 * @return string|null
*/ */
public function tryMention() public function tryMention($escape_markdown = false)
{ {
if ($this->isPrivateChat()) { if ($this->isPrivateChat()) {
if ($this->username === null) { return $this->tryMention($escape_markdown);
if ($this->last_name !== null) {
return $this->first_name . ' ' . $this->last_name;
}
return $this->first_name;
}
return '@' . $this->username;
} }
return $this->getTitle(); return $this->getTitle();
...@@ -72,7 +65,7 @@ class Chat extends Entity ...@@ -72,7 +65,7 @@ class Chat extends Entity
*/ */
public function isGroupChat() 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 ...@@ -82,7 +75,7 @@ class Chat extends Entity
*/ */
public function isPrivateChat() public function isPrivateChat()
{ {
return $this->type === 'private'; return $this->getType() === 'private';
} }
/** /**
...@@ -92,7 +85,7 @@ class Chat extends Entity ...@@ -92,7 +85,7 @@ class Chat extends Entity
*/ */
public function isSuperGroup() public function isSuperGroup()
{ {
return $this->type === 'supergroup'; return $this->getType() === 'supergroup';
} }
/** /**
...@@ -102,6 +95,6 @@ class Chat extends Entity ...@@ -102,6 +95,6 @@ class Chat extends Entity
*/ */
public function isChannel() public function isChannel()
{ {
return $this->type === 'channel'; return $this->getType() === 'channel';
} }
} }
...@@ -268,14 +268,13 @@ abstract class Entity ...@@ -268,14 +268,13 @@ abstract class Entity
} }
/** /**
* stripMarkDown * Escape markdown special characters
* Gived a string escape special charactes used in Markdown
* *
* @param string $string * @param string $string
* *
* @return string * @return string
*/ */
public function stripMarkDown($string) public function escapeMarkdown($string)
{ {
return str_replace( return str_replace(
['[', '`', '*', '_',], ['[', '`', '*', '_',],
...@@ -283,4 +282,45 @@ abstract class Entity ...@@ -283,4 +282,45 @@ abstract class Entity
$string $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; ...@@ -27,38 +27,5 @@ namespace Longman\TelegramBot\Entities;
*/ */
class User extends Entity 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 ...@@ -48,12 +48,8 @@ class UserTest extends TestCase
self::assertEquals('John Taylor', $user->tryMention()); 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. // Username.
$user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'j_taylor']); $user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'j_taylor']);
self::assertEquals('@j_taylor', $user->tryMention()); self::assertEquals('@j_taylor', $user->tryMention());
...@@ -68,6 +64,9 @@ class UserTest extends TestCase ...@@ -68,6 +64,9 @@ class UserTest extends TestCase
$user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => '`Taylor`']); $user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => '`Taylor`']);
self::assertEquals('John `Taylor`', $user->tryMention()); self::assertEquals('John `Taylor`', $user->tryMention());
self::assertEquals('John \`Taylor\`', $user->tryMention(true)); 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() 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