Completed
Branch master (ed4a79)
by Jan-Petter
01:36
created

CleanParamClient::parse()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 3
nop 2
1
<?php
2
/**
3
 * vipnytt/RobotsTxtParser
4
 *
5
 * @link https://github.com/VIPnytt/RobotsTxtParser
6
 * @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
7
 */
8
9
namespace vipnytt\RobotsTxtParser\Client\Directives;
10
11
/**
12
 * Class CleanParamClient
13
 *
14
 * @see https://github.com/VIPnytt/RobotsTxtParser/blob/master/docs/methods/CleanParamClient.md for documentation
15
 * @package vipnytt\RobotsTxtParser\Client\Directives
16
 */
17
class CleanParamClient implements ClientInterface
18
{
19
    use DirectiveClientTrait;
20
21
    /**
22
     * Common dynamic uri parameters
23
     * @var string[]
24
     */
25
    protected $commonParam = [
26
        'popup',
27
        'ref',
28
        'token',
29
        'utm_campaign',
30
        'utm_content',
31
        'utm_medium',
32
        'utm_source',
33
        'utm_term',
34
    ];
35
36
    /**
37
     * Clean-param
38
     * @var string[][]
39
     */
40
    private $cleanParam = [];
41
42
    /**
43
     * CleanParamClient constructor.
44
     *
45
     * @param string[][] $cleanParam
46
     */
47
    public function __construct(array $cleanParam)
48
    {
49
        $this->cleanParam = $cleanParam;
50
    }
51
52
    /**
53
     * Has robots.txt defined dynamic or common dynamic parameters check
54
     *
55
     * @param string $uri
56
     * @param string[] $customParam
57
     * @return string[]
58
     */
59
    public function detectWithCommon($uri, array $customParam = [])
60
    {
61
        $pairs = array_merge_recursive(
62
            $this->cleanParam,
63
            $this->appendPath($this->commonParam),
64
            $this->appendPath($customParam)
65
        );
66
        return $this->parse($uri, $pairs);
67
    }
68
69
    /**
70
     * Convert param list to an valid Clean-param list
71
     *
72
     * @param string[] $parameters
73
     * @return array
74
     */
75
    private function appendPath(array $parameters)
76
    {
77
        $result = [];
78
        foreach ($parameters as $parameter) {
79
            $result[$parameter] = ['/'];
80
        }
81
        return $result;
82
    }
83
84
    /**
85
     * Parse uri and return detected parameters
86
     *
87
     * @param string $uri
88
     * @param array $pairs
89
     * @return string[]
90
     */
91
    protected function parse($uri, array $pairs)
92
    {
93
        $result = [];
94
        foreach ($pairs as $param => $paths) {
95
            if ((
96
                    strpos($uri, "?$param=") ||
97
                    strpos($uri, "&$param=")
98
                ) &&
99
                $this->checkPaths($uri, $paths) !== false
100
            ) {
101
                $result[] = $param;
102
            }
103
        }
104
        sort($result);
105
        return $result;
106
    }
107
108
    /**
109
     * Export
110
     *
111
     * @return string[][]
112
     */
113
    public function export()
114
    {
115
        return $this->cleanParam;
116
    }
117
118
    /**
119
     * Detect dynamic parameters
120
     *
121
     * @param  string $uri
122
     * @return string[]
123
     */
124
    public function detect($uri)
125
    {
126
        return $this->parse($uri, $this->cleanParam);
127
    }
128
}
129