xref: /webtrees/app/Factories/SubmitterFactory.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Factories;
21
22use Closure;
23use Fisharebest\Webtrees\Contracts\SubmitterFactoryInterface;
24use Fisharebest\Webtrees\DB;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Submitter;
27use Fisharebest\Webtrees\Tree;
28
29use function preg_match;
30
31/**
32 * Make a Submitter object.
33 */
34class SubmitterFactory extends AbstractGedcomRecordFactory implements SubmitterFactoryInterface
35{
36    private const TYPE_CHECK_REGEX = '/^0 @[^@]+@ ' . Submitter::RECORD_TYPE . '/';
37
38    /**
39     * Create a submitter.
40     */
41    public function make(string $xref, Tree $tree, string|null $gedcom = null): Submitter|null
42    {
43        return Registry::cache()->array()->remember(self::class . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) {
44            $gedcom ??= $this->gedcom($xref, $tree);
45            $pending = $this->pendingChanges($tree)->get($xref);
46
47            if ($gedcom === null && ($pending === null || !preg_match(self::TYPE_CHECK_REGEX, $pending))) {
48                return null;
49            }
50
51            $xref = $this->extractXref($gedcom ?? $pending, $xref);
52
53            return $this->new($xref, $gedcom ?? '', $pending, $tree);
54        });
55    }
56
57    /**
58     * Create a submitter from a row in the database.
59     *
60     * @param Tree $tree
61     *
62     * @return Closure(object):Submitter
63     */
64    public function mapper(Tree $tree): Closure
65    {
66        return fn (object $row): Submitter => $this->make($row->o_id, $tree, $row->o_gedcom);
67    }
68
69    /**
70     * Create a submitter from raw GEDCOM data.
71     *
72     * @param string      $xref
73     * @param string      $gedcom  an empty string for new/pending records
74     * @param string|null $pending null for a record with no pending edits,
75     *                             empty string for records with pending deletions
76     * @param Tree        $tree
77     *
78     * @return Submitter
79     */
80    public function new(string $xref, string $gedcom, string|null $pending, Tree $tree): Submitter
81    {
82        return new Submitter($xref, $gedcom, $pending, $tree);
83    }
84
85    /**
86     * Fetch GEDCOM data from the database.
87     *
88     * @param string $xref
89     * @param Tree   $tree
90     *
91     * @return string|null
92     */
93    protected function gedcom(string $xref, Tree $tree): string|null
94    {
95        return DB::table('other')
96            ->where('o_id', '=', $xref)
97            ->where('o_file', '=', $tree->id())
98            ->where('o_type', '=', Submitter::RECORD_TYPE)
99            ->value('o_gedcom');
100    }
101}
102