Completed
Push — master ( badca6...1bd341 )
by Jan-Petter
01:59
created

CleanParam::getArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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