Passed
Push — main ( 195893...a2ce2d )
by Jonathan
06:26 queued 01:03
created

TopFilteredPlaceMapperTrait::endsWith()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
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\Module\GeoDispersion\PlaceMappers;
16
17
use Fisharebest\Webtrees\Place;
18
19
/**
20
 * Trait for Place Mappers filtering on a defined list of higher level places.
21
 */
22
trait TopFilteredPlaceMapperTrait
23
{
24
    /**
25
     * @var Place[] $top_places
26
     */
27
    private array $top_places = [];
28
29
    /**
30
     * Get the list of top level places.
31
     *
32
     * @return Place[]
33
     */
34
    public function topPlaces(): array
35
    {
36
        return $this->top_places;
37
    }
38
39
    /**
40
     * Set the list of defined top level places.
41
     *
42
     * @param Place[] $top_places
43
     */
44
    public function setTopPlaces(array $top_places): void
45
    {
46
        $this->top_places = collect($top_places)
47
            ->filter(
48
                /** @psalm-suppress MissingClosureParamType */
49
                fn($top_places): bool => $top_places instanceof Place
50
            )->toArray();
51
    }
52
53
    /**
54
     * Check whether a Place belongs to one of the defined top places.
55
     *
56
     * @param Place $place
57
     * @return bool
58
     */
59
    protected function belongsToTopLevels(Place $place): bool
60
    {
61
        foreach ($this->top_places as $top_place) {
62
            if (
63
                $top_place->tree()->id() === $place->tree()->id() &&
64
                str_ends_with($place->gedcomName(), $top_place->gedcomName())
65
            ) {
66
                    return true;
67
            }
68
        }
69
        return false;
70
    }
71
}
72