summaryrefslogtreecommitdiff
path: root/app/Factories/GedcomRecordFactory.php
blob: ab59b5aaea232aa1201f2dd5cc5ef4bae390fe37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php

/**
 * webtrees: online genealogy
 * Copyright (C) 2026 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

declare(strict_types=1);

namespace Fisharebest\Webtrees\Factories;

use Closure;
use Fisharebest\Webtrees\Contracts\GedcomRecordFactoryInterface;
use Fisharebest\Webtrees\DB;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\Gedcom;
use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\Header;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Location;
use Fisharebest\Webtrees\Media;
use Fisharebest\Webtrees\Note;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Repository;
use Fisharebest\Webtrees\Source;
use Fisharebest\Webtrees\Submission;
use Fisharebest\Webtrees\Submitter;
use Fisharebest\Webtrees\Tree;
use InvalidArgumentException;

/**
 * Make a GedcomRecord object.
 */
class GedcomRecordFactory extends AbstractGedcomRecordFactory implements GedcomRecordFactoryInterface
{
    /**
     * Create a GedcomRecord object.
     */
    public function make(string $xref, Tree $tree, string|null $gedcom = null): GedcomRecord|null
    {
        // We know the type of the record.  Return it directly.
        if ($gedcom !== null && preg_match('/^0(?: @[^@]+@)? ([A-Z_]+)/', $gedcom, $match)) {
            switch ($match[1]) {
                case Family::RECORD_TYPE:
                    return Registry::familyFactory()->make($xref, $tree, $gedcom);
                case Header::RECORD_TYPE:
                    return Registry::headerFactory()->make($xref, $tree, $gedcom);
                case Individual::RECORD_TYPE:
                    return Registry::individualFactory()->make($xref, $tree, $gedcom);
                case Location::RECORD_TYPE:
                    return Registry::locationFactory()->make($xref, $tree, $gedcom);
                case Media::RECORD_TYPE:
                    return Registry::mediaFactory()->make($xref, $tree, $gedcom);
                case Note::RECORD_TYPE:
                    return Registry::noteFactory()->make($xref, $tree, $gedcom);
                case Repository::RECORD_TYPE:
                    return Registry::repositoryFactory()->make($xref, $tree, $gedcom);
                case Source::RECORD_TYPE:
                    return Registry::sourceFactory()->make($xref, $tree, $gedcom);
                case Submitter::RECORD_TYPE:
                    return Registry::submitterFactory()->make($xref, $tree, $gedcom);
                case Submission::RECORD_TYPE:
                    return Registry::submissionFactory()->make($xref, $tree, $gedcom);
            }
        }

        // We do not know the type of the record.  Try them all in turn.
        return
            Registry::familyFactory()->make($xref, $tree, $gedcom) ??
            Registry::individualFactory()->make($xref, $tree, $gedcom) ??
            Registry::mediaFactory()->make($xref, $tree, $gedcom) ??
            Registry::noteFactory()->make($xref, $tree, $gedcom) ??
            Registry::repositoryFactory()->make($xref, $tree, $gedcom) ??
            Registry::sourceFactory()->make($xref, $tree, $gedcom) ??
            Registry::submitterFactory()->make($xref, $tree, $gedcom) ??
            Registry::submissionFactory()->make($xref, $tree, $gedcom) ??
            Registry::locationFactory()->make($xref, $tree, $gedcom) ??
            Registry::headerFactory()->make($xref, $tree, $gedcom) ??
            Registry::cache()->array()->remember(self::class . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) {
                $gedcom ??= $this->gedcom($xref, $tree);

                $pending = $this->pendingChanges($tree)->get($xref);

                if ($gedcom === null && $pending === null) {
                    return null;
                }

                $xref = $this->extractXref($gedcom ?? $pending, $xref);
                $type = $this->extractType($gedcom ?? $pending);

                return $this->newGedcomRecord($type, $xref, $gedcom ?? '', $pending, $tree);
            });
    }

    /**
     * Create a GedcomRecord object from raw GEDCOM data.
     *
     * @param string      $xref
     * @param string      $gedcom  an empty string for new/pending records
     * @param string|null $pending null for a record with no pending edits,
     *                             empty string for records with pending deletions
     * @param Tree        $tree
     *
     * @return GedcomRecord
     */
    public function new(string $xref, string $gedcom, string|null $pending, Tree $tree): GedcomRecord
    {
        return new GedcomRecord($xref, $gedcom, $pending, $tree);
    }

    /**
     * Create a GedcomRecord object from a row in the database.
     *
     * @param Tree $tree
     *
     * @return Closure(object):GedcomRecord
     */
    public function mapper(Tree $tree): Closure
    {
        return fn (object $row): GedcomRecord => $this->make($row->o_id, $tree, $row->o_gedcom);
    }

    /**
     * @param string      $type
     * @param string      $xref
     * @param string      $gedcom
     * @param string|null $pending
     * @param Tree        $tree
     *
     * @return GedcomRecord
     */
    private function newGedcomRecord(string $type, string $xref, string $gedcom, string|null $pending, Tree $tree): GedcomRecord
    {
        switch ($type) {
            case Family::RECORD_TYPE:
                return Registry::familyFactory()->new($xref, $gedcom, $pending, $tree);

            case Header::RECORD_TYPE:
                return Registry::headerFactory()->new($xref, $gedcom, $pending, $tree);

            case Individual::RECORD_TYPE:
                return Registry::individualFactory()->new($xref, $gedcom, $pending, $tree);

            case Media::RECORD_TYPE:
                return Registry::mediaFactory()->new($xref, $gedcom, $pending, $tree);

            case Note::RECORD_TYPE:
                return Registry::noteFactory()->new($xref, $gedcom, $pending, $tree);

            case Repository::RECORD_TYPE:
                return Registry::repositoryFactory()->new($xref, $gedcom, $pending, $tree);

            case Source::RECORD_TYPE:
                return Registry::sourceFactory()->new($xref, $gedcom, $pending, $tree);

            case Submission::RECORD_TYPE:
                return Registry::submissionFactory()->new($xref, $gedcom, $pending, $tree);

            case Submitter::RECORD_TYPE:
                return Registry::submitterFactory()->new($xref, $gedcom, $pending, $tree);

            default:
                return $this->new($xref, $gedcom, $pending, $tree);
        }
    }

    /**
     * Extract the type of a GEDCOM record
     *
     * @param string $gedcom
     *
     * @return string
     * @throws InvalidArgumentException
     */
    private function extractType(string $gedcom): string
    {
        if (preg_match('/^0(?: @' . Gedcom::REGEX_XREF . '@)? ([_A-Z0-9]+)/', $gedcom, $match)) {
            return $match[1];
        }

        throw new InvalidArgumentException('Invalid GEDCOM record: ' . $gedcom);
    }

    /**
     * Fetch GEDCOM data from the database.
     *
     * @param string $xref
     * @param Tree   $tree
     *
     * @return string|null
     */
    private function gedcom(string $xref, Tree $tree): string|null
    {
        return DB::table('other')
            ->where('o_id', '=', $xref)
            ->where('o_file', '=', $tree->id())
            ->whereNotIn('o_type', [
                Header::RECORD_TYPE,
                Note::RECORD_TYPE,
                Repository::RECORD_TYPE,
                Submission::RECORD_TYPE,
                Submitter::RECORD_TYPE,
            ])
            ->value('o_gedcom');
    }
}