xref: /webtrees/app/Elements/MaidenheadLocator.php (revision d11be7027e34e3121be11cc025421873364403f9)
1ae0043b7SGreg Roach<?php
2ae0043b7SGreg Roach
3ae0043b7SGreg Roach/**
4ae0043b7SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6ae0043b7SGreg Roach * This program is free software: you can redistribute it and/or modify
7ae0043b7SGreg Roach * it under the terms of the GNU General Public License as published by
8ae0043b7SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9ae0043b7SGreg Roach * (at your option) any later version.
10ae0043b7SGreg Roach * This program is distributed in the hope that it will be useful,
11ae0043b7SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ae0043b7SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13ae0043b7SGreg Roach * GNU General Public License for more details.
14ae0043b7SGreg Roach * You should have received a copy of the GNU General Public License
15ae0043b7SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16ae0043b7SGreg Roach */
17ae0043b7SGreg Roach
18ae0043b7SGreg Roachdeclare(strict_types=1);
19ae0043b7SGreg Roach
20ae0043b7SGreg Roachnamespace Fisharebest\Webtrees\Elements;
21ae0043b7SGreg Roach
22ae0043b7SGreg Roachuse function mb_strlen;
23ae0043b7SGreg Roachuse function mb_strtolower;
24ae0043b7SGreg Roachuse function mb_strtoupper;
25ae0043b7SGreg Roachuse function mb_substr;
26ae0043b7SGreg Roach
27ae0043b7SGreg Roach/**
28ae0043b7SGreg Roach * @see https://en.wikipedia.org/wiki/Maidenhead_Locator_System
29ae0043b7SGreg Roach */
30ae0043b7SGreg Roachclass MaidenheadLocator extends AbstractElement
31ae0043b7SGreg Roach{
32ae0043b7SGreg Roach    protected const MAXIMUM_LENGTH = 8;
33ae0043b7SGreg Roach    protected const PATTERN        = '[A-Ra-r][A-Ra-r]([0-9][0-9]([A-Xa-x][A-Xa-x]([0-9][0-9])?)?)?';
34ae0043b7SGreg Roach
35ae0043b7SGreg Roach    /**
36ae0043b7SGreg Roach     * Convert a value to a canonical form.
37ae0043b7SGreg Roach     *
38ae0043b7SGreg Roach     * @param string $value
39ae0043b7SGreg Roach     *
40ae0043b7SGreg Roach     * @return string
41ae0043b7SGreg Roach     */
42ae0043b7SGreg Roach    public function canonical(string $value): string
43ae0043b7SGreg Roach    {
44ae0043b7SGreg Roach        $value = parent::canonical($value);
45ae0043b7SGreg Roach
46ae0043b7SGreg Roach        // Characters 1 and 2 are upper case
47ae0043b7SGreg Roach        if (mb_strlen($value) >= 2) {
48ae0043b7SGreg Roach            $value = mb_strtoupper(mb_substr($value, 0, 2)) . mb_substr($value, 2);
49ae0043b7SGreg Roach        }
50ae0043b7SGreg Roach
51ae0043b7SGreg Roach        // Characters 5 and 6 are lower case
52ae0043b7SGreg Roach        if (mb_strlen($value) >= 6) {
53ae0043b7SGreg Roach            $value = mb_substr($value, 0, 4) . mb_strtolower(mb_substr($value, 4, 2)) . mb_substr($value, 6);
54ae0043b7SGreg Roach        }
55ae0043b7SGreg Roach
56ae0043b7SGreg Roach        return $value;
57ae0043b7SGreg Roach    }
58ae0043b7SGreg Roach}
59