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