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