Date::getMonth()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 5
nop 0
dl 0
loc 15
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * php-gedcom.
4
 *
5
 * php-gedcom is a library for parsing, manipulating, importing and exporting
6
 * GEDCOM 5.5 files in PHP 5.3+.
7
 *
8
 * @author          Kristopher Wilson <[email protected]>
9
 * @copyright       Copyright (c) 2010-2013, Kristopher Wilson
10
 * @license         MIT
11
 *
12
 * @link            http://github.com/mrkrstphr/php-gedcom
13
 */
14
15
namespace PhpGedcom\Record;
16
17
use PhpGedcom\Record;
18
19
/**
20
 * Class Date.
21
 */
22
class Date extends Record
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $date = null;
28
29
    /**
30
     * @var array
31
     */
32
    private $months = [
33
        'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6,
34
        'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' => 10, 'NOV' => 11, 'DEC' => 12,
35
    ];
36
37
    /**
38
     * @param string $date Date array
39
     *
40
     * @return Date
41
     */
42
    public function setDate($date)
43
    {
44
        $this->date = $date;
45
46
        return $this;
47
    }
48
49
    /**
50
     * @return null|string
51
     */
52
    public function getDate()
53
    {
54
        return $this->date;
55
    }
56
57
    /**
58
     * Return month part of date.
59
     *
60
     * @return int|null
61
     */
62
    public function getMonth()
63
    {
64
        $record = explode(' ', $this->date);
65
        if (count($record) > 0) {
66
            if ($this->isPrefix($record[0])) {
67
                unset($record[0]);
68
            }
69
            foreach ($record as $part) {
70
                if (isset($this->months[trim($part)])) {
71
                    return $this->months[trim($part)];
72
                }
73
            }
74
        }
75
76
        return null;
77
    }
78
79
    /**
80
     * Return year part of date.
81
     *
82
     * @return int|null
83
     */
84
    public function getYear()
85
    {
86
        $record = explode(' ', $this->date);
87
        if (count($record) > 0) {
88
            if ($this->isPrefix($record[0])) {
89
                unset($record[0]);
90
            }
91
            if (count($record) > 0) {
92
                return (int) end($record);
93
            }
94
        }
95
96
        return null;
97
    }
98
99
    /**
100
     * Return day part of date.
101
     *
102
     * @return int|null
103
     */
104
    public function getDay()
105
    {
106
        $record = explode(' ', $this->date);
107
        if (!empty($record[0])) {
108
            if ($this->isPrefix($record[0])) {
109
                unset($record[0]);
110
            }
111
            if (count($record) > 0) {
112
                $day = (int) reset($record);
113
                if ($day >= 1 && $day <= 31) {
114
                    return $day;
115
                }
116
            }
117
        }
118
119
        return null;
120
    }
121
122
    /**
123
     * Check if the first part is a prefix (eg 'BEF', 'ABT',).
124
     *
125
     * @param string $datePart Date part to be checked
126
     *
127
     * @return bool
128
     */
129
    private function isPrefix($datePart)
130
    {
131
        return in_array($datePart, ['FROM', 'TO', 'BEF', 'AFT', 'BET', 'AND', 'ABT', 'EST', 'CAL', 'INT']);
132
    }
133
}
134