Passed
Push — master ( a64a0f...fd132a )
by Curtis
32:51 queued 29:49
created

DateParser::try_parse_Y_date()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace FamilyTree365\LaravelGedcom\Utils;
4
5
class DateParser
6
{
7
    private $year;
8
    private $month;
9
    private $day;
10
11
    public function __construct()
12
    {
13
        private $date_string = '';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_PRIVATE on line 13 at column 8
Loading history...
14
    }
15
16
    public function parse_date()
17
    {
18
        $this->trim_datestring();
19
        if (!$this->try_parse_full_date()) {
20
            if (!$this->try_parse_M_Y_date()) {
21
                if (!$this->try_parse_Y_date()) {
22
                    $this->set_null_date();
23
                }
24
            }
25
        }
26
27
        return $this->export();
28
    }
29
30
    private function trim_datestring()
31
    {
32
        $words_to_remove = [
33
            // If an exact date is not known, a date range can be specified
34
            'AFT', 'BEF', 'BET', 'AND',
35
            //For approximate dates
36
            'ABT', 'CAL', 'EST',
37
            // Takes a property over a certain period of time ( e.g. exercise of a profession, living in a particular place )
38
            'FROM', 'TO',
39
        ];
40
        foreach ($words_to_remove as $word) {
41
            $this->date_string = str_replace($word, '', $this->date_string);
42
        }
43
        $this->date_string = trim($this->date_string);
44
    }
45
46
    private function try_parse_full_date()
47
    {
48
        $this->set_null_date(); // Default
49
        $date_parts = explode(' ', $this->date_string);
50
        if (count($date_parts) > 3) {
51
            return false;
52
        }
53
54
        return $this->try_get_day(
55
            $date_parts[0] ?? false
56
        )
57
            and
58
            $this->try_get_month(
59
                $date_parts[1] ?? false
60
            )
61
            and
62
            $this->try_get_year(
63
                $date_parts[2] ?? false,
64
                $date_parts[3] ?? false
65
            );
66
    }
67
68
    private function try_parse_M_Y_date()
69
    {
70
        $this->set_null_date(); // Default
71
        $date_parts = explode(' ', $this->date_string);
72
        if (count($date_parts) > 3) {
73
            return false;
74
        }
75
76
        return $this->try_get_month(
77
            $date_parts[0] ?? false
78
        )
79
            and
80
            $this->try_get_year(
81
                $date_parts[1] ?? false,
82
                $date_parts[2] ?? false
83
            );
84
    }
85
86
    private function try_parse_Y_date()
87
    {
88
        $this->set_null_date(); // Default
89
        $date_parts = explode(' ', $this->date_string);
90
        if (count($date_parts) > 2) {
91
            return false;
92
        }
93
94
        return $this->try_get_year(
95
            $date_parts[0] ?? false,
96
            $date_parts[1] ?? false
97
        );
98
    }
99
100
    private function try_get_year($year, $epoch = false)
101
    {
102
        $sign = 1;
103
        if ($epoch) {
104
            if ($epoch == 'BC') {
105
                $sign = -1;
106
            } elseif ($epoch == 'AC') {
107
                $sign = 1;
108
            } else {
109
                return false;
110
            }
111
        }
112
        if (!$year) {
113
            return false;
114
        }
115
        if (is_numeric($year)) {
116
            $this->year = $year * $sign;
117
        } else {
118
            return false;
119
        }
120
121
        return true;
122
    }
123
124
    private function try_get_month($month)
125
    {
126
        $months = [
127
            'JAN' => 1,
128
            'FEB' => 2,
129
            'MAR' => 3,
130
            'APR' => 4,
131
            'MAY' => 5,
132
            'JUN' => 6,
133
            'JUL' => 7,
134
            'AUG' => 8,
135
            'SEP' => 9,
136
            'OCT' => 10,
137
            'NOV' => 11,
138
            'DEC' => 12,
139
        ];
140
141
        if (isset($months[$month])) {
142
            $this->month = $months[$month];
143
144
            return true;
145
        } else {
146
            return false;
147
        }
148
    }
149
150
    private function try_get_day($day)
151
    {
152
        if (is_numeric($day)) {
153
            $this->day = $day * 1;
154
155
            return true;
156
        } else {
157
            return false;
158
        }
159
    }
160
161
    public function set_null_date()
162
    {
163
        $this->year = null;
164
        $this->month = null;
165
        $this->day = null;
166
    }
167
168
    public function export()
169
    {
170
        return [
171
            'year'          => $this->year,
172
            'month'         => $this->month,
173
            'day'           => $this->day,
174
        ];
175
    }
176
}
177