Completed
Branch 2.0-dev (131e57)
by Jan-Petter
02:08
created

RequestRate::export()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
namespace vipnytt\RobotsTxtParser\Core\Directives;
3
4
use vipnytt\RobotsTxtParser\Core\RobotsTxtInterface;
5
use vipnytt\RobotsTxtParser\Core\Toolbox;
6
7
/**
8
 * Class RequestRate
9
 *
10
 * @package vipnytt\RobotsTxtParser\Core\Directives
11
 */
12
class RequestRate implements DirectiveInterface, RobotsTxtInterface
13
{
14
    use Toolbox;
15
16
    /**
17
     * Directive
18
     */
19
    const DIRECTIVE = self::DIRECTIVE_REQUEST_RATE;
20
21
    /**
22
     * RequestRate array
23
     * @var array
24
     */
25
    protected $array = [];
26
27
    /**
28
     * RequestRate constructor.
29
     */
30
    public function __construct()
31
    {
32
    }
33
34
    /**
35
     * Add
36
     *
37
     * @param string $line
38
     * @return bool
39
     */
40
    public function add($line)
41
    {
42
        $array = preg_split('/\s+/', $line, 2);
43
        $result = [
44
            'rate' => $this->draftParseRate($array[0]),
45
        ];
46
        if ($result['rate'] === false) {
47
            return false;
48
        } elseif (
49
            !empty($array[1]) &&
50
            ($times = $this->draftParseTime($array[1])) !== false
51
        ) {
52
            $result = array_merge($result, $times);
53
        }
54
        $this->array[] = $result;
55
        return true;
56
    }
57
58
    /**
59
     * Export rules
60
     *
61
     * @return string[][]
62
     */
63
    public function export()
64
    {
65
        return empty($this->array) ? [] : [self::DIRECTIVE => $this->array];
66
    }
67
68
    /**
69
     * Render
70
     *
71
     * @return string[]
72
     */
73
    public function render()
74
    {
75
        $result = [];
76
        foreach ($this->array as $array) {
77
            $suffix = 's';
78
            if (
79
                isset($array['from']) &&
80
                isset($array['to'])
81
            ) {
82
                $suffix .= ' ' . $array['from'] . '-' . $array['to'];
83
            }
84
            $result[] = self::DIRECTIVE . ':1/' . $array['rate'] . $suffix;
85
        }
86
        return $result;
87
    }
88
}
89