Completed
Push — master ( 1b6743...e69112 )
by Jan-Petter
9s
created

CleanParamParser::client()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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
     * Clean-param array
20
     * @var string[][]
21
     */
22
    private $cleanParam = [];
23
24
    /**
25
     * CleanParam constructor.
26
     */
27
    public function __construct()
28
    {
29
    }
30
31
    /**
32
     * Add
33
     *
34
     * @param string $line
35
     * @return bool
36
     */
37
    public function add($line)
38
    {
39
        // split into parameter and path
40
        $array = array_map('trim', mb_split('\s+', $line, 2));
41
        // strip any invalid characters from path prefix
42
        $path = isset($array[1]) ? $this->urlEncode(preg_replace('/[^A-Za-z0-9\.-\/\*\_]/', '', $array[1])) : "/";
43
        $param = array_map('trim', mb_split('&', $array[0]));
44
        foreach ($param as $key) {
45
            $this->cleanParam[$key][] = $path;
46
        }
47
        return true;
48
    }
49
50
    /**
51
     * Client
52
     *
53
     * @return CleanParamClient
54
     */
55
    public function client()
56
    {
57
        return new CleanParamClient($this->cleanParam);
58
    }
59
60
    /**
61
     * Render
62
     *
63
     * @return string[]
64
     */
65
    public function render()
66
    {
67
        $result = [];
68
        foreach ($this->cleanParam as $param => $paths) {
69
            foreach ($paths as $path) {
70
                $result[] = self::DIRECTIVE_CLEAN_PARAM . ':' . $param . ' ' . $path;
71
            }
72
        }
73
        sort($result);
74
        return $result;
75
    }
76
}
77