Completed
Branch 2.0-dev (4f313a)
by Jan-Petter
02:57
created

Core::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 5
1
<?php
2
namespace vipnytt\RobotsTxtParser;
3
4
use vipnytt\RobotsTxtParser\Client\Directives\CleanParamClient;
5
use vipnytt\RobotsTxtParser\Client\Directives\HostClient;
6
use vipnytt\RobotsTxtParser\Client\Directives\SitemapClient;
7
use vipnytt\RobotsTxtParser\Client\Directives\UserAgentClient;
8
use vipnytt\RobotsTxtParser\Client\Encoding\EncodingConverter;
9
use vipnytt\RobotsTxtParser\Parser\RobotsTxtParser;
10
use vipnytt\RobotsTxtParser\Parser\UrlParser;
11
12
/**
13
 * Class Core
14
 *
15
 * @link https://developers.google.com/webmasters/control-crawl-index/docs/robots_txt
16
 * @link https://yandex.com/support/webmaster/controlling-robot/robots-txt.xml
17
 * @link http://www.robotstxt.org/robotstxt.html
18
 * @link https://www.w3.org/TR/html4/appendix/notes.html#h-B.4.1.1
19
 * @link http://www.conman.org/people/spc/robots2.html
20
 *
21
 * @package vipnytt\RobotsTxtParser
22
 */
23
class Core extends RobotsTxtParser
24
{
25
    use UrlParser;
26
27
    /**
28
     * Base uri
29
     * @var string
30
     */
31
    protected $base;
32
33
    /**
34
     * Status code
35
     * @var int|null
36
     */
37
    protected $statusCode;
38
39
    /**
40
     * Robots.txt content
41
     * @var string
42
     */
43
    protected $content;
44
45
    /**
46
     * Core constructor.
47
     *
48
     * @param string $baseUri
49
     * @param int $statusCode
50
     * @param string|null $content
51
     * @param string $encoding
52
     * @param int|null $byteLimit
53
     */
54
    public function __construct($baseUri, $statusCode, $content, $encoding = self::ENCODING, $byteLimit = self::BYTE_LIMIT)
55
    {
56
        $this->base = $this->urlBase($this->urlEncode($baseUri));
57
        $this->statusCode = $statusCode;
58
        $this->content = $content;
59
        $this->convertEncoding($encoding);
60
        $this->limitBytes($byteLimit);
61
        parent::__construct($this->base, $this->content);
62
    }
63
64
    /**
65
     * Convert character encoding
66
     *
67
     * @param string $encoding
68
     * @return string
69
     */
70
    private function convertEncoding($encoding)
71
    {
72
        mb_internal_encoding(self::ENCODING);
73
        $convert = new EncodingConverter($this->content, $encoding);
74
        if (($result = $convert->auto()) !== false) {
75
            return $this->content = $result;
76
        }
77
        return $this->content;
78
    }
79
80
    /**
81
     * Byte limit
82
     *
83
     * @param int|null $bytes
84
     * @return string
85
     */
86
    private function limitBytes($bytes)
87
    {
88
        if ($bytes === null) {
89
            return $this->content;
90
        } elseif ($bytes < 5000) {
91
            trigger_error('Byte limit is set dangerously low!', E_USER_WARNING);
92
        }
93
        return $this->content = mb_strcut($this->content, 0, intval($bytes));
94
    }
95
96
    /**
97
     * Clean-param
98
     *
99
     * @return CleanParamClient
100
     */
101
    public function cleanParam()
102
    {
103
        return $this->handler->cleanParam()->client();
104
    }
105
106
    /**
107
     * Host
108
     *
109
     * @return HostClient
110
     */
111
    public function host()
112
    {
113
        return $this->handler->host()->client();
114
    }
115
116
    /**
117
     * Sitemaps
118
     *
119
     * @return SitemapClient
120
     */
121
    public function sitemap()
122
    {
123
        return $this->handler->sitemap()->client();
124
    }
125
126
    /**
127
     * Get User-agent list
128
     *
129
     * @return string[]
130
     */
131
    public function getUserAgents()
132
    {
133
        return $this->handler->userAgent()->getUserAgents();
134
    }
135
136
    /**
137
     * Client User-agent specific rules
138
     *
139
     * @param string $string
140
     * @return UserAgentClient
141
     */
142
    public function userAgent($string = self::USER_AGENT)
143
    {
144
        return $this->handler->userAgent()->client($string, $this->statusCode);
145
    }
146
}
147