Completed
Push — master ( a7ec5f...7812c9 )
by Jan-Petter
02:03
created

CleanParam::check()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 8.8571
cc 6
eloc 10
nc 5
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Directives;
3
4
use vipnytt\RobotsTxtParser\ObjectTools;
5
use vipnytt\RobotsTxtParser\UrlToolbox;
6
7
/**
8
 * Class CleanParam
9
 *
10
 * @package vipnytt\RobotsTxtParser\Directives
11
 */
12
class CleanParam implements DirectiveInterface
13
{
14
    use ObjectTools;
15
    use UrlToolbox;
16
17
    /**
18
     * Directive
19
     */
20
    const DIRECTIVE = 'Clean-param';
21
22
    protected $array = [];
23
    protected $parent;
24
25
    public function __construct($array = [], $parent = null)
26
    {
27
        $this->array = $array;
28
    }
29
30
    /**
31
     * Add
32
     *
33
     * @param string $line
34
     * @return bool
35
     */
36
    public function add($line)
37
    {
38
        // split into parameter and path
39
        $array = array_map('trim', mb_split('\s+', $line, 2));
40
        // strip any invalid characters from path prefix
41
        $path = isset($array[1]) ? $this->urlEncode(mb_ereg_replace('[^A-Za-z0-9\.-\/\*\_]', '', $array[1])) : '/*';
42
        $param = array_map('trim', mb_split('&', $array[0]));
43
        foreach ($param as $key) {
44
            $this->array[$key][] = $path;
45
        }
46
        return true;
47
    }
48
49
    /**
50
     * Check Clean-Param rule
51
     *
52
     * @param  string $path
53
     * @return bool
54
     */
55
    public function check($path)
56
    {
57
        foreach ($this->array as $param => $paths) {
58
            if (
59
                mb_strpos($path, "?$param=") ||
60
                mb_strpos($path, "&$param=")
61
            ) {
62
                if (empty($paths)) {
63
                    return true;
64
                }
65
                if ($this->checkPath($path, $paths)) {
66
                    return true;
67
                }
68
            }
69
        }
70
        return false;
71
    }
72
73
    /**
74
     * Rule export
75
     *
76
     * @return array
77
     */
78
    public function getArray()
79
    {
80
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
81
    }
82
83
    public function export()
84
    {
85
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
86
    }
87
}
88