Completed
Pull Request — refactor_entities (#3)
by
unknown
02:50
created

User::stripMarkDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the TelegramBot package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Longman\TelegramBot\Entities;
12
13
/**
14
 * Class User
15
 *
16
 * @link https://core.telegram.org/bots/api#user
17
 *
18
 * @property int    $id         Unique identifier for this user or bot
19
 * @property string $first_name User's or bot’s first name
20
 * @property string $last_name  Optional. User's or bot’s last name
21
 * @property string $username   Optional. User's or bot’s username
22
 *
23
 * @method int    getId()        Unique identifier for this user or bot
24
 * @method string getFirstName() User's or bot’s first name
25
 * @method string getLastName()  Optional. User's or bot’s last name
26
 * @method string getUsername()  Optional. User's or bot’s username
27
 */
28
class User extends Entity
29
{
30
    /**
31
     * tryMention
32
     *
33
     * @param bool $markdown
34
     *
35
     * @return string
36
     */
37 6
    public function tryMention($markdown = false)
38
    {
39 6
        if (isset($this->username)) {
40 2
            if ($markdown) {
41
                //Escaping md special characters
42
                //Please notice that just the _ is allowed in the username ` * [ are not allowed
43 1
                return $this->prependAt($this->stripMarkDown($this->username));
44
            }
45 2
            return $this->prependAt($this->username);
46
        }
47
48 4
        $name = $this->first_name;
49 4
        if (isset($this->last_name)) {
50 2
            $name .= ' ' . $this->last_name;
51
        }
52
        
53 4
        if ($markdown) {
54
            //Escaping md special characters
55 2
            return $this->stripMarkDown($name);
56
        }
57 4
        return $name;
58
    }
59
60
    /**
61
     * stripMarkDown
62
     *
63
     * @param string $string
64
     *
65
     * @return string
66
     */
67 4
    public function stripMarkDown($string)
68
    {
69 4
        $string = str_replace('[', '\[', $string);
70 4
        $string = str_replace('`', '\`', $string);
71 4
        $string = str_replace('*', '\*', $string);
72 4
        return str_replace('_', '\_', $string);
73
    }
74
75
    /**
76
     * prepend@
77
     *
78
     * @param string $string
79
     *
80
     * @return string
81
     */
82 3
    public function prependAt($string)
83
    {
84 3
        return '@' . $string;
85
    }
86
}
87