xref: /webtrees/app/Factories/LocationFactory.php (revision 6b9cb339562c8aa4681c7eff2bf3bdf401cc9edd)
10d15532eSGreg Roach<?php
20d15532eSGreg Roach
30d15532eSGreg Roach/**
40d15532eSGreg Roach * webtrees: online genealogy
569675509SGreg Roach * Copyright (C) 2020 webtrees development team
60d15532eSGreg Roach * This program is free software: you can redistribute it and/or modify
70d15532eSGreg Roach * it under the terms of the GNU General Public License as published by
80d15532eSGreg Roach * the Free Software Foundation, either version 3 of the License, or
90d15532eSGreg Roach * (at your option) any later version.
100d15532eSGreg Roach * This program is distributed in the hope that it will be useful,
110d15532eSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
120d15532eSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
130d15532eSGreg Roach * GNU General Public License for more details.
140d15532eSGreg Roach * You should have received a copy of the GNU General Public License
150d15532eSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
160d15532eSGreg Roach */
170d15532eSGreg Roach
180d15532eSGreg Roachdeclare(strict_types=1);
190d15532eSGreg Roach
200d15532eSGreg Roachnamespace Fisharebest\Webtrees\Factories;
210d15532eSGreg Roach
220d15532eSGreg Roachuse Closure;
230d15532eSGreg Roachuse Fisharebest\Webtrees\Contracts\LocationFactoryInterface;
240d15532eSGreg Roachuse Fisharebest\Webtrees\Location;
25*6b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
260d15532eSGreg Roachuse Fisharebest\Webtrees\Tree;
270d15532eSGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
280d15532eSGreg Roachuse stdClass;
290d15532eSGreg Roach
300d15532eSGreg Roachuse function assert;
310d15532eSGreg Roachuse function preg_match;
320d15532eSGreg Roach
330d15532eSGreg Roach/**
340d15532eSGreg Roach * Make a Location object.
350d15532eSGreg Roach */
360d15532eSGreg Roachclass LocationFactory extends AbstractGedcomRecordFactory implements LocationFactoryInterface
370d15532eSGreg Roach{
380d15532eSGreg Roach    private const TYPE_CHECK_REGEX = '/^0 @[^@]+@ ' . Location::RECORD_TYPE . '/';
390d15532eSGreg Roach
400d15532eSGreg Roach    /**
410d15532eSGreg Roach     * Create a Location.
420d15532eSGreg Roach     *
430d15532eSGreg Roach     * @param string      $xref
440d15532eSGreg Roach     * @param Tree        $tree
450d15532eSGreg Roach     * @param string|null $gedcom
460d15532eSGreg Roach     *
470d15532eSGreg Roach     * @return Location|null
480d15532eSGreg Roach     */
490d15532eSGreg Roach    public function make(string $xref, Tree $tree, string $gedcom = null): ?Location
500d15532eSGreg Roach    {
51*6b9cb339SGreg Roach        return Registry::cache()->array()->remember(__CLASS__ . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) {
520d15532eSGreg Roach            $gedcom  = $gedcom ?? $this->gedcom($xref, $tree);
530d15532eSGreg Roach            $pending = $this->pendingChanges($tree)->get($xref);
540d15532eSGreg Roach
550d15532eSGreg Roach            if ($gedcom === null && ($pending === null || !preg_match(self::TYPE_CHECK_REGEX, $pending))) {
560d15532eSGreg Roach                return null;
570d15532eSGreg Roach            }
580d15532eSGreg Roach
590d15532eSGreg Roach            $xref = $this->extractXref($gedcom ?? $pending, $xref);
600d15532eSGreg Roach
610d15532eSGreg Roach            return new Location($xref, $gedcom ?? '', $pending, $tree);
620d15532eSGreg Roach        });
630d15532eSGreg Roach    }
640d15532eSGreg Roach
650d15532eSGreg Roach    /**
660d15532eSGreg Roach     * Create a Location from a row in the database.
670d15532eSGreg Roach     *
680d15532eSGreg Roach     * @param Tree $tree
690d15532eSGreg Roach     *
700d15532eSGreg Roach     * @return Closure
710d15532eSGreg Roach     */
720d15532eSGreg Roach    public function mapper(Tree $tree): Closure
730d15532eSGreg Roach    {
740d15532eSGreg Roach        return function (stdClass $row) use ($tree): Location {
750874af26SRichard Cissée            $location = $this->make($row->o_id, $tree, $row->o_gedcom);
760874af26SRichard Cissée            assert($location instanceof Location);
770d15532eSGreg Roach
780874af26SRichard Cissée            return $location;
790d15532eSGreg Roach        };
800d15532eSGreg Roach    }
810d15532eSGreg Roach
820d15532eSGreg Roach    /**
830d15532eSGreg Roach     * Create a Location from raw GEDCOM data.
840d15532eSGreg Roach     *
850d15532eSGreg Roach     * @param string      $xref
860d15532eSGreg Roach     * @param string      $gedcom  an empty string for new/pending records
870d15532eSGreg Roach     * @param string|null $pending null for a record with no pending edits,
880d15532eSGreg Roach     *                             empty string for records with pending deletions
890d15532eSGreg Roach     * @param Tree        $tree
900d15532eSGreg Roach     *
910d15532eSGreg Roach     * @return Location
920d15532eSGreg Roach     */
930d15532eSGreg Roach    public function new(string $xref, string $gedcom, ?string $pending, Tree $tree): Location
940d15532eSGreg Roach    {
950d15532eSGreg Roach        return new Location($xref, $gedcom, $pending, $tree);
960d15532eSGreg Roach    }
970d15532eSGreg Roach
980d15532eSGreg Roach    /**
990d15532eSGreg Roach     * Fetch GEDCOM data from the database.
1000d15532eSGreg Roach     *
1010d15532eSGreg Roach     * @param string $xref
1020d15532eSGreg Roach     * @param Tree   $tree
1030d15532eSGreg Roach     *
1040d15532eSGreg Roach     * @return string|null
1050d15532eSGreg Roach     */
1060d15532eSGreg Roach    protected function gedcom(string $xref, Tree $tree): ?string
1070d15532eSGreg Roach    {
1080d15532eSGreg Roach        return DB::table('other')
1090d15532eSGreg Roach            ->where('o_id', '=', $xref)
1100d15532eSGreg Roach            ->where('o_file', '=', $tree->id())
1110d15532eSGreg Roach            ->where('o_type', '=', Location::RECORD_TYPE)
1120d15532eSGreg Roach            ->value('o_gedcom');
1130d15532eSGreg Roach    }
1140d15532eSGreg Roach}
115