Passed
Push — master ( cc2aed...bd2365 )
by Curtis
29:28 queued 25:58
created

DateParser::try_get_year()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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