Passed
Push — main ( b1997c...78fee3 )
by Greg
06:03
created

NamePersonal   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
A default() 0 5 1
A edit() 0 9 1
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2022 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Elements;
21
22
use Fisharebest\Webtrees\I18N;
23
use Fisharebest\Webtrees\Registry;
24
use Fisharebest\Webtrees\Tree;
25
26
use function e;
27
use function in_array;
28
use function view;
29
30
/**
31
 * NAME_PERSONAL := {Size=1:120}
32
 * [
33
 * <NAME_TEXT> | /<NAME_TEXT>/ |
34
 * <NAME_TEXT> /<NAME_TEXT>/ | /<NAME_TEXT>/ <NAME_TEXT> |
35
 * <NAME_TEXT> /<NAME_TEXT>/ <NAME_TEXT> ]
36
 * The surname of an individual, if known, is enclosed between two slash (/)
37
 * characters. The order of the name parts should be the order that the person
38
 * would, by custom of their culture, have used when giving it to a recorder.
39
 * Early versions of Personal Ancestral File ® and other products did not use
40
 * the trailing slash when the surname was the last element of the name. If
41
 * part of name is illegible, that part is indicated by an ellipsis (...).
42
 * Capitalize the name of a person or place in the conventional manner—
43
 * capitalize the first letter of each part and lowercase the other letters,
44
 * unless conventional usage is otherwise. For example: McMurray.
45
 * Examples:
46
 * William Lee (given name only or surname not known)
47
 * /Parry/ (surname only)
48
 * William Lee /Parry/
49
 * William Lee /Mac Parry/ (both parts (Mac and Parry) are surname parts
50
 * William /Lee/ Parry (surname imbedded in the name string)
51
 * William Lee /Pa.../
52
 */
53
class NamePersonal extends AbstractElement
54
{
55
    protected const MAXIMUM_LENGTH = 120;
56
57
    protected const SUBTAGS = [
58
        'TYPE' => '0:1',
59
        'NPFX' => '0:1',
60
        'GIVN' => '0:1',
61
        'SPFX' => '0:1',
62
        'SURN' => '0:1',
63
        'NSFX' => '0:1',
64
        'NICK' => '0:1',
65
        'NOTE' => '0:M',
66
        'SOUR' => '0:M',
67
        'FONE' => '0:M',
68
        'ROMN' => '0:M',
69
    ];
70
71
    // For some languages, we want to show the surname field first.
72
    protected const SURNAME_FIRST_LANGUAGES = ['hu', 'jp', 'ko', 'zh-Hans', 'zh-Hant'];
73
74
    protected const SUBTAGS_SURNAME_FIRST = [
75
        'TYPE' => '0:1',
76
        'NPFX' => '0:1',
77
        'SPFX' => '0:1',
78
        'SURN' => '0:1',
79
        'GIVN' => '0:1',
80
        'NSFX' => '0:1',
81
        'NICK' => '0:1',
82
        'NOTE' => '0:M',
83
        'SOUR' => '0:M',
84
        'FONE' => '0:M',
85
        'ROMN' => '0:M',
86
    ];
87
88
    /**
89
     * AbstractGedcomElement constructor.
90
     *
91
     * @param string             $label
92
     * @param array<string>|null $subtags
93
     */
94
    public function __construct(string $label, array $subtags = null)
95
    {
96
97
        if ($subtags === null && in_array(I18N::languageTag(), static::SURNAME_FIRST_LANGUAGES, true)) {
98
            $subtags = static::SUBTAGS_SURNAME_FIRST;
99
        }
100
        parent::__construct($label, $subtags);
101
    }
102
103
    /**
104
     * Create a default value for this element.
105
     *
106
     * @param Tree $tree
107
     *
108
     * @return string
109
     */
110
    public function default(Tree $tree): string
111
    {
112
        return Registry::surnameTraditionFactory()
113
            ->make($tree->getPreference('SURNAME_TRADITION'))
114
            ->defaultName();
115
    }
116
117
    /**
118
     * An edit control for this data.
119
     *
120
     * @param string $id
121
     * @param string $name
122
     * @param string $value
123
     * @param Tree   $tree
124
     *
125
     * @return string
126
     */
127
    public function edit(string $id, string $name, string $value, Tree $tree): string
128
    {
129
        return
130
            '<div class="input-group">' .
131
            view('edit/input-addon-edit-name', ['id' => $id]) .
132
            '<input class="form-control" type="text" id="' . e($id) . '" name="' . e($name) . '" value="' . e($value) . '" readonly="readonly" />' .
133
            view('edit/input-addon-keyboard', ['id' => $id]) .
134
            view('edit/input-addon-help', ['topic' => 'NAME']) .
135
            '</div>';
136
    }
137
}
138