Issues (45)

src/lib/AutoLink.php (1 issue)

1
<?php
2
/**
3
 * @link      https://dukt.net/twitter/
4
 * @copyright Copyright (c) Dukt
5
 * @license   https://github.com/dukt/twitter/blob/master/LICENSE.md
6
 */
7
8
namespace dukt\twitter\lib;
9
10
/**
11
 * Class Auto Link
12
 *
13
 * @author Dukt <[email protected]>
14
 * @since  3.0
15
 */
16
class AutoLink extends \Twitter\Text\Autolink
17
{
18
    // Properties
19
    // =========================================================================
20
21
    /**
22
     * Whether to include the value 'noopener' in the 'rel' attribute.
23
     *
24
     * @var  bool
25
     */
26
    protected $noopener = true;
27
28
    // Public Methods
29
    // =========================================================================
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public static function create($tweet = null, $full_encode = false)
35
    {
36
        return new self($tweet, $full_encode);
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function autoLinkEntities($tweet = null, $entities = null)
43
    {
44
        if (null === $tweet) {
45
            $tweet = $this->tweet;
0 ignored issues
show
Deprecated Code introduced by
The property Twitter\Text\Autolink::$tweet has been deprecated: will be removed ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

45
            $tweet = /** @scrutinizer ignore-deprecated */ $this->tweet;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
46
        }
47
48
        $text = '';
49
        $beginIndex = 0;
50
51
        foreach ($entities as $entity) {
52
            if (isset($entity['screen_name'])) {
53
                $text .= mb_substr($tweet, $beginIndex, $entity['indices'][0] - $beginIndex);
54
            } else {
55
                $text .= mb_substr($tweet, $beginIndex, $entity['indices'][0] - $beginIndex);
56
            }
57
58
            if (isset($entity['url'])) {
59
                $text .= $this->linkToUrl($entity);
60
            } elseif (isset($entity['hashtag'])) {
61
                $text .= $this->linkToHashtag($entity, $tweet);
62
            } elseif (isset($entity['screen_name'])) {
63
                $text .= $this->linkToMentionAndList($entity);
64
            } elseif (isset($entity['cashtag'])) {
65
                $text .= $this->linkToCashtag($entity, $tweet);
66
            }
67
68
            $beginIndex = $entity['indices'][1];
69
        }
70
71
        $text .= mb_substr($tweet, $beginIndex, mb_strlen($tweet));
72
73
        return $text;
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function linkToMentionAndList($entity)
80
    {
81
        $attributes = [];
82
83
        if (!empty($entity['list_slug'])) {
84
            # Replace the list and username
85
            $linkText = $entity['screen_name'].$entity['list_slug'];
86
            $class = $this->class_list;
87
            $url = $this->url_base_list.$linkText;
88
        } else {
89
            # Replace the username
90
            $linkText = '@'.$entity['screen_name'];
91
            $class = $this->class_user;
92
            $url = $this->url_base_user.$linkText;
93
        }
94
95
        if (!empty($class)) {
96
            $attributes['class'] = $class;
97
        }
98
99
        $attributes['href'] = $url;
100
101
        return $this->linkToText($entity, $linkText, $attributes);
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107
    public function linkToText(array $entity, $text, $attributes = [])
108
    {
109
        $rel = [];
110
        if ($this->external) {
111
            $rel[] = 'external';
112
        }
113
        if ($this->nofollow) {
114
            $rel[] = 'nofollow';
115
        }
116
        if ($this->noopener) {
117
            $rel[] = 'noopener';
118
        }
119
        if (!empty($rel)) {
120
            $attributes['rel'] = join(' ', $rel);
121
        }
122
        if ($this->target) {
123
            $attributes['target'] = $this->target;
124
        }
125
        $link = '<a';
126
        foreach ($attributes as $key => $val) {
127
            $link .= ' '.$key.'="'.$this->escapeHTML($val).'"';
128
        }
129
        $link .= '>'.$text.'</a>';
130
131
        return $link;
132
    }
133
134
    /**
135
     * Whether to include the value 'noopener' in the 'rel' attribute.
136
     *
137
     * @return  bool  Whether to add 'noopener' to the 'rel' attribute.
138
     */
139
    public function getNoOpener()
140
    {
141
        return $this->noopener;
142
    }
143
144
    /**
145
     * Whether to include the value 'noopener' in the 'rel' attribute.
146
     *
147
     * @param  bool $v The value to add to the 'target' attribute.
148
     *
149
     * @return  Autolink  Fluid method chaining.
150
     */
151
    public function setNoOpener($v)
152
    {
153
        $this->noopener = $v;
154
155
        return $this;
156
    }
157
}