InstagramUrlManipulator::mergeUrlParams()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Maztech\Url;
4
5
/**
6
 * Class InstagramUrlManipulator
7
 *
8
 * @package Instagram
9
 */
10
class InstagramUrlManipulator
11
{
12
    /**
13
     * Remove params from a URL.
14
     *
15
     * @param string $url            The URL to filter.
16
     * @param array  $paramsToFilter The params to filter from the URL.
17
     *
18
     * @return string The URL with the params removed.
19
     */
20
    public static function removeParamsFromUrl($url, array $paramsToFilter)
21
    {
22
        $parts = parse_url($url);
23
24
        $query = '';
25
        if (isset($parts['query'])) {
26
            $params = [];
27
            parse_str($parts['query'], $params);
28
29
            // Remove query params
30
            foreach ($paramsToFilter as $paramName) {
31
                unset($params[$paramName]);
32
            }
33
34
            if (count($params) > 0) {
35
                $query = '?' . http_build_query($params, '', '&');
36
            }
37
        }
38
39
        $scheme = isset($parts['scheme']) ? $parts['scheme'] . '://' : '';
40
        $host = isset($parts['host']) ? $parts['host'] : '';
41
        $port = isset($parts['port']) ? ':' . $parts['port'] : '';
42
        $path = isset($parts['path']) ? $parts['path'] : '';
43
        $fragment = isset($parts['fragment']) ? '#' . $parts['fragment'] : '';
44
45
        return $scheme . $host . $port . $path . $query . $fragment;
46
    }
47
48
    /**
49
     * Gracefully appends params to the URL.
50
     *
51
     * @param string $url       The URL that will receive the params.
52
     * @param array  $newParams The params to append to the URL.
53
     *
54
     * @return string
55
     */
56
    public static function appendParamsToUrl($url, array $newParams = [])
57
    {
58
        if (empty($newParams)) {
59
            return $url;
60
        }
61
62
        if (strpos($url, '?') === false) {
63
            return $url . '?' . http_build_query($newParams, '', '&');
64
        }
65
66
        list($path, $query) = explode('?', $url, 2);
67
        $existingParams = [];
68
        parse_str($query, $existingParams);
69
70
        // Favor params from the original URL over $newParams
71
        $newParams = array_merge($newParams, $existingParams);
72
73
        // Sort for a predicable order
74
        ksort($newParams);
75
76
        return $path . '?' . http_build_query($newParams, '', '&');
77
    }
78
79
    /**
80
     * Returns the params from a URL in the form of an array.
81
     *
82
     * @param string $url The URL to parse the params from.
83
     *
84
     * @return array
85
     */
86
    public static function getParamsAsArray($url)
87
    {
88
        $query = parse_url($url, PHP_URL_QUERY);
89
        if (!$query) {
90
            return [];
91
        }
92
        $params = [];
93
        parse_str($query, $params);
94
95
        return $params;
96
    }
97
98
    /**
99
     * Adds the params of the first URL to the second URL.
100
     *
101
     * Any params that already exist in the second URL will go untouched.
102
     *
103
     * @param string $urlToStealFrom The URL harvest the params from.
104
     * @param string $urlToAddTo     The URL that will receive the new params.
105
     *
106
     * @return string The $urlToAddTo with any new params from $urlToStealFrom.
107
     */
108
    public static function mergeUrlParams($urlToStealFrom, $urlToAddTo)
109
    {
110
        $newParams = static::getParamsAsArray($urlToStealFrom);
111
        // Nothing new to add, return as-is
112
        if (!$newParams) {
0 ignored issues
show
introduced by
$newParams is an empty array, thus ! $newParams is always true.
Loading history...
Bug Best Practice introduced by
The expression $newParams of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
113
            return $urlToAddTo;
114
        }
115
116
        return static::appendParamsToUrl($urlToAddTo, $newParams);
117
    }
118
119
    /**
120
     * Check for a "/" prefix and prepend it if not exists.
121
     *
122
     * @param string|null $string
123
     *
124
     * @return string|null
125
     */
126
    public static function forceSlashPrefix($string)
127
    {
128
        if (!$string) {
129
            return $string;
130
        }
131
132
        return strpos($string, '/') === 0 ? $string : '/' . $string;
133
    }
134
135
    /**
136
     * Trims off the hostname and Graph version from a URL.
137
     *
138
     * @param string $urlToTrim The URL the needs the surgery.
139
     *
140
     * @return string The $urlToTrim with the hostname and Graph version removed.
141
     */
142
    public static function baseGraphUrlEndpoint($urlToTrim)
143
    {
144
        return '/' . preg_replace('/^https:\/\/.+\.Instagram\.com(\/v.+?)?\//', '', $urlToTrim);
145
    }
146
}
147