Completed
Push — master ( 0489e6...065a8d )
by Jan-Petter
04:09
created

AllowParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\Client\Directives\AllowClient;
5
use vipnytt\RobotsTxtParser\Exceptions;
6
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
7
8
/**
9
 * Class AllowParser
10
 *
11
 * @package vipnytt\RobotsTxtParser\Parser\Directives
12
 */
13
class AllowParser implements ParserInterface, RobotsTxtInterface
14
{
15
    use DirectiveParserCommons;
16
17
    /**
18
     * Supported directives
19
     */
20
    const SUPPORTED_DIRECTIVES = [
21
        self::DIRECTIVE_ALLOW,
22
        self::DIRECTIVE_DISALLOW,
23
        self::DIRECTIVE_NO_INDEX,
24
    ];
25
26
    /**
27
     * Sub directives white list
28
     */
29
    const SUB_DIRECTIVES = [
30
        self::DIRECTIVE_CLEAN_PARAM,
31
        self::DIRECTIVE_HOST,
32
    ];
33
34
    /**
35
     * Directive
36
     * @var string
37
     */
38
    private $directive;
39
40
    /**
41
     * Path
42
     * @var array
43
     */
44
    private $path = [];
45
46
    /**
47
     * Sub-directive Clean-param
48
     * @var CleanParamParser
49
     */
50
    private $cleanParam;
51
52
    /**
53
     * Sub-directive Host
54
     * @var HostParser
55
     */
56
    private $host;
57
58
    /**
59
     * AllowParser constructor
60
     *
61
     * @param string $base
62
     * @param string $effective
63
     * @param string $directive
64
     */
65
    public function __construct($base, $effective, $directive)
66
    {
67
        $this->directive = $this->validateDirective($directive, self::SUPPORTED_DIRECTIVES);
68
        $this->cleanParam = new CleanParamParser();
69
        $this->host = new HostParser($base, $effective, $this->directive);
70
    }
71
72
    /**
73
     * Add
74
     *
75
     * @param string $line
76
     * @return bool
77
     */
78
    public function add($line)
79
    {
80
        $pair = $this->generateRulePair($line, self::SUB_DIRECTIVES);
81
        switch ($pair['directive']) {
82
            case self::DIRECTIVE_CLEAN_PARAM:
83
                return $this->cleanParam->add($pair['value']);
84
            case self::DIRECTIVE_HOST:
85
                return $this->host->add($pair['value']);
86
        }
87
        return $this->addPath($line);
88
    }
89
90
    /**
91
     * Add plain path to allow/disallow
92
     *
93
     * @param string $path
94
     * @return bool
95
     */
96
    private function addPath($path)
97
    {
98
        if (in_array($path, $this->path)) {
99
            return false;
100
        }
101
        $this->path[] = $path;
102
        return true;
103
    }
104
105
    /**
106
     * Render
107
     *
108
     * @return string[]
109
     */
110
    public function render()
111
    {
112
        $result = [];
113
        $values = array_merge(
114
            $this->host->render(),
115
            $this->path,
116
            $this->cleanParam->render()
117
        );
118
        foreach ($values as $value) {
119
            $result[] = $this->directive . ':' . $value;
120
        }
121
        sort($result);
122
        return $result;
123
    }
124
125
    /**
126
     * Client
127
     *
128
     * @return AllowClient
129
     */
130
    public function client()
131
    {
132
        return new AllowClient($this->path, $this->host->client(), $this->cleanParam->client());
133
    }
134
}
135