Completed
Push — master ( 0cbd7a...258693 )
by Jan-Petter
04:51
created

RenderClient::normal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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\Client;
10
11
use vipnytt\RobotsTxtParser\Handler\Directives\RootDirectiveHandler;
12
use vipnytt\RobotsTxtParser\Handler\RenderHandler;
13
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
14
15
/**
16
 * Class RenderClient
17
 *
18
 * @package vipnytt\RobotsTxtParser\Client
19
 */
20
class RenderClient implements RobotsTxtInterface
21
{
22
    /**
23
     * Handler
24
     * @var RootDirectiveHandler
25
     */
26
    private $root;
27
28
    /**
29
     * RenderClient constructor.
30
     *
31
     * @param RootDirectiveHandler $handler
32
     */
33
    public function __construct(RootDirectiveHandler $handler)
34
    {
35
        $this->root = $handler;
36
    }
37
38
    /**
39
     * Compatibility optimized robots.txt
40
     *
41
     * Differences compared to Normal:
42
     * - Each User-agent is listed with it's own separate rule set (even if it's equal to others)
43
     * - Byte consuming, may be multiple times larger (depending on the number of user-agents)
44
     * - Maximum compatibility, optimized for badly written 3rd party parsers with limited specification support
45
     *
46
     * @param string $lineSeparator
47
     * @return string
48
     */
49
    public function compatibility($lineSeparator = "\r\n")
50
    {
51
        return $this->generate(3, $lineSeparator);
52
    }
53
54
    /**
55
     * Generate an rule string array
56
     *
57
     * @param int $level
58
     * @param string $lineSeparator
59
     * @return string[]
60
     */
61
    private function generate($level, $lineSeparator)
62
    {
63
        $handler = new RenderHandler($level, $lineSeparator);
64
        if ($level === 3) {
65
            $this->root->userAgent()->render($handler);
66
        }
67
        $this->root->host()->render($handler);
68
        $this->root->cleanParam()->render($handler);
69
        $this->root->sitemap()->render($handler);
70
        if ($level !== 3) {
71
            $this->root->userAgent()->render($handler);
72
        }
73
        return $handler->generate();
74
    }
75
76
    /**
77
     * Minimal but normal looking robots.txt
78
     *
79
     * Differences compared to Compressed:
80
     * - The directives first character is uppercase
81
     * - Whitespace between the directive and it's value
82
     *
83
     * @param string $lineSeparator
84
     * @return string
85
     */
86
    public function minimal($lineSeparator = "\r\n")
87
    {
88
89
        return $this->generate(1, $lineSeparator);
90
    }
91
92
    /**
93
     * Normal looking robots.txt
94
     *
95
     * Differences compared to Compressed:
96
     * - Maximum human readability
97
     * - Easy to edit
98
     * - User-agent groups are separated with blank lines
99
     *
100
     * @param string $lineSeparator
101
     * @return string
102
     */
103
    public function normal($lineSeparator = "\r\n")
104
    {
105
        return $this->generate(2, $lineSeparator);
106
    }
107
108
    /**
109
     * Compressed robots.txt
110
     *
111
     * Generates an robots.txt compressed to a absolute minimum.
112
     * Best suited for parsing purposes and storage in databases.
113
     *
114
     * @param string $lineSeparator
115
     * @return string
116
     */
117
    public function compressed($lineSeparator = "\r\n")
118
    {
119
        return $this->generate(0, $lineSeparator);
120
    }
121
}
122