xref: /webtrees/app/Factories/SourceFactory.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\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    /**
40     * Create a individual.
41     */
42    public function make(string $xref, Tree $tree, string|null $gedcom = null): Source|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):Source
64     */
65    public function mapper(Tree $tree): Closure
66    {
67        return fn (object $row): Source => $this->make($row->s_id, $tree, $row->s_gedcom);
68    }
69
70    /**
71     * Create a source from raw GEDCOM data.
72     *
73     * @param string      $xref
74     * @param string      $gedcom  an empty string for new/pending records
75     * @param string|null $pending null for a record with no pending edits,
76     *                             empty string for records with pending deletions
77     * @param Tree        $tree
78     *
79     * @return Source
80     */
81    public function new(string $xref, string $gedcom, string|null $pending, Tree $tree): Source
82    {
83        return new Source($xref, $gedcom, $pending, $tree);
84    }
85
86    /**
87     * Fetch GEDCOM data from the database.
88     *
89     * @param string $xref
90     * @param Tree   $tree
91     *
92     * @return string|null
93     */
94    protected function gedcom(string $xref, Tree $tree): string|null
95    {
96        return DB::table('sources')
97            ->where('s_id', '=', $xref)
98            ->where('s_file', '=', $tree->id())
99            ->value('s_gedcom');
100    }
101}
102