Completed
Push — 2.0-dev ( 2c252e...97b412 )
by Jan-Petter
02:13
created

DelayParser::client()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Parser\Directives;
3
4
use vipnytt\RobotsTxtParser\Client\Directives\DelayClient;
5
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
6
7
/**
8
 * Class DelayParser
9
 *
10
 * @package vipnytt\RobotsTxtParser\Parser\Directives
11
 */
12
class DelayParser implements ParserInterface, RobotsTxtInterface
13
{
14
    use DirectiveParserCommons;
15
16
    /**
17
     * Directive
18
     * @var string
19
     */
20
    private $directive;
21
22
    /**
23
     * Base Uri
24
     * @var string
25
     */
26
    private $base;
27
28
    /**
29
     * User-agent
30
     * @var string
31
     */
32
    private $userAgent;
33
34
    /**
35
     * Delay
36
     * @var float|int
37
     */
38
    private $value;
39
40
    /**
41
     * DelayParser constructor.
42
     *
43
     * @param string $base
44
     * @param string $userAgent
45
     * @param string $directive
46
     */
47
    public function __construct($base, $userAgent, $directive)
48
    {
49
        $this->base = $base;
50
        $this->userAgent = $userAgent;
51
        $this->directive = $this->validateDirective($directive, [self::DIRECTIVE_CRAWL_DELAY, self::DIRECTIVE_CACHE_DELAY]);
52
    }
53
54
    /**
55
     * Add
56
     *
57
     * @param float|int|string $line
58
     * @return bool
59
     */
60
    public function add($line)
61
    {
62
        if (
63
            !is_numeric($line) ||
64
            (
65
                isset($this->value) &&
66
                $this->value > 0
67
            )
68
        ) {
69
            return false;
70
        }
71
        // PHP hack to convert numeric string to float or int
72
        // http://stackoverflow.com/questions/16606364/php-cast-string-to-either-int-or-float
73
        $this->value = $line + 0;
74
        return true;
75
    }
76
77
    /**
78
     * Client
79
     *
80
     * @param float|int $fallbackValue
81
     * @return DelayClient
82
     */
83
    public function client($fallbackValue = 0)
84
    {
85
        return new DelayClient($this->base, $this->userAgent, $this->value, $fallbackValue);
86
    }
87
88
    /**
89
     * Render
90
     *
91
     * @return string[]
92
     */
93
    public function render()
94
    {
95
        return empty($this->value) ? [] : [$this->directive . ':' . $this->value];
96
    }
97
}
98