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

GeoAnalysisPlace   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 128
rs 10
wmc 16

10 Methods

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