Commit c816458d authored by MBoretto's avatar MBoretto

improving try mention markdown

parent 94aa39ee
...@@ -266,4 +266,21 @@ abstract class Entity ...@@ -266,4 +266,21 @@ abstract class Entity
return $new_objects; 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
);
}
} }
...@@ -30,24 +30,29 @@ class User extends Entity ...@@ -30,24 +30,29 @@ class User extends Entity
/** /**
* tryMention * 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 * @param bool $markdown
* *
* @return string * @return string
*/ */
public function tryMention($markdown = false) public function tryMention($markdown = false)
{ {
if (isset($this->username)) { $username = $this->getProperty('username');
if ($username !== null) {
if ($markdown) { if ($markdown) {
//Escaping md special characters //Escaping md special characters
//Please notice that just the _ is allowed in the username ` * [ are not allowed //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; $name = $this->getProperty('first_name');
if (isset($this->last_name)) { $last_name = $this->getProperty('last_name');
$name .= ' ' . $this->last_name; if ($last_name !== null) {
$name .= ' ' . $last_name;
} }
if ($markdown) { if ($markdown) {
...@@ -56,31 +61,4 @@ class User extends Entity ...@@ -56,31 +61,4 @@ class User extends Entity
} }
return $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;
}
} }
...@@ -50,12 +50,6 @@ class UserTest extends TestCase ...@@ -50,12 +50,6 @@ class UserTest extends TestCase
$this->assertEquals('\`\[\*\_', $this->user->stripMarkDown('`[*_')); $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() public function testUsernameMarkdown()
{ {
$this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'j_taylor']); $this->user = new User(['id' => 1, 'first_name' => 'John', 'last_name' => 'Taylor', 'username' => 'j_taylor']);
......
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