GeoAnalysisPlace::include()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage GeoDispersion
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2021-2022, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Common\GeoDispersion\GeoAnalysis;
16
17
use Fisharebest\Webtrees\Place;
18
use Fisharebest\Webtrees\Tree;
19
20
/**
21
 * Place resulting from a geographical analysis.
22
 * The place can be in one of several statuses:
23
 *  - Unknown : the analysis could not find any place for an analysed item, and that information needs to be captured
24
 *  - Known: the analysis has identify a place for an analysed item
25
 *      - Found: the known place is within the scope of the analysis view
26
 *      - Invalid: the known place does not match the required depth of the analysis
27
 *      - Excluded: the known place is not within the scope of the analysis view
28
 */
29
class GeoAnalysisPlace
30
{
31
    /**
32
     * The default place name for invalid places
33
     * @var string INVALID_PLACE
34
     */
35
    private const INVALID_PLACE = '##INVALID##';
36
37
    private Place $place;
38
    private bool $is_excluded;
39
40
    /**
41
     * Constructor for GeoAnalysisPlace
42
     *
43
     * @param Tree $tree Default tree
44
     * @param Place|null $place Place resulting from the analysis
45
     * @param int $depth Place hierarchy depth defined by the geographical analysis view
46
     * @param bool $strict_depth Checks whether places with a lower depth than defined should be flagged as invalid
47
     */
48
    public function __construct(Tree $tree, ?Place $place, int $depth, bool $strict_depth = false)
49
    {
50
        $this->place = $this->extractPlace($place, $depth, $strict_depth) ?? new Place('', $tree);
51
        $this->is_excluded = false;
52
    }
53
54
    /**
55
     * Process the provided Place to determine its status for further usage
56
     *
57
     * @param Place|null $place
58
     * @param int $depth
59
     * @param bool $strict_depth
60
     * @return Place|NULL
61
     */
62
    private function extractPlace(?Place $place, int $depth, bool $strict_depth): ?Place
63
    {
64
        if ($place === null) {
65
            return null;
66
        }
67
        if (mb_strlen($place->gedcomName()) === 0) {
68
            return null;
69
        }
70
        $parts = $place->lastParts($depth);
71
        if ($strict_depth && $parts->count() !== $depth) {
72
            return new Place(self::INVALID_PLACE, $place->tree());
73
        }
74
        return new Place($parts->implode(', '), $place->tree());
75
    }
76
77
    /**
78
     * Get the GeoAnalysis Place key
79
     *
80
     * @return string
81
     */
82
    public function key(): string
83
    {
84
        return $this->place->gedcomName();
85
    }
86
87
    /**
88
     * Get the underlying Place object
89
     *
90
     * @return Place
91
     */
92
    public function place(): Place
93
    {
94
        return $this->place;
95
    }
96
97
    /**
98
     * Check if the GeoAnalysis Place is in the Known status
99
     *
100
     * @return bool
101
     */
102
    public function isKnown(): bool
103
    {
104
        return !$this->isUnknown();
105
    }
106
107
    /**
108
     * Check if the GeoAnalysis Place is in the Unknown status
109
     *
110
     * @return bool
111
     */
112
    public function isUnknown(): bool
113
    {
114
        return mb_strlen($this->place->gedcomName()) === 0;
115
    }
116
117
    /**
118
     * Check if the GeoAnalysis Place is in the Invalid status
119
     *
120
     * @return bool
121
     */
122
    public function isInvalid(): bool
123
    {
124
        return $this->place->gedcomName() === self::INVALID_PLACE;
125
    }
126
127
    /**
128
     * Check if the GeoAnalysis Place is in the Excluded status
129
     *
130
     * @return bool
131
     */
132
    public function isExcluded(): bool
133
    {
134
        return $this->isUnknown() || $this->isInvalid() || $this->is_excluded;
135
    }
136
137
    /**
138
     * Set the GeoAnalysis Place status to Found, if the parameter is true
139
     *
140
     * @param bool $include
141
     * @return $this
142
     */
143
    public function include(bool $include = true): self
144
    {
145
        $this->is_excluded = !$include;
146
        return $this;
147
    }
148
149
    /**
150
     * Set the GeoAnalysis Place status to Excluded, if the parameter is true
151
     *
152
     * @param bool $exclude
153
     * @return $this
154
     */
155
    public function exclude(bool $exclude = true): self
156
    {
157
        $this->is_excluded = $exclude;
158
        return $this;
159
    }
160
}
161