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

Birt::getDay()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
rs 9.2222
cc 6
nc 7
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
use Gedcom\Record;
18
19
/**
20
 * Class Chan.
21
 */
22
class Birt 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;
34
    
35
    public $dati;
36
    
37
    public $plac;
38
39
    public function setDate($date) {
40
        $this->date = $date;
41
        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...
42
            $this->dateFormatted = $this->getYear() .'-'. $this->getMonth() .'-'. substr("0{$this->getDay()}", -2);
43
        }
44
        else {
45
            $this->year = $date;
46
            $this->date = null;
47
        }
48
    }
49
    
50
    public function getDateFormatted() {
51
        return $this->dateFormatted;
52
    }
53
    
54
    public function getDate() {
55
        return $this->date;
56
    }
57
    
58
    public function setDati($dati) {
59
        $this->dati = $dati;
60
    }
61
    
62
    public function getDati() {
63
        return $this->dati;
64
    }
65
    
66
    public function setPlac($plac) {
67
        $this->plac = $plac;
68
    }
69
    
70
    public function getPlac() {
71
        return $this->plac;
72
    }
73
74
    public function getDay()
75
    {
76
        $record = explode(' ', $this->date);
77
        if (!empty($record[0])) {
78
            if ($this->isPrefix($record[0])) {
79
                unset($record[0]);
80
            }
81
            if (count($record) > 0) {
82
                $day = (int) reset($record);
83
                if ($day >= 1 && $day <= 31) {
84
                    return $day;
85
                }
86
            }
87
        }
88
89
        return null;
90
    }
91
92
    public function getMonth()
93
    {
94
        $record = explode(' ', $this->date);
95
        if (count($record) > 0) {
96
            if ($this->isPrefix($record[0])) {
97
                unset($record[0]);
98
            }
99
            foreach ($record as $part) {
100
                if (isset($this->months[trim($part)])) {
101
                    return $this->months[trim($part)];
102
                }
103
            }
104
        }
105
106
        return null;
107
    }
108
109
    public function getYear()
110
    {
111
        $record = explode(' ', $this->date);
112
        if (count($record) > 0) {
113
            if ($this->isPrefix($record[0])) {
114
                unset($record[0]);
115
            }
116
            if (count($record) > 0) {
117
                return (int) end($record);
118
            }
119
        }
120
121
        return null;
122
    }
123
124
    private function isPrefix($datePart)
125
    {
126
        return in_array($datePart, ['FROM', 'TO', 'BEF', 'AFT', 'BET', 'AND', 'ABT', 'EST', 'CAL', 'INT']);
127
    }
128
}
129