Passed
Push — master ( abe9d4...f9c79d )
by Vladimir
04:18
created

DriverAbstract::appendToData()   A

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
 * @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 DriverAbstract 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
     * @since 2.0
63
     */
64
    public function setUrl($url)
65
    {
66
        $this->url = $url;
67
    }
68
69
    /**
70
     * @param string $title
71
     * @since 2.0
72
     */
73
    public function setTitle($title)
74
    {
75
        $this->title = $title;
76
    }
77
78
    /**
79
     * @param string $description
80
     * @since 2.0
81
     */
82
    public function setDescription($description)
83
    {
84
        $this->description = $description;
85
    }
86
87
    /**
88
     * @param string $imageUrl
89
     * @since 2.0
90
     */
91
    public function setImageUrl($imageUrl)
92
    {
93
        $this->imageUrl = $imageUrl;
94
    }
95
96
    /**
97
     * Append value to data array.
98
     *
99
     * @param string $key
100
     * @param string $value
101
     * @param bool $urlEncode
102
     * @since 2.0
103
     */
104
    public function appendToData($key, $value, $urlEncode = true)
105
    {
106
        $key = '{' . $key . '}';
107
        $this->_data[$key] = $urlEncode ? static::encodeData($value) : $value;
108
    }
109
110
    /**
111
     * Prepare data data to insert into the link.
112
     */
113
    public function init()
114
    {
115
        $this->processShareData();
116
117
        $this->_data = ArrayHelper::merge([
118
            '{url}'         => $this->url,
119
            '{title}'       => $this->title,
120
            '{description}' => $this->description,
121
            '{imageUrl}'    => $this->imageUrl
122
        ], $this->_data);
123
124
        $metaTags = $this->getMetaTags();
125
        if (!empty($metaTags)) {
126
            $rawData = static::decodeData($this->_data);
127
            $view = Yii::$app->getView();
128
129
            foreach ($metaTags as $metaTag) {
130
                $metaTag['content'] = strtr($metaTag['content'], $rawData);
131
                $view->registerMetaTag($metaTag);
132
            }
133
        }
134
    }
135
136
    /**
137
     * Generates share link.
138
     *
139
     * @return string
140
     */
141
    final public function getLink()
142
    {
143
        return strtr($this->buildLink(), $this->_data);
144
    }
145
146
    /**
147
     * Encode data for URL.
148
     *
149
     * @param string|array $data
150
     * @return string|array
151
     */
152
    public static function encodeData($data)
153
    {
154
        if (is_array($data)) {
155
            foreach ($data as $key => $value) {
156
                $data[$key] = urlencode($value);
157
            }
158
159
            return $data;
160
        }
161
162
        return urlencode($data);
163
    }
164
165
    /**
166
     * Decode the encoded data.
167
     *
168
     * @param string|array $data
169
     * @return string|array
170
     */
171
    public static function decodeData($data)
172
    {
173
        if (is_array($data)) {
174
            foreach ($data as $key => $value) {
175
                $data[$key] = urldecode($value);
176
            }
177
178
            return $data;
179
        }
180
181
        return urldecode($data);
182
    }
183
184
    /**
185
     * Adds URL param to link.
186
     *
187
     * @param string $link
188
     * @param string $name Param name.
189
     * @param string $value Param value.
190
     * @since 1.4.0
191
     */
192
    final protected function addUrlParam(&$link, $name, $value)
193
    {
194
        $base = $name . '=' . $value;
195
196
        if (strpos($link, '?') !== false) {
197
            $last = substr($link, -1);
198
            if ('?' === $last || '&' === $last) {
199
                $link .= $base;
200
            } else {
201
                $link .= '&' . $base;
202
            }
203
        } else {
204
            $link .= '?' . $base;
205
        }
206
    }
207
208
    /**
209
     * Returns array of meta tags.
210
     *
211
     * @return array
212
     * @since 2.0
213
     */
214
    protected function getMetaTags()
215
    {
216
        return [];
217
    }
218
219
    /**
220
     * Method should process the share data for current driver.
221
     */
222
    abstract protected function processShareData();
223
224
    /**
225
     * Method should build template of share link.
226
     *
227
     * @return string
228
     * @since 2.0
229
     */
230
    abstract protected function buildLink();
231
}
232