xref: /webtrees/app/Factories/SourceFactory.php (revision 6f4ec3cadc983f0a7294108c634bef48846b4311)
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     * @param string      $xref
43     * @param Tree        $tree
44     * @param string|null $gedcom
45     *
46     * @return Source|null
47     */
48    public function make(string $xref, Tree $tree, string $gedcom = null): ?Source
49    {
50        return Registry::cache()->array()->remember(self::class . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) {
51            $gedcom ??= $this->gedcom($xref, $tree);
52            $pending = $this->pendingChanges($tree)->get($xref);
53
54            if ($gedcom === null && ($pending === null || !preg_match(self::TYPE_CHECK_REGEX, $pending))) {
55                return null;
56            }
57
58            $xref = $this->extractXref($gedcom ?? $pending, $xref);
59
60            return $this->new($xref, $gedcom ?? '', $pending, $tree);
61        });
62    }
63
64    /**
65     * Create a source from a row in the database.
66     *
67     * @param Tree $tree
68     *
69     * @return Closure(object):Source
70     */
71    public function mapper(Tree $tree): Closure
72    {
73        return fn (object $row): Source => $this->make($row->s_id, $tree, $row->s_gedcom);
74    }
75
76    /**
77     * Create a source from raw GEDCOM data.
78     *
79     * @param string      $xref
80     * @param string      $gedcom  an empty string for new/pending records
81     * @param string|null $pending null for a record with no pending edits,
82     *                             empty string for records with pending deletions
83     * @param Tree        $tree
84     *
85     * @return Source
86     */
87    public function new(string $xref, string $gedcom, ?string $pending, Tree $tree): Source
88    {
89        return new Source($xref, $gedcom, $pending, $tree);
90    }
91
92    /**
93     * Fetch GEDCOM data from the database.
94     *
95     * @param string $xref
96     * @param Tree   $tree
97     *
98     * @return string|null
99     */
100    protected function gedcom(string $xref, Tree $tree): ?string
101    {
102        return DB::table('sources')
103            ->where('s_id', '=', $xref)
104            ->where('s_file', '=', $tree->id())
105            ->value('s_gedcom');
106    }
107}
108