Test Failed
Push — master ( 3f892b...ca4849 )
by Curtis
03:14 queued 02:42
created

Deat::isPrefix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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 Deat extends \Gedcom\Record
23
{
24
    private $months = [
25
        'JAN' => '01', 'FEB' => '02', 'MAR' => '03', 'APR' => '04', 'MAY' => '05', 'JUN' => '06',
26
        'JUL' => '07', 'AUG' => '08', 'SEP' => '09', 'OCT' => '10', 'NOV' => '11', 'DEC' => '12',
27
    ];
28
    
29
    public $date;
30
31
    public $year;
32
33
    public $dateFormatted = null;
34
    
35
    public $dati;
36
    
37
    public $plac;
38
    
39
    public $caus;
40
    
41
    public function setDate($date) {
42
        $this->date = $date;
43
        if ($this->getDay()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getDay() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
44
            $this->dateFormatted = $this->getYear() .'-'. $this->getMonth() .'-'. substr("0{$this->getDay()}", -2);
45
        }
46
        else {
47
            $this->year = $date;
48
            $this->date = null;
49
        }
50
    }
51
    
52
    public function getDateFormatted() {
53
        return $this->dateFormatted;
54
    }
55
    
56
    public function getDate() {
57
        return $this->date;
58
    }
59
    
60
    public function setDati($dati) {
61
        $this->dati = $dati;
62
    }
63
    
64
    public function getDati() {
65
        return $this->dati;
66
    }
67
    
68
    public function setPlac($plac) {
69
        $this->plac = $plac;
70
    }
71
    
72
    public function getPlac() {
73
        return $this->plac;
74
    }
75
76
    public function setCaus($caus) {
77
        $this->caus = $caus;
78
    }
79
80
    public function getCaus() {
81
        return $this->caus;
82
    }
83
84
    public function getDay()
85
    {
86
        $record = explode(' ', $this->date);
87
        if (!empty($record[0])) {
88
            if ($this->isPrefix($record[0])) {
89
                unset($record[0]);
90
            }
91
            if (count($record) > 0) {
92
                $day = (int) reset($record);
93
                if ($day >= 1 && $day <= 31) {
94
                    return $day;
95
                }
96
            }
97
        }
98
99
        return null;
100
    }
101
102
    public function getMonth()
103
    {
104
        $record = explode(' ', $this->date);
105
        if (count($record) > 0) {
106
            if ($this->isPrefix($record[0])) {
107
                unset($record[0]);
108
            }
109
            foreach ($record as $part) {
110
                if (isset($this->months[trim($part)])) {
111
                    return $this->months[trim($part)];
112
                }
113
            }
114
        }
115
116
        return null;
117
    }
118
119
    public function getYear()
120
    {
121
        $record = explode(' ', $this->date);
122
        if (count($record) > 0) {
123
            if ($this->isPrefix($record[0])) {
124
                unset($record[0]);
125
            }
126
            if (count($record) > 0) {
127
                return (int) end($record);
128
            }
129
        }
130
131
        return null;
132
    }
133
134
    private function isPrefix($datePart)
135
    {
136
        return in_array($datePart, ['FROM', 'TO', 'BEF', 'AFT', 'BET', 'AND', 'ABT', 'EST', 'CAL', 'INT']);
137
    }
138
}
139