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
|
|
|
/** |
18
|
|
|
* Class Date. |
19
|
|
|
*/ |
20
|
|
|
class Date extends \Gedcom\Record |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $date; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $months = [ |
31
|
|
|
'JAN' => 1, 'FEB' => 2, 'MAR' => 3, 'APR' => 4, 'MAY' => 5, 'JUN' => 6, |
32
|
|
|
'JUL' => 7, 'AUG' => 8, 'SEP' => 9, 'OCT' => 10, 'NOV' => 11, 'DEC' => 12, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $date Date array |
37
|
|
|
* |
38
|
|
|
* @return Date |
39
|
|
|
*/ |
40
|
|
|
public function setDate($date) |
41
|
|
|
{ |
42
|
|
|
$this->date = $date; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return null|string |
49
|
|
|
*/ |
50
|
|
|
public function getDate() |
51
|
|
|
{ |
52
|
|
|
return $this->date; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return month part of date. |
57
|
|
|
* |
58
|
|
|
* @return int|null |
59
|
|
|
*/ |
60
|
|
|
public function getMonth() |
61
|
|
|
{ |
62
|
|
|
$record = explode(' ', $this->date); |
63
|
|
|
if (count($record) > 0) { |
64
|
|
|
if ($this->isPrefix($record[0])) { |
65
|
|
|
unset($record[0]); |
66
|
|
|
} |
67
|
|
|
foreach ($record as $part) { |
68
|
|
|
if (isset($this->months[trim($part)])) { |
69
|
|
|
return $this->months[trim($part)]; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return year part of date. |
79
|
|
|
* |
80
|
|
|
* @return int|null |
81
|
|
|
*/ |
82
|
|
|
public function getYear() |
83
|
|
|
{ |
84
|
|
|
$record = explode(' ', $this->date); |
85
|
|
|
if (count($record) > 0) { |
86
|
|
|
if ($this->isPrefix($record[0])) { |
87
|
|
|
unset($record[0]); |
88
|
|
|
} |
89
|
|
|
if (count($record) > 0) { |
90
|
|
|
return (int) end($record); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return day part of date. |
99
|
|
|
* |
100
|
|
|
* @return int|null |
101
|
|
|
*/ |
102
|
|
|
public function getDay() |
103
|
|
|
{ |
104
|
|
|
$record = explode(' ', $this->date); |
105
|
|
|
if (!empty($record[0])) { |
106
|
|
|
if ($this->isPrefix($record[0])) { |
107
|
|
|
unset($record[0]); |
108
|
|
|
} |
109
|
|
|
if (count($record) > 0) { |
110
|
|
|
$day = (int) reset($record); |
111
|
|
|
if ($day >= 1 && $day <= 31) { |
112
|
|
|
return $day; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Check if the first part is a prefix (eg 'BEF', 'ABT',). |
122
|
|
|
* |
123
|
|
|
* @param string $datePart Date part to be checked |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
private function isPrefix($datePart) |
128
|
|
|
{ |
129
|
|
|
return in_array($datePart, ['FROM', 'TO', 'BEF', 'AFT', 'BET', 'AND', 'ABT', 'EST', 'CAL', 'INT']); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|