Completed
Push — master ( b23c5f...0489e6 )
by Jan-Petter
02:23
created

RobotsTxtParser::parseLine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser;
3
4
use vipnytt\RobotsTxtParser\Parser\Directives\DirectiveParserCommons;
5
use vipnytt\RobotsTxtParser\Parser\Directives\RootDirectiveHandler;
6
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
7
8
/**
9
 * Class RobotsTxtParser
10
 *
11
 * @package vipnytt\RobotsTxtParser\Parser
12
 */
13
class RobotsTxtParser implements RobotsTxtInterface
14
{
15
    use DirectiveParserCommons;
16
    use UrlParser;
17
18
    /**
19
     * Directive white list
20
     */
21
    const TOP_LEVEL_DIRECTIVES = [
22
        self::DIRECTIVE_CLEAN_PARAM => 'cleanParam',
23
        self::DIRECTIVE_HOST => 'host',
24
        self::DIRECTIVE_SITEMAP => 'sitemap',
25
    ];
26
27
    /**
28
     * Root directive handler
29
     * @var RootDirectiveHandler
30
     */
31
    protected $handler;
32
33
    /**
34
     * TxtClient constructor.
35
     *
36
     * @param string $baseUri
37
     * @param string $content
38
     */
39
    public function __construct($baseUri, $content)
40
    {
41
        mb_internal_encoding(self::ENCODING);
42
        $this->handler = new RootDirectiveHandler($this->urlBase($this->urlEncode($baseUri)));
43
        $this->parseTxt($content);
44
    }
45
46
    /**
47
     * Client robots.txt
48
     *
49
     * @param string $txt
50
     * @return bool
51
     */
52
    private function parseTxt($txt)
53
    {
54
        $result = [];
55
        $lines = array_filter(array_map('trim', mb_split('\r\n|\n|\r', $txt)));
56
        // Client each line individually
57
        foreach ($lines as $line) {
58
            // Limit rule length
59
            $line = mb_substr($line, 0, self::MAX_LENGTH_RULE);
60
            // Remove comments
61
            $line = mb_split('#', $line, 2)[0];
62
            // Parse line
63
            $result[] = $this->parseLine($line);
64
        }
65
        return in_array(true, $result, true);
66
    }
67
68
    /**
69
     * Add line
70
     *
71
     * @param string $line
72
     * @return bool
73
     */
74
    private function parseLine($line)
75
    {
76
        if (($pair = $this->generateRulePair($line, array_keys(self::TOP_LEVEL_DIRECTIVES))) !== false) {
77
            return $this->handler->{self::TOP_LEVEL_DIRECTIVES[$pair['directive']]}()->add($pair['value']);
78
        }
79
        return $this->handler->userAgent()->add($line);
80
    }
81
82
    /**
83
     * Render
84
     *
85
     * @param string $lineSeparator
86
     * @return string
87
     */
88
    public function render($lineSeparator = "\n")
89
    {
90
        return implode($lineSeparator, array_merge(
91
            $this->handler->host()->render(),
92
            $this->handler->cleanParam()->render(),
93
            $this->handler->sitemap()->render(),
94
            $this->handler->userAgent()->render()
95
        ));
96
    }
97
98
    /**
99
     * Rule array
100
     *
101
     * @return array
102
     */
103
    public function export()
104
    {
105
        return [
106
            self::DIRECTIVE_HOST => $this->handler->host()->client()->export(),
107
            self::DIRECTIVE_CLEAN_PARAM => $this->handler->cleanParam()->client()->export(),
108
            self::DIRECTIVE_SITEMAP => $this->handler->sitemap()->client()->export(),
109
            self::DIRECTIVE_USER_AGENT => $this->handler->userAgent()->export(),
110
        ];
111
    }
112
}
113