Completed
Branch 2.0-dev (131e57)
by Jan-Petter
02:08
created

CleanParam::check()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 9
nc 3
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Core\Directives;
3
4
use vipnytt\RobotsTxtParser\Core\RobotsTxtInterface;
5
use vipnytt\RobotsTxtParser\Core\Toolbox;
6
use vipnytt\RobotsTxtParser\Core\UrlParser;
7
8
/**
9
 * Class CleanParam
10
 *
11
 * @package vipnytt\RobotsTxtParser\Core\Directives
12
 */
13
class CleanParam implements DirectiveInterface, RobotsTxtInterface
14
{
15
    use Toolbox;
16
    use UrlParser;
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(preg_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
                (
66
                    mb_stripos($path, "?$param=") ||
67
                    mb_stripos($path, "&$param=")
68
                ) &&
69
                $this->checkPath($path, $paths)
70
            ) {
71
                return true;
72
            }
73
        }
74
        return false;
75
    }
76
77
    /**
78
     * Export rules
79
     *
80
     * @return string[][][]
81
     */
82
    public function export()
83
    {
84
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
85
    }
86
87
    /**
88
     * Render
89
     *
90
     * @return string[]
91
     */
92
    public function render()
93
    {
94
        $result = [];
95
        foreach ($this->array as $param => $paths) {
96
            foreach ($paths as $path) {
97
                $result[] = self::DIRECTIVE . ':' . $param . ' ' . $path;
98
            }
99
        }
100
        return $result;
101
    }
102
}
103