AbstractDriver::appendToData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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