Completed
Branch 2.0-dev (2c252e)
by Jan-Petter
02:43
created

CleanParamParser::getRules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\Client\Directives\CleanParamClient;
5
use vipnytt\RobotsTxtParser\Parser\UrlParser;
6
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
7
8
/**
9
 * Class CleanParamParser
10
 *
11
 * @package vipnytt\RobotsTxtParser\Parser\Directives
12
 */
13
class CleanParamParser implements ParserInterface, RobotsTxtInterface
14
{
15
    use DirectiveParserCommons;
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
    private $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
     * Client
57
     *
58
     * @return CleanParamClient
59
     */
60
    public function client()
61
    {
62
        return new CleanParamClient($this->array);
63
    }
64
65
    /**
66
     * Rule array
67
     *
68
     * @return string[][][]
69
     */
70
    public function getRules()
71
    {
72
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
73
    }
74
75
    /**
76
     * Render
77
     *
78
     * @return string[]
79
     */
80
    public function render()
81
    {
82
        $result = [];
83
        foreach ($this->array as $param => $paths) {
84
            foreach ($paths as $path) {
85
                $result[] = self::DIRECTIVE . ':' . $param . ' ' . $path;
86
            }
87
        }
88
        return $result;
89
    }
90
}
91