Completed
Push — master ( 902e66...0cbd7a )
by Jan-Petter
02:44
created

CleanParamParserCore::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\Parser\UriParser;
5
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
6
7
/**
8
 * Class CleanParamParserCore
9
 *
10
 * @package vipnytt\RobotsTxtParser\Parser\Directives
11
 */
12
abstract class CleanParamParserCore implements ParserInterface, RobotsTxtInterface
13
{
14
    /**
15
     * Clean-param array
16
     * @var string[][]
17
     */
18
    protected $cleanParam = [];
19
20
    /**
21
     * CleanParamParserCore constructor.
22
     */
23
    public function __construct()
24
    {
25
    }
26
27
    /**
28
     * Add
29
     *
30
     * @param string $line
31
     * @return bool
32
     */
33
    public function add($line)
34
    {
35
        // split into parameter and path
36
        $array = array_map('trim', mb_split('\s+', $line, 2));
37
        // strip any invalid characters from path prefix
38
        $path = '/';
39
        if (isset($array[1])) {
40
            $uriParser = new UriParser(preg_replace('/[^A-Za-z0-9\.-\/\*\_]/', '', $array[1]));
41
            $path = $uriParser->encode();
42
        }
43
        $param = array_map('trim', explode('&', $array[0]));
44
        foreach ($param as $key) {
45
            $this->cleanParam[$key][] = $path;
46
        }
47
        return true;
48
    }
49
50
    /**
51
     * Render
52
     *
53
     * @return string[]
54
     */
55
    public function render()
56
    {
57
        $result = [];
58
        foreach ($this->cleanParam as $param => $paths) {
59
            foreach ($paths as $path) {
60
                $result[] = self::DIRECTIVE_CLEAN_PARAM . ':' . $param . ' ' . $path;
61
            }
62
        }
63
        sort($result);
64
        return $result;
65
    }
66
}
67