xref: /webtrees/app/Factories/SourceFactory.php (revision 9d74ed0ab3ae61aa8ae07578e026f14020844ce9)
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\SourceFactoryInterface;
24use Fisharebest\Webtrees\DB;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Source;
27use Fisharebest\Webtrees\Tree;
28
29use function preg_match;
30
31/**
32 * Make a Source object.
33 */
34class SourceFactory extends AbstractGedcomRecordFactory implements SourceFactoryInterface
35{
36    private const TYPE_CHECK_REGEX = '/^0 @[^@]+@ ' . Source::RECORD_TYPE . '/';
37
38    /**
39     * Create a individual.
40     */
41    public function make(string $xref, Tree $tree, string|null $gedcom = null): Source|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 source from a row in the database.
59     *
60     * @param Tree $tree
61     *
62     * @return Closure(object):Source
63     */
64    public function mapper(Tree $tree): Closure
65    {
66        return fn (object $row): Source => $this->make($row->s_id, $tree, $row->s_gedcom);
67    }
68
69    /**
70     * Create a source 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 Source
79     */
80    public function new(string $xref, string $gedcom, string|null $pending, Tree $tree): Source
81    {
82        return new Source($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('sources')
96            ->where('s_id', '=', $xref)
97            ->where('s_file', '=', $tree->id())
98            ->value('s_gedcom');
99    }
100}
101