1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16namespace Fisharebest\Webtrees\GedcomCode;
17
18use Fisharebest\Webtrees\GedcomRecord;
19use Fisharebest\Webtrees\I18N;
20use Fisharebest\Webtrees\Individual;
21
22/**
23 * Class GedcomCodeName - Functions and logic for GEDCOM "NAME" codes
24 */
25class GedcomCodeName
26{
27    /** @var string[] A list of possible types of name */
28    private static $TYPES = array('adopted', 'aka', 'birth', 'change', 'estate', 'immigrant', 'maiden', 'married', 'religious');
29
30    /**
31     * Translate a code, for an (optional) record
32     *
33     * @param string               $type
34     * @param GedcomRecord|null $record
35     *
36     * @return string
37     */
38    public static function getValue($type, GedcomRecord $record = null)
39    {
40        if ($record instanceof Individual) {
41            $sex = $record->getSex();
42        } else {
43            $sex = 'U';
44        }
45
46        switch ($type) {
47            case 'adopted':
48                switch ($sex) {
49                    case 'M':
50                        /* I18N: The name given to a child by its adoptive parents */
51                        return I18N::translateContext('MALE', 'adopted name');
52                    case 'F':
53                        /* I18N: The name given to a child by its adoptive parents */
54                    return I18N::translateContext('FEMALE', 'adopted name');
55                    default:
56                        /* I18N: The name given to a child by its adoptive parents */
57                    return I18N::translate('adopted name');
58                }
59            case 'aka':
60                switch ($sex) {
61                    case 'M':
62                        /* I18N: The name by which an individual is also known. e.g. a professional name or a stage name */
63                        return I18N::translateContext('MALE', 'also known as');
64                    case 'F':
65                        /* I18N: The name by which an individual is also known. e.g. a professional name or a stage name */
66                    return I18N::translateContext('FEMALE', 'also known as');
67                    default:
68                        /* I18N: The name by which an individual is also known. e.g. a professional name or a stage name */
69                    return I18N::translate('also known as');
70                }
71            case 'birth':
72                switch ($sex) {
73                    case 'M':
74                        /* I18N: The name given to an individual at their birth */
75                        return I18N::translateContext('MALE', 'birth name');
76                    case 'F':
77                        /* I18N: The name given to an individual at their birth */
78                    return I18N::translateContext('FEMALE', 'birth name');
79                    default:
80                        /* I18N: The name given to an individual at their birth */
81                    return I18N::translate('birth name');
82                }
83            case 'change':
84                switch ($sex) {
85                    case 'M':
86                        /* I18N: A name chosen by an individual, to replace their existing name (whether legal or otherwise) */
87                        return I18N::translateContext('MALE', 'change of name');
88                    case 'F':
89                        /* I18N: A name chosen by an individual, to replace their existing name (whether legal or otherwise) */
90                    return I18N::translateContext('FEMALE', 'change of name');
91                    default:
92                        /* I18N: A name chosen by an individual, to replace their existing name (whether legal or otherwise) */
93                    return I18N::translate('change of name');
94                }
95            case 'estate':
96                switch ($sex) {
97                    case 'M':
98                        /* I18N: A name given to an individual, from the farm or estate on which they lived or worked */
99                        return I18N::translateContext('MALE', 'estate name');
100                    case 'F':
101                        /* I18N: A name given to an individual, from the farm or estate on which they lived or worked */
102                    return I18N::translateContext('FEMALE', 'estate name');
103                    default:
104                        /* I18N: A name given to an individual, from the farm or estate on which they lived or worked */
105                    return I18N::translate('estate name');
106                }
107            case 'immigrant':
108                switch ($sex) {
109                    case 'M':
110                        /* I18N: A name taken on immigration - e.g. migrants to the USA frequently anglicized their names */
111                        return I18N::translateContext('MALE', 'immigration name');
112                    case 'F':
113                        /* I18N: A name taken on immigration - e.g. migrants to the USA frequently anglicized their names */
114                    return I18N::translateContext('FEMALE', 'immigration name');
115                    default:
116                        /* I18N: A name taken on immigration - e.g. migrants to the USA frequently anglicized their names */
117                    return I18N::translate('immigration name');
118                }
119            case 'maiden':
120                // Only women have “maiden” names!
121                return
122                /* I18N: A woman’s name, before she marries (in cultures where women take their new husband’s name on marriage) */
123                I18N::translate('maiden name');
124            case 'married':
125                switch ($sex) {
126                    case 'M':
127                        /* I18N: A name taken on marriage - usually the wife takes the husband’s surname */
128                        return I18N::translateContext('MALE', 'married name');
129                    case 'F':
130                        /* I18N: A name taken on marriage - usually the wife takes the husband’s surname */
131                    return I18N::translateContext('FEMALE', 'married name');
132                    default:
133                        /* I18N: A name taken on marriage - usually the wife takes the husband’s surname */
134                    return I18N::translate('married name');
135                }
136            case 'religious':
137                switch ($sex) {
138                    case 'M':
139                        /* I18N: A name taken when entering a religion or a religious order */
140                        return I18N::translateContext('MALE', 'religious name');
141                    case 'F':
142                        /* I18N: A name taken when entering a religion or a religious order */
143                    return I18N::translateContext('FEMALE', 'religious name');
144                    default:
145                        /* I18N: A name taken when entering a religion or a religious order */
146                    return I18N::translate('religious name');
147                }
148            default:
149                return $type;
150        }
151    }
152
153    /**
154     * A list of all possible values for NAME types
155     *
156     * @param GedcomRecord|null $record
157     *
158     * @return string[]
159     */
160    public static function getValues(GedcomRecord $record = null)
161    {
162        $values = array();
163        foreach (self::$TYPES as $type) {
164            $values[$type] = self::getValue($type, $record);
165        }
166        uasort($values, '\Fisharebest\Webtrees\I18N::strcasecmp');
167
168        return $values;
169    }
170}
171