RenderClient::compatibility()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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 $eol
47
     * @return string
48
     */
49
    public function compatibility($eol = "\r\n")
50
    {
51
        return $this->generate(2, $eol) . $eol;
52
    }
53
54
    /**
55
     * Generate an rule string array
56
     *
57
     * @param int $level
58
     * @param string $eol
59
     * @return string
60
     */
61
    private function generate($level, $eol)
62
    {
63
        $handler = new RenderHandler($level, $eol);
64
        if ($level == 2) {
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 != 2) {
71
            $this->root->userAgent->render($handler);
72
        }
73
        return $handler->generate();
74
    }
75
76
    /**
77
     * 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
     * - Easy to edit
83
     * - User-agent groups are separated with blank lines
84
     *
85
     * @param string $eol
86
     * @return string
87
     */
88
    public function normal($eol = "\r\n")
89
    {
90
        return $this->generate(1, $eol);
91
    }
92
93
    /**
94
     * Compressed robots.txt
95
     *
96
     * Generates an robots.txt compressed to a absolute minimum.
97
     * Best suited for parsing purposes and storage in databases.
98
     *
99
     * @param string $eol
100
     * @return string
101
     */
102
    public function compressed($eol = PHP_EOL)
103
    {
104
        return $this->generate(0, $eol);
105
    }
106
}
107