Completed
Push — master ( 74ab5d...88dd1b )
by Jan-Petter
04:03
created

AllowParser::normalize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
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\Parser\Directives;
10
11
use vipnytt\RobotsTxtParser\Client\Directives\AllowClient;
12
use vipnytt\RobotsTxtParser\Handler\RenderHandler;
13
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
14
15
/**
16
 * Class AllowParser
17
 *
18
 * @package vipnytt\RobotsTxtParser\Parser\Directives
19
 */
20
class AllowParser implements ParserInterface, RobotsTxtInterface
21
{
22
    /**
23
     * Directive
24
     * @var string
25
     */
26
    private $directive;
27
28
    /**
29
     * Path
30
     * @var array
31
     */
32
    private $path = [];
33
34
    /**
35
     * Sort result
36
     * @var bool
37
     */
38
    private $sort = false;
39
40
    /**
41
     * AllowParser constructor
42
     *
43
     * @param string $directive
44
     */
45
    public function __construct($directive)
46
    {
47
        $this->directive = $directive;
48
    }
49
50
    /**
51
     * Add
52
     *
53
     * @param string $line
54
     * @return bool
55
     */
56
    public function add($line)
57
    {
58
        $line = $this->normalize($line);
59
        if (substr($line, 0, 1) == '/' &&
60
            !in_array($line, $this->path)
61
        ) {
62
            $this->path[] = $line;
63
            return true;
64
        }
65
        return false;
66
    }
67
68
    /**
69
     * Normalize rules
70
     *
71
     * @param $line
72
     * @return string
73
     */
74
    private function normalize($line)
75
    {
76
        // Prepend slash if starting with an wildcard
77
        if (substr($line, 0, 1) == '*') {
78
            $line = '/' . $line;
79
        }
80
        // Remove unnecessary characters after an end anchor
81
        if (($pos = mb_strpos($line, '$')) !== false) {
82
            $line = mb_substr($line, 0, $pos + 1);
83
        }
84
        // Remove unnecessary wildcards
85
        $line = rtrim($line, '*');
86
        return $line;
87
    }
88
89
    /**
90
     * Render
91
     *
92
     * @param RenderHandler $handler
93
     * @return bool
94
     */
95
    public function render(RenderHandler $handler)
96
    {
97
        if ($this->directive === self::DIRECTIVE_DISALLOW &&
98
            count($this->path) === 0 &&
99
            $handler->getLevel() == 2
100
        ) {
101
            $handler->add($this->directive, '');
102
            return true;
103
        }
104
        $this->sort();
105
        foreach ($this->path as $path) {
106
            $handler->add($this->directive, $path);
107
        }
108
        return true;
109
    }
110
111
    /**
112
     * Sort by length
113
     *
114
     * @return bool
115
     */
116
    private function sort()
117
    {
118
        if ($this->sort) {
119
            return $this->sort;
120
        };
121
        return $this->sort = rsort($this->path) && usort($this->path, function ($a, $b) {
122
                // PHP 7: Switch to the <=> "Spaceship" operator
123
                return mb_strlen($a) - mb_strlen($b);
124
            });
125
    }
126
127
    /**
128
     * Client
129
     *
130
     * @return AllowClient
131
     */
132
    public function client()
133
    {
134
        $this->sort();
135
        return new AllowClient($this->path);
136
    }
137
}
138