Completed
Push — develop ( 989811...4ba2ac )
by Greg
10:44
created

AbstractCensusColumnCondition::conditionChild()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2016 webtrees development team
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
 * GNU General Public License for more details.
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15
 */
16
namespace Fisharebest\Webtrees\Census;
17
18
use Fisharebest\Webtrees\Date;
19
use Fisharebest\Webtrees\Individual;
20
21
/**
22
 * Marital status.
23
 */
24
abstract class AbstractCensusColumnCondition extends AbstractCensusColumn implements CensusColumnInterface {
25
	/* Text to display for married individuals */
26
	protected $husband = '';
27
	protected $wife    = '';
28
29
	/* Text to display for unmarried individuals */
30
	protected $bachelor = '';
31
	protected $spinster = '';
32
33
	/* Text to display for children */
34
	protected $boy  = '';
35
	protected $girl = '';
36
37
	/* Text to display for divorced individuals */
38
	protected $divorce  = '';
39
	protected $divorcee = '';
40
41
	/* Text to display for widowed individuals */
42
	protected $widower = '';
43
	protected $widow   = '';
44
45
	/* At what age is this individual recorded as an adult */
46
	protected $age_adult = 15;
47
48
	/**
49
	 * Generate the likely value of this census column, based on available information.
50
	 *
51
	 * @param Individual      $individual
52
	 * @param Individual|null $head
53
	 *
54
	 * @return string
55
	 */
56
	public function generate(Individual $individual, Individual $head = null) {
57
		$family = $this->spouseFamily($individual);
58
		$sex    = $individual->getSex();
59
60
		if ($family === null || count($family->getFacts('_NMR')) > 0) {
61
			if ($this->isChild($individual)) {
62
				return $this->conditionChild($sex);
63
			} else {
64
				return $this->conditionSingle($sex);
65
			}
66
		} elseif (count($family->getFacts('DIV')) > 0) {
67
			return $this->conditionDivorced($sex);
68
		} else {
69
			$spouse = $family->getSpouse($individual);
70
			if ($spouse instanceof Individual && $this->isDead($spouse)) {
71
				return $this->conditionWidowed($sex);
72
			} else {
73
				return $this->conditionMarried($sex);
74
			}
75
		}
76
	}
77
78
	/**
79
	 * How is this condition written in a census column.
80
	 *
81
	 * @param $sex
82
	 *
83
	 * @return string
84
	 */
85
	private function conditionChild($sex) {
86
		if ($sex === 'F') {
87
			return $this->girl;
88
		} else {
89
			return $this->boy;
90
		}
91
	}
92
93
	/**
94
	 * How is this condition written in a census column.
95
	 *
96
	 * @param $sex
97
	 *
98
	 * @return string
99
	 */
100
	private function conditionDivorced($sex) {
101
		if ($sex === 'F') {
102
			return $this->divorcee;
103
		} else {
104
			return $this->divorce;
105
		}
106
	}
107
108
	/**
109
	 * How is this condition written in a census column.
110
	 *
111
	 * @param $sex
112
	 *
113
	 * @return string
114
	 */
115
	private function conditionMarried($sex) {
116
		if ($sex === 'F') {
117
			return $this->wife;
118
		} else {
119
			return $this->husband;
120
		}
121
	}
122
123
	/**
124
	 * How is this condition written in a census column.
125
	 *
126
	 * @param $sex
127
	 *
128
	 * @return string
129
	 */
130
	private function conditionSingle($sex) {
131
		if ($sex === 'F') {
132
			return $this->spinster;
133
		} else {
134
			return $this->bachelor;
135
		}
136
	}
137
138
	/**
139
	 * How is this condition written in a census column.
140
	 *
141
	 * @param $sex
142
	 *
143
	 * @return string
144
	 */
145
	private function conditionWidowed($sex) {
146
		if ($sex === 'F') {
147
			return $this->widow;
148
		} else {
149
			return $this->widower;
150
		}
151
	}
152
153
	/**
154
	 * Is the individual a child.
155
	 *
156
	 * @param Individual $individual
157
	 *
158
	 * @return bool
159
	 */
160
	private function isChild(Individual $individual) {
161
		$age = (int) Date::getAge($individual->getEstimatedBirthDate(), $this->date(), 0);
162
163
		return $age < $this->age_adult;
164
	}
165
166
	/**
167
	 * Is the individual dead.
168
	 *
169
	 * @param Individual $individual
170
	 *
171
	 * @return bool
172
	 */
173
	private function isDead(Individual $individual) {
174
		return $individual->getDeathDate()->isOK() && Date::compare($individual->getDeathDate(), $this->date()) < 0;
175
	}
176
}
177