Completed
Push — master ( 49e9a0...fa7091 )
by Jan-Petter
02:11
created

CrawlDelay::add()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.2
cc 4
eloc 7
nc 3
nop 1
1
<?php
2
namespace vipnytt\RobotsTxtParser\Modules\Directives;
3
4
use vipnytt\RobotsTxtParser\Exceptions\ParserException;
5
use vipnytt\RobotsTxtParser\Modules\Toolbox;
6
use vipnytt\RobotsTxtParser\RobotsTxtInterface;
7
8
/**
9
 * Class CrawlDelay
10
 *
11
 * @package vipnytt\RobotsTxtParser\Modules\Directives
12
 */
13
class CrawlDelay implements DirectiveInterface, RobotsTxtInterface
14
{
15
    use Toolbox;
16
17
    /**
18
     * Directive alternatives
19
     */
20
    const DIRECTIVE = [
21
        self::DIRECTIVE_CACHE_DELAY,
22
        self::DIRECTIVE_CRAWL_DELAY,
23
    ];
24
25
    /**
26
     * Directive
27
     */
28
    protected $directive = self::DIRECTIVE_CRAWL_DELAY;
29
30
    /**
31
     * Delay
32
     * @var float|int
33
     */
34
    protected $value;
35
36
    /**
37
     * CrawlDelay constructor.
38
     * @param string $directive
39
     * @throws ParserException
40
     */
41
    public function __construct($directive = self::DIRECTIVE_CRAWL_DELAY)
42
    {
43
        $this->directive = $this->validateDirective($directive, self::DIRECTIVE);
44
    }
45
46
    /**
47
     * Add
48
     *
49
     * @param float|int|string $line
50
     * @return bool
51
     */
52
    public function add($line)
53
    {
54
        if (isset($this->value) && $this->value > 0) {
55
            return false;
56
        }
57
        if (empty($float = floatval($line))) {
58
            return false;
59
        }
60
        $this->value = $float;
61
        return true;
62
    }
63
64
    /**
65
     * Export
66
     *
67
     * @return array
68
     */
69
    public function export()
70
    {
71
        return empty($this->value) ? [] : [$this->directive => $this->value];
72
    }
73
}
74