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

UnavailableAfter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 7
c 1
b 1
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
/**
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