Test Failed
Push — master ( 82be03...a7be7e )
by Curtis
01:26 queued 53s
created

Chan::getMonth()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 15
rs 9.6111
c 1
b 0
f 0
cc 5
nc 5
nop 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 Gedcom\Record;
16
17
use Gedcom\Record;
18
19
/**
20
 * Class Chan.
21
 */
22
class Chan extends \Gedcom\Record
23
{
24
    /**
25
     * @var array
26
     */
27
    private $months = [
28
        'JAN' => '01', 'FEB' => '02', 'MAR' => '03', 'APR' => '04', 'MAY' => '05', 'JUN' => '06',
29
        'JUL' => '07', 'AUG' => '08', 'SEP' => '09', 'OCT' => '10', 'NOV' => '11', 'DEC' => '12',
30
    ];
31
    
32
    /**
33
     * @var string
34
     */
35
    protected $date;
36
37
    /**
38
     * @var string
39
     */
40
    protected $time;
41
42
    /**
43
     * @var string
44
     */
45
    protected $datetime;
46
47
    /**
48
     * @var array
49
     */
50
    protected $note = [];
51
52
    /**
53
     * @param string $date
54
     *
55
     * @return Chan
56
     */
57
    public function setDate($date = '')
58
    {
59
        $this->date = $date;
60
61
        return $this;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getDate()
68
    {
69
        return $this->date;
70
    }
71
72
    /**
73
     * @param Record\NoteRef $note
74
     *
75
     * @return Chan
76
     */
77
    public function addNote($note = [])
78
    {
79
        $this->note[] = $note;
80
81
        return $this;
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    public function getNote()
88
    {
89
        return $this->note;
90
    }
91
92
    /**
93
     * @param string $time
94
     *
95
     * @return Chan
96
     */
97
    public function setTime($time = '')
98
    {
99
        $this->time = $time;
100
101
        return $this;
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function getTime()
108
    {
109
        return $this->time;
110
    }
111
    
112
    public function setDatetime($date)
113
    {
114
        $this->datetime = $date .' '. $this->time;
115
        
116
        return $this;
117
    }
118
    
119
    public function getDatetime()
120
    {
121
        return $this->datetime;
122
    }
123
124
    public function getMonth()
125
    {
126
        $record = explode(' ', $this->date);
127
        if (count($record) > 0) {
128
            if ($this->isPrefix($record[0])) {
129
                unset($record[0]);
130
            }
131
            foreach ($record as $part) {
132
                if (isset($this->months[trim($part)])) {
133
                    return $this->months[trim($part)];
134
                }
135
            }
136
        }
137
138
        return null;
139
    }
140
141
    /**
142
     * Return year part of date.
143
     *
144
     * @return int|null
145
     */
146
    public function getYear()
147
    {
148
        $record = explode(' ', $this->date);
149
        if (count($record) > 0) {
150
            if ($this->isPrefix($record[0])) {
151
                unset($record[0]);
152
            }
153
            if (count($record) > 0) {
154
                return (int) end($record);
155
            }
156
        }
157
158
        return null;
159
    }
160
161
    /**
162
     * Return day part of date.
163
     *
164
     * @return int|null
165
     */
166
    public function getDay()
167
    {
168
        $record = explode(' ', $this->date);
169
        if (!empty($record[0])) {
170
            if ($this->isPrefix($record[0])) {
171
                unset($record[0]);
172
            }
173
            if (count($record) > 0) {
174
                $day = (int) reset($record);
175
                if ($day >= 1 && $day <= 31) {
176
                    return substr("0{$day}", -2);
0 ignored issues
show
Bug Best Practice introduced by
The expression return substr('0'.$day, -2) returns the type string which is incompatible with the documented return type integer|null.
Loading history...
177
                }
178
            }
179
        }
180
181
        return null;
182
    }
183
184
    /**
185
     * Check if the first part is a prefix (eg 'BEF', 'ABT',).
186
     *
187
     * @param string $datePart Date part to be checked
188
     *
189
     * @return bool
190
     */
191
    private function isPrefix($datePart)
192
    {
193
        return in_array($datePart, ['FROM', 'TO', 'BEF', 'AFT', 'BET', 'AND', 'ABT', 'EST', 'CAL', 'INT']);
194
    }
195
}
196