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

UnavailableAfter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
c 4
b 0
f 0
lcom 1
cbo 0
dl 0
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDirective() 0 4 1
A getValue() 0 14 4
A getMeaning() 0 4 1
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