GeoAnalysisResults::addPlaceInCreatedCategory()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
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\I18N;
18
use Illuminate\Support\Collection;
19
20
/**
21
 * Aggregated global and detailed results of a geographical dispersion analysis
22
 *
23
 */
24
class GeoAnalysisResults
25
{
26
    private GeoAnalysisResult $global;
27
28
    /**
29
     * @var Collection<string, GeoAnalysisResult> $detailed
30
     */
31
    private Collection $detailed;
32
33
    /**
34
     * Constructor for GeoAnalysisResults
35
     */
36
    public function __construct()
37
    {
38
        $this->global = new GeoAnalysisResult('Global', 0);
39
        $this->detailed = new Collection();
40
    }
41
42
    /**
43
     * Global result of the geographical analysis
44
     *
45
     * @return GeoAnalysisResult
46
     */
47
    public function global(): GeoAnalysisResult
48
    {
49
        return $this->global;
50
    }
51
52
    /**
53
     * List of results by category of the geographical analysis
54
     *
55
     * @return Collection<string, GeoAnalysisResult>
56
     */
57
    public function detailed(): Collection
58
    {
59
        return $this->detailed;
60
    }
61
62
    /**
63
     * List of results by category of the geographical analysis.
64
     * The list is sorted first by the category order, then by the category description
65
     *
66
     * @return Collection<string, GeoAnalysisResult>
67
     */
68
    public function sortedDetailed(): Collection
69
    {
70
        return $this->detailed->sortBy([
71
            fn(GeoAnalysisResult $a, GeoAnalysisResult $b): int => $a->order() <=> $b->order(),
72
            fn(GeoAnalysisResult $a, GeoAnalysisResult $b): int =>
73
                I18N::comparator()($a->description(), $b->description())
74
        ]);
75
    }
76
77
    /**
78
     * Add a GeoAnalysis Place to the global result
79
     *
80
     * @param GeoAnalysisPlace $place
81
     */
82
    public function addPlace(GeoAnalysisPlace $place): void
83
    {
84
        $this->global()->addPlace($place);
85
    }
86
87
    /**
88
     * Add a new category to the list of results, if it does not exist yet
89
     *
90
     * @param string $description
91
     * @param int $order
92
     */
93
    public function addCategory(string $description, int $order): void
94
    {
95
        if (!$this->detailed->has($description)) {
96
            $this->detailed->put($description, new GeoAnalysisResult($description, $order));
97
        }
98
    }
99
100
    /**
101
     * Add a GeoAnalysis Place to a category result, if the category exist.
102
     *
103
     * @param string $category_name
104
     * @param GeoAnalysisPlace $place
105
     */
106
    public function addPlaceInCreatedCategory(string $category_name, GeoAnalysisPlace $place): void
107
    {
108
        if ($this->detailed->has($category_name)) {
109
            $this->detailed->get($category_name)->addPlace($place);
110
        }
111
    }
112
113
    /**
114
     * Add a GeoAnalysis Place to a category result, after creating the category if it does not exist.
115
     *
116
     * @param string $category_name
117
     * @param GeoAnalysisPlace $place
118
     */
119
    public function addPlaceInCategory(string $category_name, int $category_order, GeoAnalysisPlace $place): void
120
    {
121
        if (!$this->detailed->has($category_name)) {
122
            $this->addCategory($category_name, $category_order);
123
        }
124
        $this->addPlaceInCreatedCategory($category_name, $place);
125
    }
126
}
127