Completed
Push — master ( 2c377d...bb4c66 )
by Jan-Petter
02:06
created

UnavailableAfter::getDirective()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace vipnytt\XRobotsTagParser\Directives;
3
4
/**
5
 * Class UnavailableAfter
6
 *
7
 * @package vipnytt\XRobotsTagParser\Directives
8
 */
9
final class UnavailableAfter implements DirectiveInterface
10
{
11
    const DIRECTIVE = 'unavailable_after';
12
    const MEANING = 'Do not show this page in search results after the specified date/time.';
13
14
    const DATE_GOOGLE = 'd M Y H:i:s T';
15
16
    private $supportedDateFormats = [
17
        DATE_RFC850,
18
        self::DATE_GOOGLE
19
    ];
20
21
    private $value;
22
23
    /**
24
     * Constructor
25
     *
26
     * @param string $rule
27
     */
28
    public function __construct($rule)
29
    {
30
        $this->value = $rule;
31
    }
32
33
    /**
34
     * Get directive name
35
     *
36
     * @return string
37
     */
38
    public function getDirective()
39
    {
40
        return self::DIRECTIVE;
41
    }
42
43
    /**
44
     * Get value
45
     *
46
     * @return string|null
47
     */
48
    public function getValue()
49
    {
50
        $parts = mb_split(',', trim(substr($this->value, mb_stripos($this->value, self::DIRECTIVE) + mb_strlen(self::DIRECTIVE) + 1)));
51
        $count = count($parts);
52
        for ($i = 1; $i <= $count; $i++) {
53
            foreach ($this->supportedDateFormats as $format) {
54
                $dateTime = date_create_from_format($format, trim(implode(',', array_slice($parts, 0, $i))));
55
                if ($dateTime !== false) {
56
                    return date_format($dateTime, DATE_RFC850);
57
                }
58
            }
59
        }
60
        return null;
61
    }
62
63
    /**
64
     * Get directive meaning
65
     *
66
     * @return string
67
     */
68
    public function getMeaning()
69
    {
70
        return self::MEANING;
71
    }
72
}
73
74