GeoAnalysisDataService   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A placeHierarchyExample() 0 25 1
A individualsAndFamilies() 0 17 1
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\Module\GeoDispersion\Services;
16
17
use Fisharebest\Webtrees\GedcomRecord;
18
use Fisharebest\Webtrees\I18N;
19
use Fisharebest\Webtrees\Registry;
20
use Fisharebest\Webtrees\Tree;
21
use Illuminate\Database\Capsule\Manager as DB;
22
use Generator;
23
24
/**
25
 * Service for accessing data used by geographical analyses in the GeoDispersion module.
26
 */
27
class GeoAnalysisDataService
28
{
29
    /**
30
     * Yields indviduals and family records for a specified tree.
31
     *
32
     * @param Tree $tree
33
     * @return \Generator<\Fisharebest\Webtrees\GedcomRecord>
34
     */
35
    public function individualsAndFamilies(Tree $tree): Generator
36
    {
37
        yield from DB::table('individuals')
38
            ->where('i_file', '=', $tree->id())
39
            ->select(['individuals.*'])
40
            ->get()
41
            ->map(Registry::individualFactory()->mapper($tree))
42
            ->filter(GedcomRecord::accessFilter())
43
            ->all();
44
45
        yield from DB::table('families')
46
            ->where('f_file', '=', $tree->id())
47
            ->select(['families.*'])
48
            ->get()
49
            ->map(Registry::familyFactory()->mapper($tree))
50
            ->filter(GedcomRecord::accessFilter())
51
            ->all();
52
    }
53
54
    /**
55
     * Returns an example of the place hierarchy, from a place within the GEDCOM file, looking for the deepest
56
     * hierarchy found. The part order is reversed compared to the normal GEDCOM structure (largest first).
57
     *
58
     * {@internal The places are taken only from the individuals and families records.}
59
     *
60
     * @param Tree $tree
61
     * @return array<int, string[]>
62
     */
63
    public function placeHierarchyExample(Tree $tree): array
64
    {
65
        $query_individuals = DB::table('individuals')
66
            ->select(['i_gedcom AS g_gedcom'])
67
            ->where('i_file', '=', $tree->id())
68
            ->where('i_gedcom', 'like', '%2 PLAC %');
69
70
        $query_families = DB::table('families')
71
            ->select(['f_gedcom AS g_gedcom'])
72
            ->where('f_file', '=', $tree->id())
73
            ->where('f_gedcom', 'like', '%2 PLAC %');
74
75
        return $query_individuals->unionAll($query_families)
76
            ->get()->pluck('g_gedcom')
77
            ->flatMap(static function (string $gedcom): array {
78
                preg_match_all('/\n2 PLAC (.+)/', $gedcom, $matches);
79
                return $matches[1] ?? [];
80
            })
81
            ->sort(I18N::comparator())->reverse()
82
            ->mapWithKeys(static function (string $place): array {
83
                $place_array = array_reverse(array_filter(array_map('trim', explode(",", $place))));
84
                return [ count($place_array) => $place_array ];
85
            })
86
            ->sortKeys()
87
            ->last() ?? [];
88
    }
89
}
90