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