Completed
Push — master ( d0eb74...69cae2 )
by Vladimir
03:54
created

AbstractDriver::setTitle()   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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-social-share
4
 * @copyright Copyright (c) 2017 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\social\share\base;
9
10
use Yii;
11
use yii\base\BaseObject;
12
use yii\helpers\ArrayHelper;
13
14
/**
15
 * Base driver for social network definition classes.
16
 *
17
 * @property-write $url
18
 * @property-write $title
19
 * @property-write $description
20
 * @property-write $imageUrl
21
 *
22
 * @author Vladimir Kuprienko <[email protected]>
23
 * @since 1.0
24
 */
25
abstract class AbstractDriver extends BaseObject
26
{
27
    /**
28
     * Absolute URL to the page.
29
     *
30
     * @var string
31
     */
32
    protected $url;
33
    /**
34
     * Title for share.
35
     *
36
     * @var string
37
     */
38
    protected $title;
39
    /**
40
     * Description for share.
41
     *
42
     * @var string
43
     */
44
    protected $description;
45
    /**
46
     * Absolute URL to the image for share.
47
     *
48
     * @var string
49
     */
50
    protected $imageUrl;
51
52
    /**
53
     * Contains data for URL.
54
     *
55
     * @var array
56
     */
57
    private $_data = [];
58
59
60
    /**
61
     * @param string $url
62
     *
63
     * @since 2.0
64
     */
65
    public function setUrl($url)
66
    {
67
        $this->url = $url;
68
    }
69
70
    /**
71
     * @param string $title
72
     *
73
     * @since 2.0
74
     */
75
    public function setTitle($title)
76
    {
77
        $this->title = $title;
78
    }
79
80
    /**
81
     * @param string $description
82
     *
83
     * @since 2.0
84
     */
85
    public function setDescription($description)
86
    {
87
        $this->description = $description;
88
    }
89
90
    /**
91
     * @param string $imageUrl
92
     *
93
     * @since 2.0
94
     */
95
    public function setImageUrl($imageUrl)
96
    {
97
        $this->imageUrl = $imageUrl;
98
    }
99
100
    /**
101
     * Append value to data array.
102
     *
103
     * @param string    $key
104
     * @param string    $value
105
     * @param bool      $urlEncode
106
     *
107
     * @since 2.0
108
     */
109
    public function appendToData($key, $value, $urlEncode = true)
110
    {
111
        $key = '{' . $key . '}';
112
        $this->_data[$key] = $urlEncode ? static::encodeData($value) : $value;
113
    }
114
115
    /**
116
     * Prepare data data to insert into the link.
117
     */
118
    public function init()
119
    {
120
        $this->processShareData();
121
122
        $this->_data = ArrayHelper::merge([
123
            '{url}'         => $this->url,
124
            '{title}'       => $this->title,
125
            '{description}' => $this->description,
126
            '{imageUrl}'    => $this->imageUrl
127
        ], $this->_data);
128
129
        $metaTags = $this->getMetaTags();
130
        if (!empty($metaTags)) {
131
            $rawData = static::decodeData($this->_data);
132
            $view = Yii::$app->getView();
133
134
            foreach ($metaTags as $metaTag) {
135
                $metaTag['content'] = strtr($metaTag['content'], $rawData);
136
                $view->registerMetaTag($metaTag);
137
            }
138
        }
139
    }
140
141
    /**
142
     * Generates share link.
143
     *
144
     * @return string
145
     */
146
    final public function getLink()
147
    {
148
        return strtr($this->buildLink(), $this->_data);
149
    }
150
151
    /**
152
     * Encode data for URL.
153
     *
154
     * @param string|array $data
155
     *
156
     * @return string|array
157
     */
158
    public static function encodeData($data)
159
    {
160
        if (is_array($data)) {
161
            foreach ($data as $key => $value) {
162
                $data[$key] = urlencode($value);
163
            }
164
165
            return $data;
166
        }
167
168
        return urlencode($data);
169
    }
170
171
    /**
172
     * Decode the encoded data.
173
     *
174
     * @param string|array $data
175
     *
176
     * @return string|array
177
     */
178
    public static function decodeData($data)
179
    {
180
        if (is_array($data)) {
181
            foreach ($data as $key => $value) {
182
                $data[$key] = urldecode($value);
183
            }
184
185
            return $data;
186
        }
187
188
        return urldecode($data);
189
    }
190
191
    /**
192
     * Adds URL param to link.
193
     *
194
     * @param string $link
195
     * @param string $name  Param name.
196
     * @param string $value Param value.
197
     *
198
     * @since 1.4.0
199
     */
200
    final protected function addUrlParam(&$link, $name, $value)
201
    {
202
        $base = $name . '=' . $value;
203
204
        if (strpos($link, '?') !== false) {
205
            $last = substr($link, -1);
206
            if ('?' === $last || '&' === $last) {
207
                $link .= $base;
208
            } else {
209
                $link .= '&' . $base;
210
            }
211
        } else {
212
            $link .= '?' . $base;
213
        }
214
    }
215
216
    /**
217
     * Returns array of meta tags.
218
     *
219
     * @return array
220
     *
221
     * @since 2.0
222
     */
223
    protected function getMetaTags()
224
    {
225
        return [];
226
    }
227
228
    /**
229
     * Method should process the share data for current driver.
230
     */
231
    abstract protected function processShareData();
232
233
    /**
234
     * Method should build template of share link.
235
     *
236
     * @return string
237
     *
238
     * @since 2.0
239
     */
240
    abstract protected function buildLink();
241
}
242