Completed
Push — master ( 0cbd7a...258693 )
by Jan-Petter
04:51
created

DirectiveParserTrait::draftParseTime()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 9
nc 2
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 DateTimeZone;
12
13
/**
14
 * Class DirectiveParserCommons
15
 *
16
 * @package vipnytt\RobotsTxtParser\Directive
17
 */
18
trait DirectiveParserTrait
19
{
20
    /**
21
     * Generate directive/rule pair
22
     *
23
     * @param string $line
24
     * @param string[] $whiteList
25
     * @return string[]|false
26
     */
27
    private function generateRulePair($line, array $whiteList)
28
    {
29
        $whiteList = array_map('strtolower', $whiteList);
30
        // Split by directive and rule
31
        $pair = array_map('trim', explode(':', $line, 2));
32
        // Check if the line contains a rule
33
        if (empty($pair[1]) ||
34
            empty($pair[0]) ||
35
            !in_array(($pair[0] = str_ireplace(array_keys(self::ALIAS_DIRECTIVES), array_values(self::ALIAS_DIRECTIVES), strtolower($pair[0]))), $whiteList)
36
        ) {
37
            // Line does not contain any supported directive
38
            return false;
39
        }
40
        return [
41
            'directive' => $pair[0],
42
            'value' => $pair[1],
43
        ];
44
    }
45
46
    /**
47
     * Client rate as specified in the `Robot exclusion standard` version 2.0 draft
48
     * rate = numDocuments / timeUnit
49
     * @link http://www.conman.org/people/spc/robots2.html#format.directives.request-rate
50
     *
51
     * @param string $string
52
     * @return float|int|false
53
     */
54
    private function draftParseRate($string)
55
    {
56
        $parts = array_map('trim', explode('/', $string));
57
        if (count($parts) != 2) {
58
            return false;
59
        }
60
        $multiplier = 1;
61
        switch (strtolower(substr(preg_replace('/[^A-Za-z]/', '', $parts[1]), 0, 1))) {
62
            case 'm':
63
                $multiplier = 60;
64
                break;
65
            case 'h':
66
                $multiplier = 3600;
67
                break;
68
            case 'd':
69
                $multiplier = 86400;
70
                break;
71
        }
72
        $num = floatval(preg_replace('/[^0-9]/', '', $parts[0]));
73
        $sec = floatval(preg_replace('/[^0-9.]/', '', $parts[1])) * $multiplier;
74
        $rate = $sec / $num;
75
        return $rate > 0 ? $rate : false;
76
    }
77
78
    /**
79
     * Client timestamp range as specified in the `Robot exclusion standard` version 2.0 draft
80
     * @link http://www.conman.org/people/spc/robots2.html#format.directives.visit-time
81
     *
82
     * @param $string
83
     * @return string[]|false
84
     */
85
    private function draftParseTime($string)
86
    {
87
        $array = preg_replace('/[^0-9]/', '', explode('-', $string, 3));
88
        if (count($array) !== 2 ||
89
            ($fromTime = date_create_from_format('Hi', $array[0], $dtz = new DateTimeZone('UTC'))) === false ||
90
            ($toTime = date_create_from_format('Hi', $array[1], $dtz)) === false
91
        ) {
92
            return false;
93
        }
94
        return [
95
            'from' => date_format($fromTime, 'Hi'),
96
            'to' => date_format($toTime, 'Hi'),
97
        ];
98
    }
99
}
100