RenderHandler::lineBreak()   A
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2222
c 0
b 0
f 0
cc 6
nc 3
nop 0
1
<?php
2
/**
3
 * vipnytt/RobotsTxtParser
4
 *
5
 * @link https://github.com/VIPnytt/RobotsTxtParser
6
 * @license https://github.com/VIPnytt/RobotsTxtParser/blob/master/LICENSE The MIT License (MIT)
7
 */
8
9
namespace vipnytt\RobotsTxtParser\Handler;
10
11
use vipnytt\RobotsTxtParser\Parser\RobotsTxtParser;
12
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
13
14
/**
15
 * Class RenderHandler
16
 *
17
 * @package vipnytt\RobotsTxtParser\Handler
18
 */
19
class RenderHandler implements RobotsTxtInterface
20
{
21
    /**
22
     * Render level
23
     * @var int
24
     */
25
    private $level;
26
27
    /**
28
     * Line separator
29
     * @var string
30
     */
31
    private $eol;
32
33
    /**
34
     * Rule strings
35
     * @var string[]
36
     */
37
    private $strings = [];
38
39
    /**
40
     * Current directive
41
     * @var string
42
     */
43
    private $directive;
44
45
    /**
46
     * Previous directive
47
     * @var string
48
     */
49
    private $previous;
50
51
    /**
52
     * Previous root level directive
53
     * @var string
54
     */
55
    private $previousRoot;
56
57
    /**
58
     * RenderHandler constructor.
59
     *
60
     * @param int $level
61
     * @param string $lineSeparator
62
     */
63
    public function __construct($level, $lineSeparator = "\r\n")
64
    {
65
        $this->level = $level;
66
        $this->separatorCheck($lineSeparator);
67
    }
68
69
    /**
70
     * Line separator check
71
     *
72
     * @param string $eol
73
     * @throws \InvalidArgumentException
74
     */
75
    private function separatorCheck($eol)
76
    {
77
        if (!in_array($eol, [
78
            "\r\n",
79
            "\n",
80
            "\r",
81
        ])
82
        ) {
83
            throw new \InvalidArgumentException('Invalid line separator');
84
        }
85
        $this->eol = $eol;
86
    }
87
88
    /**
89
     * Get mode
90
     *
91
     * @return int
92
     */
93
    public function getLevel()
94
    {
95
        return $this->level;
96
    }
97
98
    /**
99
     * Export
100
     *
101
     * @return string[]
102
     */
103
    public function export()
104
    {
105
        return $this->strings;
106
    }
107
108
    /**
109
     * Add line
110
     *
111
     * @param string $directive
112
     * @param string $line
113
     * @return bool
114
     */
115
    public function add($directive, $line)
116
    {
117
        $this->previous = $this->directive;
118
        $this->directive = $directive;
119
        $this->strings[] = rtrim($this->prefix() . $line);
120
        return true;
121
    }
122
123
    /**
124
     * Advanced beautifier
125
     *
126
     * @return string
127
     */
128
    private function prefix()
129
    {
130
        $result = $this->lineBreak() . $this->directive . ':';
131
        if ($this->level === 0) {
132
            return $result;
133
        }
134
        return ucwords($result) . ' ';
135
    }
136
137
    /**
138
     * Returns an line separator if required
139
     *
140
     * @return string
141
     */
142
    private function lineBreak()
143
    {
144
        if ($this->previousRoot !== $this->directive &&
145
            in_array($this->directive, array_keys(RobotsTxtParser::TOP_LEVEL_DIRECTIVES)) ||
146
            $this->previous !== self::DIRECTIVE_USER_AGENT &&
147
            $this->directive === self::DIRECTIVE_USER_AGENT
148
        ) {
149
            $this->previousRoot = $this->directive;
150
            if ($this->level >= 1) {
151
                return $this->eol;
152
            }
153
        }
154
        return '';
155
    }
156
157
    /**
158
     * Generate robots.txt
159
     *
160
     * @return string
161
     */
162
    public function generate()
163
    {
164
        return trim(implode($this->eol, $this->strings), $this->eol);
165
    }
166
}
167