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