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

Core::add()   C

Complexity

Conditions 7
Paths 15

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
rs 6.7272
cc 7
eloc 18
nc 15
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser;
3
4
use vipnytt\RobotsTxtParser\Core\Directives\CleanParam;
5
use vipnytt\RobotsTxtParser\Core\Directives\Host;
6
use vipnytt\RobotsTxtParser\Core\Directives\Sitemap;
7
use vipnytt\RobotsTxtParser\Core\Directives\UserAgent;
8
use vipnytt\RobotsTxtParser\Core\RobotsTxtInterface;
9
use vipnytt\RobotsTxtParser\Core\Toolbox;
10
11
/**
12
 * Class Core
13
 *
14
 * @package vipnytt\RobotsTxtParser
15
 */
16
class Core implements RobotsTxtInterface
17
{
18
    use Toolbox;
19
20
    /**
21
     * Directive white list
22
     */
23
    const TOP_LEVEL_DIRECTIVES = [
24
        self::DIRECTIVE_CLEAN_PARAM,
25
        self::DIRECTIVE_HOST,
26
        self::DIRECTIVE_SITEMAP,
27
        self::DIRECTIVE_USER_AGENT,
28
    ];
29
30
    /**
31
     * Previous directive
32
     * @var string
33
     */
34
    protected $previousDirective;
35
36
    /**
37
     * Current user-agent(s)
38
     * @var array
39
     */
40
    protected $userAgentValues;
41
42
    /**
43
     * Clean-param class
44
     * @var CleanParam
45
     */
46
    protected $cleanParam;
47
48
    /**
49
     * Host class
50
     * @var Host
51
     */
52
    protected $host;
53
54
    /**
55
     * Sitemap class
56
     * @var Sitemap
57
     */
58
    protected $sitemap;
59
60
    /**
61
     * User-agent class
62
     * @var UserAgent
63
     */
64
    protected $userAgent;
65
66
    /**
67
     * Core constructor.
68
     *
69
     * @param string $content - file content
70
     */
71
    public function __construct($content)
72
    {
73
        mb_internal_encoding(self::ENCODING);
74
        $this->cleanParam = new CleanParam();
75
        $this->host = new Host();
76
        $this->sitemap = new Sitemap();
77
        $this->userAgent = new UserAgent();
78
        $this->parseTxt($content);
79
    }
80
81
    /**
82
     * Client robots.txt
83
     *
84
     * @param string $txt
85
     * @return void
86
     */
87
    private function parseTxt($txt)
88
    {
89
        $lines = array_filter(array_map('trim', mb_split('\r\n|\n|\r', $txt)));
90
        // Client each line individually
91
        foreach ($lines as $line) {
92
            // Limit rule length
93
            $line = mb_substr($line, 0, self::MAX_LENGTH_RULE);
94
            // Remove comments
95
            $line = mb_split('#', $line, 2)[0];
96
            // Client line
97
            $this->add($line);
98
        }
99
    }
100
101
    /**
102
     * Add line
103
     *
104
     * @param string $line
105
     * @return bool
106
     */
107
    public function add($line)
108
    {
109
        $previousDirective = $this->previousDirective;
110
        $pair = $this->generateRulePair($line, self::TOP_LEVEL_DIRECTIVES);
111
        if ($pair['directive'] === self::DIRECTIVE_USER_AGENT) {
112
            if ($previousDirective !== self::DIRECTIVE_USER_AGENT) {
113
                $this->userAgentValues = [];
114
            }
115
            $this->userAgentValues[] = $pair['value'];
116
        }
117
        $this->previousDirective = $pair['directive'];
118
        switch ($pair['directive']) {
119
            case self::DIRECTIVE_CLEAN_PARAM:
120
                return $this->cleanParam->add($pair['value']);
121
            case self::DIRECTIVE_HOST:
122
                return $this->host->add($pair['value']);
123
            case self::DIRECTIVE_SITEMAP:
124
                return $this->sitemap->add($pair['value']);
125
            case self::DIRECTIVE_USER_AGENT:
126
                return $this->userAgent->set($this->userAgentValues);
127
        }
128
        return $this->userAgent->add($line);
129
    }
130
131
    /**
132
     * Render
133
     *
134
     * @param string $lineSeparator
135
     * @return string
136
     */
137
    public function render($lineSeparator = "\n")
138
    {
139
        return implode($lineSeparator, array_merge(
140
            $this->cleanParam->render(),
141
            $this->host->render(),
142
            $this->sitemap->render(),
143
            $this->userAgent->render()
144
        ));
145
    }
146
147
    /**
148
     * Export rules
149
     *
150
     * @return array
151
     */
152
    public function export()
153
    {
154
        return array_merge(
155
            $this->cleanParam->export(),
156
            $this->host->export(),
157
            $this->sitemap->export(),
158
            $this->userAgent->export()
159
        );
160
    }
161
}
162