Passed
Branch feature/2.1-geodispersion-dev (1d61a8)
by Jonathan
61:21
created

FactSourceStatus::label()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 15
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 26
rs 8.8333
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage IsSourced
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2009-2021, Jonathan Jaubart
10
 * @license https://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\IsSourced\Data;
16
17
use Fisharebest\Webtrees\I18N;
18
use Fisharebest\Webtrees\Registry;
19
20
/**
21
 * Data class for holding source status for facts.
22
 */
23
class FactSourceStatus extends SourceStatus
24
{
25
    /**
26
     * @var boolean $has_date
27
     */
28
    private $has_date = false;
29
30
    /**
31
     * @var boolean $has_precise_date
32
     */
33
    private $has_precise_date = false;
34
35
    /**
36
     * @var boolean $has_source_date
37
     */
38
    private $has_source_date = false;
39
40
    /**
41
     * @var boolean $source_date_match
42
     */
43
    private $source_date_match = false;
44
45
    /**
46
     * Return whether the fact is dated.
47
     *
48
     * @return bool
49
     */
50
    public function factHasDate(): bool
51
    {
52
        return $this->has_date;
53
    }
54
55
    /**
56
     * Set whether the fact is dated.
57
     *
58
     * @param bool $has_date
59
     * @return self
60
     */
61
    public function setFactHasDate(bool $has_date): self
62
    {
63
        $this->has_date = $has_date;
64
        return $this;
65
    }
66
67
    /**
68
     * Combinate whether the fact is dated with the previous status.
69
     *
70
     * @param bool $has_date
71
     * @return self
72
     */
73
    public function addFactHasDate(bool $has_date): self
74
    {
75
        $this->has_date = $this->has_date || $has_date;
76
        return $this;
77
    }
78
79
    /**
80
     * Return whether the fact is dated with a precise day.
81
     * Any date modifier will be considered as not precise.
82
     * A month or year will be considered as not precise.
83
     *
84
     * @return bool
85
     */
86
    public function factHasPreciseDate(): bool
87
    {
88
        return $this->has_date && $this->has_precise_date;
89
    }
90
91
    /**
92
     * Set whather the fact is dated with a precise day.
93
     *
94
     * @param bool $has_precise_date
95
     * @return self
96
     */
97
    public function setFactHasPreciseDate(bool $has_precise_date): self
98
    {
99
        $this->has_precise_date = $has_precise_date;
100
        return $this;
101
    }
102
103
    public function addFactHasPreciseDate(bool $has_precise_date): self
104
    {
105
        $this->has_precise_date = $this->has_precise_date || $has_precise_date;
106
        return $this;
107
    }
108
109
    /**
110
     * Return whether the source citation is dated.
111
     *
112
     * @return bool
113
     */
114
    public function sourceHasDate(): bool
115
    {
116
        return $this->has_source_date;
117
    }
118
119
    /**
120
     * Set whether the source citation is dated.
121
     *
122
     * @param bool $has_source_date
123
     * @return self
124
     */
125
    public function setSourceHasDate(bool $has_source_date): self
126
    {
127
        $this->has_source_date = $has_source_date;
128
        return $this;
129
    }
130
131
    /**
132
     * Combine whether the source citation is dated with the previous status.
133
     *
134
     * @param bool $has_source_date
135
     * @return self
136
     */
137
    public function addSourceHasDate(bool $has_source_date): self
138
    {
139
        $this->has_source_date = $this->has_source_date || $has_source_date;
140
        return $this;
141
    }
142
143
    /**
144
     * Return whether the source citation date is close to the fact date.
145
     *
146
     * @return bool
147
     */
148
    public function sourceMatchesFactDate(): bool
149
    {
150
        return $this->has_precise_date && $this->has_source_date && $this->source_date_match;
151
    }
152
153
    /**
154
     * Set whether the source citation date is close to the fact date.
155
     *
156
     * @param bool $source_date_match
157
     * @return self
158
     */
159
    public function setSourceMatchesFactDate(bool $source_date_match): self
160
    {
161
        $this->source_date_match = $source_date_match;
162
        return $this;
163
    }
164
165
    /**
166
     * Combine whether the source citation date is close to the fact date with the previous status.
167
     *
168
     * @param bool $source_date_match
169
     * @return self
170
     */
171
    public function addSourceMatchesFactDate(bool $source_date_match): self
172
    {
173
        $this->source_date_match = $this->source_date_match || $source_date_match;
174
        return $this;
175
    }
176
177
    /**
178
     * {@inheritDoc}
179
     * @see \MyArtJaub\Webtrees\Module\IsSourced\Data\SourceStatus::isFullySourced()
180
     */
181
    public function isFullySourced(): bool
182
    {
183
        return parent::isFullySourced() && $this->sourceMatchesFactDate();
184
    }
185
186
    /**
187
     * {@inheritDoc}
188
     * @see \MyArtJaub\Webtrees\Module\IsSourced\Data\SourceStatus::label()
189
     */
190
    public function label(string $context): string
191
    {
192
        $context_label = Registry::elementFactory()->make($context)->label();
193
194
        if ($this->factHasPreciseDate()) {
195
            if ($this->hasSource()) {
196
                if ($this->hasSupportingDocument()) {
197
                    if ($this->sourceMatchesFactDate()) {
198
                        return I18N::translate('%s sourced with exact certificate', $context_label);
199
                    } else {
200
                        return I18N::translate('%s sourced with a certificate', $context_label);
201
                    }
202
                }
203
204
                if ($this->sourceMatchesFactDate()) {
205
                    return I18N::translate('%s precisely sourced', $context_label);
206
                }
207
                return I18N::translate('%s sourced', $context_label);
208
            }
209
            return I18N::translate('%s not sourced', $context_label);
210
        }
211
212
        if ($this->factHasDate()) {
213
            return I18N::translate('%s not precise', $context_label);
214
        }
215
        return I18N::translate('%s not found', $context_label);
216
    }
217
218
    /**
219
     * {@inheritDoc}
220
     * @see \MyArtJaub\Webtrees\Module\IsSourced\Data\SourceStatus::combineWith()
221
     */
222
    public function combineWith(SourceStatus $other): SourceStatus
223
    {
224
        if ($other instanceof FactSourceStatus) {
225
            $this->addFactHasDate($other->factHasDate());
226
            $this->addFactHasPreciseDate($other->factHasPreciseDate());
227
            $this->addSourceHasDate($other->sourceHasDate());
228
            $this->addSourceMatchesFactDate($other->sourceMatchesFactDate());
229
        }
230
        return parent::combineWith($other);
231
    }
232
}
233