Passed
Branch main (f9aaf7)
by Jonathan
14:43
created

TitlesCardHook::accordionCard()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
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 MiscExtensions
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2009-2021, 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\MiscExtensions\Hooks;
16
17
use Fisharebest\Webtrees\Fact;
18
use Fisharebest\Webtrees\Individual;
19
use Fisharebest\Webtrees\Module\ModuleInterface;
20
use MyArtJaub\Webtrees\Contracts\Hooks\NameAccordionExtenderInterface;
21
use MyArtJaub\Webtrees\Module\Sosa\Services\SosaRecordsService;
22
23
/**
24
 * Hook for displaying the individual's title in the names accordion.
25
 */
26
class TitlesCardHook implements NameAccordionExtenderInterface
27
{
28
    private ModuleInterface $module;
29
30
    /**
31
     * Constructor for TitlesCardHook
32
     *
33
     * @param ModuleInterface $module
34
     */
35
    public function __construct(ModuleInterface $module)
36
    {
37
        $this->module = $module;
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\HookInterface::module()
43
     */
44
    public function module(): ModuleInterface
45
    {
46
        return $this->module;
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     * @see \MyArtJaub\Webtrees\Contracts\Hooks\NameAccordionExtenderInterface::accordionCard()
52
     */
53
    public function accordionCard(Individual $individual): string
54
    {
55
        $title_separator = $this->module->getPreference('MAJ_TITLE_PREFIX');
56
        if ($title_separator === '') {
57
            return '';
58
        }
59
60
        $titles = $this->individualTitles($individual, '/(.*?) ((' . $title_separator .  ')(.*))/i');
61
62
        return count($titles) === 0 ? '' :
63
            view($this->module()->name() . '::components/card-titles', [ 'titles' => $titles ]);
64
    }
65
66
    /**
67
     * Extract the individual titles from the TITL tags.
68
     * Split the title based on a pattern to identify the title and the land it refers to.
69
     *
70
     * @param Individual $individual
71
     * @param string $pattern
72
     * @return array<string, string[]>
73
     */
74
    protected function individualTitles(Individual $individual, string $pattern): array
75
    {
76
        $titles_list = [];
77
        /** @var \Illuminate\Support\Collection<string> $titles */
78
        $titles = $individual->facts(['TITL'])
79
            ->sortByDesc(fn(Fact $fact) => $fact->date()->julianDay())
80
            ->map(fn(Fact $fact) => $fact->value());
81
82
        foreach ($titles as $title) {
83
            if (preg_match($pattern, $title, $match) === 1) {
84
                /** @var array<int, string> $match */
85
                $titles_list[$match[1]][] = trim($match[2]);
86
            } else {
87
                $titles_list[$title][] = '';
88
            }
89
        }
90
        return $titles_list;
91
    }
92
}
93