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