Chr::getYear()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 7
c 1
b 1
f 0
dl 0
loc 13
rs 10
cc 4
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
/**
18
 * Class Chan.
19
 */
20
class Chr extends \Gedcom\Record
21
{
22
    private $months = [
23
        'JAN' => '01', 'FEB' => '02', 'MAR' => '03', 'APR' => '04', 'MAY' => '05', 'JUN' => '06',
24
        'JUL' => '07', 'AUG' => '08', 'SEP' => '09', 'OCT' => '10', 'NOV' => '11', 'DEC' => '12',
25
    ];
26
27
    public $date;
28
29
    public $dateFormatted = null;
30
31
    public $plac;
32
33
    public function setDate($date)
34
    {
35
        $this->date = $date;
36
        $this->dateFormatted = $this->getYear().'-'.$this->getMonth().'-'.substr("0{$this->getDay()}", -2);
37
    }
38
39
    public function getDateFormatted()
40
    {
41
        return $this->dateFormatted;
42
    }
43
44
    public function getDate()
45
    {
46
        return $this->date;
47
    }
48
49
    public function setPlac($plac)
50
    {
51
        $this->plac = $plac;
52
    }
53
54
    public function getPlac()
55
    {
56
        return $this->plac;
57
    }
58
59
    public function getDay()
60
    {
61
        $record = explode(' ', $this->date);
62
        if (!empty($record[0])) {
63
            if ($this->isPrefix($record[0])) {
64
                unset($record[0]);
65
            }
66
            if (count($record) > 0) {
67
                $day = (int) reset($record);
68
                if ($day >= 1 && $day <= 31) {
69
                    return $day;
70
                }
71
            }
72
        }
73
74
        return null;
75
    }
76
77
    public function getMonth()
78
    {
79
        $record = explode(' ', $this->date);
80
        if (count($record) > 0) {
81
            if ($this->isPrefix($record[0])) {
82
                unset($record[0]);
83
            }
84
            foreach ($record as $part) {
85
                if (isset($this->months[trim($part)])) {
86
                    return $this->months[trim($part)];
87
                }
88
            }
89
        }
90
91
        return null;
92
    }
93
94
    public function getYear()
95
    {
96
        $record = explode(' ', $this->date);
97
        if (count($record) > 0) {
98
            if ($this->isPrefix($record[0])) {
99
                unset($record[0]);
100
            }
101
            if (count($record) > 0) {
102
                return (int) end($record);
103
            }
104
        }
105
106
        return null;
107
    }
108
109
    private function isPrefix($datePart)
110
    {
111
        return in_array($datePart, ['FROM', 'TO', 'BEF', 'AFT', 'BET', 'AND', 'ABT', 'EST', 'CAL', 'INT']);
112
    }
113
}
114