|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FabienCrassat\CurriculumVitaeBundle Symfony bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Fabien Crassat <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace FabienCrassat\CurriculumVitaeBundle\Utility; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
|
15
|
|
|
|
|
16
|
|
|
class AgeCalculator |
|
17
|
|
|
{ |
|
18
|
|
|
private $birthdayDate; |
|
19
|
|
|
private $birthday; |
|
20
|
|
|
private $age = 0; |
|
21
|
|
|
private $birth; |
|
22
|
|
|
private $today; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string $birthday |
|
26
|
|
|
*/ |
|
27
|
17 |
|
public function __construct($birthday) |
|
28
|
|
|
{ |
|
29
|
17 |
|
$this->birthday = $birthday; |
|
30
|
17 |
|
$this->birth = new \stdClass(); |
|
|
|
|
|
|
31
|
17 |
|
$this->setBirthday(); |
|
32
|
15 |
|
$this->today = new \stdClass(); |
|
33
|
15 |
|
$this->setToday(); |
|
34
|
15 |
|
} |
|
35
|
|
|
|
|
36
|
15 |
|
public function age() |
|
37
|
|
|
{ |
|
38
|
|
|
// The calculator of the age |
|
39
|
15 |
|
$this->setAgeByYear(); |
|
40
|
|
|
|
|
41
|
15 |
|
if ($this->today->month <= $this->birth->month) { |
|
42
|
3 |
|
if ($this->birth->month == $this->today->month) { |
|
43
|
3 |
|
if ($this->birth->day > $this->today->day) { |
|
44
|
2 |
|
$this->setAgeMinusOne(); |
|
45
|
2 |
|
return $this->getAge(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
return $this->getAge(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$this->setAgeMinusOne(); |
|
52
|
|
|
return $this->getAge(); |
|
53
|
|
|
}; |
|
54
|
|
|
|
|
55
|
12 |
|
return $this->getAge(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
17 |
|
private function setBirthday() |
|
59
|
|
|
{ |
|
60
|
17 |
|
$this->birthdayDate = date_parse_from_format("Y-m-d", $this->birthday); |
|
|
|
|
|
|
61
|
17 |
|
if ($this->birthdayDate['error_count'] > 0) { |
|
62
|
1 |
|
throw new InvalidArgumentException("The date (". $this->birthday .") is bad formatted, expected Y-m-d."); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
// Retreive the date and transform it to integer |
|
65
|
16 |
|
$this->birth->day = (int) $this->birthdayDate["day"]; |
|
|
|
|
|
|
66
|
16 |
|
$this->birth->month = (int) $this->birthdayDate["month"]; |
|
|
|
|
|
|
67
|
16 |
|
$this->birth->year = (int) $this->birthdayDate["year"]; |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
16 |
|
if(!checkdate($this->birth->month, $this->birth->day, $this->birth->year)) { |
|
70
|
1 |
|
throw new InvalidArgumentException("The date (". $this->birthday .") is unknown."); |
|
|
|
|
|
|
71
|
|
|
}; |
|
72
|
15 |
|
} |
|
73
|
|
|
|
|
74
|
15 |
|
private function setToday() |
|
75
|
|
|
{ |
|
76
|
|
|
// Retreive today and transform it to integer |
|
77
|
15 |
|
$this->today->day = (int) date('j'); |
|
|
|
|
|
|
78
|
15 |
|
$this->today->month = (int) date('n'); |
|
79
|
15 |
|
$this->today->year = (int) date('Y'); |
|
|
|
|
|
|
80
|
15 |
|
} |
|
81
|
|
|
|
|
82
|
15 |
|
private function getAge() |
|
83
|
|
|
{ |
|
84
|
15 |
|
return $this->age; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
2 |
|
private function setAgeMinusOne() |
|
88
|
|
|
{ |
|
89
|
2 |
|
$this->age = $this->getAge() - 1; |
|
90
|
2 |
|
} |
|
91
|
|
|
|
|
92
|
15 |
|
private function setAgeByYear() |
|
93
|
|
|
{ |
|
94
|
15 |
|
$this->age = (int) ($this->today->year - $this->birth->year); |
|
95
|
15 |
|
} |
|
96
|
|
|
} |
|
|
|
|
|
|
97
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.