Passed
Push — develop ( 2ab306...3001e5 )
by Benjamin
02:54
created

AutoLink::getNoOpener()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link      https://dukt.net/craft/twitter/
4
 * @copyright Copyright (c) 2018, Dukt
5
 * @license   https://dukt.net/craft/twitter/docs/license
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 (is_null($tweet)) {
45
            $tweet = $this->tweet;
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 = array())
108
    {
109
        $rel = array();
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
        return $link;
131
    }
132
133
    /**
134
     * Whether to include the value 'noopener' in the 'rel' attribute.
135
     *
136
     * @return  bool  Whether to add 'noopener' to the 'rel' attribute.
137
     */
138
    public function getNoOpener()
139
    {
140
        return $this->noopener;
141
    }
142
143
    /**
144
     * Whether to include the value 'noopener' in the 'rel' attribute.
145
     *
146
     * @param  bool  $v  The value to add to the 'target' attribute.
147
     *
148
     * @return  Autolink  Fluid method chaining.
149
     */
150
    public function setNoOpener($v)
151
    {
152
        $this->noopener = $v;
153
        return $this;
154
    }
155
}