Completed
Push — master ( a008aa...b78379 )
by Nikita
01:50
created

OpenGraph::getUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace LaraComponents\Seo\OpenGraph;
4
5
class OpenGraph
6
{
7
    const DETERMINER_SUPPORTED = [
8
        'a', 'an', 'auto', 'the',
9
    ];
10
11
    const LOCALE_SUPPORTED = [
12
        'af_ZA', 'ak_GH', 'am_ET', 'ar_AR', 'as_IN', 'ay_BO', 'az_AZ', 'be_BY', 'bg_BG', 'bn_IN', 'br_FR',
13
        'bs_BA', 'ca_ES', 'cb_IQ', 'ck_US', 'co_FR', 'cs_CZ', 'cx_PH', 'cy_GB', 'da_DK', 'de_DE', 'el_GR',
14
        'en_GB', 'en_IN', 'en_US', 'eo_EO', 'es_CO', 'es_ES', 'es_LA', 'et_EE', 'eu_ES', 'fa_IR', 'ff_NG',
15
        'fi_FI', 'fo_FO', 'fr_CA', 'fr_FR', 'fy_NL', 'ga_IE', 'gl_ES', 'gn_PY', 'gu_IN', 'gx_GR', 'ha_NG',
16
        'he_IL', 'hi_IN', 'hr_HR', 'hu_HU', 'hy_AM', 'id_ID', 'ig_NG', 'is_IS', 'it_IT', 'ja_JP', 'ja_KS',
17
        'jv_ID', 'ka_GE', 'kk_KZ', 'km_KH', 'kn_IN', 'ko_KR', 'ku_TR', 'la_VA', 'lg_UG', 'li_NL', 'ln_CD',
18
        'lo_LA', 'lt_LT', 'lv_LV', 'mg_MG', 'mk_MK', 'ml_IN', 'mn_MN', 'mr_IN', 'ms_MY', 'mt_MT', 'my_MM',
19
        'nb_NO', 'nd_ZW', 'ne_NP', 'nl_BE', 'nl_NL', 'nn_NO', 'ny_MW', 'or_IN', 'pa_IN', 'pl_PL', 'ps_AF',
20
        'pt_BR', 'pt_PT', 'qu_PE', 'rm_CH', 'ro_RO', 'ru_RU', 'rw_RW', 'sa_IN', 'sc_IT', 'se_NO', 'si_LK',
21
        'sk_SK', 'sl_SI', 'sn_ZW', 'so_SO', 'sq_AL', 'sr_RS', 'sv_SE', 'sw_KE', 'sy_SY', 'sz_PL', 'ta_IN',
22
        'te_IN', 'tg_TJ', 'th_TH', 'tk_TM', 'tl_PH', 'tr_TR', 'tt_RU', 'tz_MA', 'uk_UA', 'ur_PK', 'uz_UZ',
23
        'vi_VN', 'wo_SN', 'xh_ZA', 'yi_DE', 'yo_NG', 'zh_CN', 'zh_HK', 'zh_TW', 'zu_ZA', 'zz_TR',
24
    ];
25
26
    /**
27
     * @var string
28
     */
29
    protected $type;
30
31
    /**
32
     * @var string
33
     */
34
    protected $title;
35
36
    /**
37
     * @var string
38
     */
39
    protected $siteName;
40
41
    /**
42
     * @var string
43
     */
44
    protected $description;
45
46
    /**
47
     * @var string
48
     */
49
    protected $url;
50
51
    /**
52
     * @var string
53
     */
54
    protected $determiner;
55
56
    /**
57
     * @var string
58
     */
59
    protected $locale;
60
61
    /**
62
     * @var array
63
     */
64
    protected $alternateLocales = [];
65
66
    /**
67
     * @var array
68
     */
69
    protected $images = [];
70
71
    /**
72
     * @var array
73
     */
74
    protected $audios = [];
75
76
    /**
77
     * @var array
78
     */
79
    protected $videos = [];
80
81
    public function setType($type)
82
    {
83
        $this->type = $this->trim($type);
84
85
        return $this;
86
    }
87
88
    public function getType()
89
    {
90
        return $this->type;
91
    }
92
93
    public function setTitle($title)
94
    {
95
        $this->title = $this->trim($title);
96
97
        return $this;
98
    }
99
100
    public function getTitle()
101
    {
102
        return $this->title;
103
    }
104
105
    public function setSiteName($siteName)
106
    {
107
        $this->siteName = $this->trim($siteName);
108
109
        return $this;
110
    }
111
112
    public function getSiteName()
113
    {
114
        return $this->siteName;
115
    }
116
117
    public function setDescription($description)
118
    {
119
        $this->description = $this->trim($description);
120
121
        return $this;
122
    }
123
124
    public function getDescription()
125
    {
126
        return $this->description;
127
    }
128
129
    public function setUrl($url)
130
    {
131
        $this->url = $this->trim($url);
132
133
        return $this;
134
    }
135
136
    public function getUrl()
137
    {
138
        return $this->url;
139
    }
140
141
    public function setDeterminer($determiner)
142
    {
143
        if (in_array($determiner, OpenGraph::DETERMINER_SUPPORTED) || is_null($determiner)) {
144
            $this->determiner = $determiner;
145
        }
146
147
        return $this;
148
    }
149
150
    public function getDeterminer()
151
    {
152
        return $this->determiner;
153
    }
154
155
    public function setLocale($locale)
156
    {
157
        if (in_array($locale, OpenGraph::LOCALE_SUPPORTED) || is_null($locale)) {
158
            $this->locale = $locale;
159
        }
160
161
        return $this;
162
    }
163
164
    public function getLocale()
165
    {
166
        return $this->locale;
167
    }
168
169
    public function addAlternateLocale($locale)
170
    {
171
        if (in_array($locale, OpenGraph::LOCALE_SUPPORTED)) {
172
            $this->alternateLocales[] = $locale;
173
        }
174
175
        return $this;
176
    }
177
178
    public function setAlternateLocales(array $locales)
179
    {
180
        $this->alternateLocales = [];
181
182
        foreach ($locales as $locale) {
183
            $this->addAlternateLocale($locale);
184
        }
185
186
        return $this;
187
    }
188
189
    public function getAlternateLocales()
190
    {
191
        return $this->alternateLocales;
192
    }
193
194
    public function addImage(Image $image)
195
    {
196
        $this->images[] = $image;
197
198
        return $this;
199
    }
200
201
    public function setImages(array $images)
202
    {
203
        $this->images = [];
204
205
        foreach ($images as $image) {
206
            $this->addImage($image);
207
        }
208
209
        return $this;
210
    }
211
212
    public function getImages()
213
    {
214
        return $this->images;
215
    }
216
217
    public function addAudio(Audio $audio)
218
    {
219
        $this->audios[] = $audio;
220
221
        return $this;
222
    }
223
224
    public function setAudios(array $audios)
225
    {
226
        $this->audios = [];
227
228
        foreach ($audios as $audio) {
229
            $this->addAudio($audio);
230
        }
231
232
        return $this;
233
    }
234
235
    public function getAudios()
236
    {
237
        return $this->audios;
238
    }
239
240
    public function addVideo(Video $video)
241
    {
242
        $this->videos[] = $video;
243
244
        return $this;
245
    }
246
247
    public function setVideos(array $videos)
248
    {
249
        $this->videos = [];
250
251
        foreach ($videos as $video) {
252
            $this->addVideo($video);
253
        }
254
255
        return $this;
256
    }
257
258
    public function getVideos()
259
    {
260
        return $this->videos;
261
    }
262
263
    protected function trim($string)
264
    {
265
        $string = (is_string($string) && !empty(trim($string))) ? trim($string) : null;
266
267
        return $string;
268
    }
269
270
    public function toArray()
271
    {
272
        $result = [];
273
274
        $result['og:type'] = $this->getType();
275
        $result['og:title'] = $this->getTitle();
276
        $result['og:site_name'] = $this->getSiteName();
277
        $result['og:description'] = $this->getDescription();
278
        $result['og:url'] = $this->getUrl();
279
        $result['og:determiner'] = $this->getDeterminer();
280
        $result['og:locale'] = $this->getLocale();
281
        $result['og:locale:alternate'] = $this->getAlternateLocales();
282
283
        $result['og:image'] = [];
284
        foreach ($this->getImages() as $image) {
285
            $result['og:image'][] = $image->toArray();
286
        }
287
288
        $result['og:audio'] = [];
289
        foreach ($this->getAudios() as $audio) {
290
            $result['og:audio'][] = $audio->toArray();
291
        }
292
293
        $result['og:video'] = [];
294
        foreach ($this->getVideos() as $video) {
295
            $result['og:video'][] = $video->toArray();
296
        }
297
298
        return array_filter($result);
299
    }
300
}
301