Completed
Push — master ( 5753af...41dae5 )
by Jan-Petter
06:59
created

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