xref: /webtrees/app/Factories/FamilyFactory.php (revision 531c6e7cb7a0ab5fa4bcb50613b814dd81c279e0)
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\FamilyFactoryInterface;
24use Fisharebest\Webtrees\DB;
25use Fisharebest\Webtrees\Family;
26use Fisharebest\Webtrees\Gedcom;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Tree;
29
30use function preg_match;
31
32/**
33 * Make a Family object.
34 */
35class FamilyFactory extends AbstractGedcomRecordFactory implements FamilyFactoryInterface
36{
37    private const TYPE_CHECK_REGEX = '/^0 @[^@]+@ ' . Family::RECORD_TYPE . '/';
38
39    /**
40     * Create a family.
41     *
42     * @param string      $xref
43     * @param Tree        $tree
44     * @param string|null $gedcom
45     *
46     * @return Family|null
47     */
48    public function make(string $xref, Tree $tree, string $gedcom = null): ?Family
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            // Preload all the family members using a single database query.
61            preg_match_all('/\n1 (?:HUSB|WIFE|CHIL) @(' . Gedcom::REGEX_XREF . ')@/', $gedcom . "\n" . $pending, $match);
62            DB::table('individuals')
63                ->where('i_file', '=', $tree->id())
64                ->whereIn('i_id', $match[1])
65                ->get()
66                ->map(Registry::individualFactory()->mapper($tree));
67
68            return $this->new($xref, $gedcom ?? '', $pending, $tree);
69        });
70    }
71
72    /**
73     * Create a Family object from a row in the database.
74     *
75     * @param Tree $tree
76     *
77     * @return Closure(object):Family
78     */
79    public function mapper(Tree $tree): Closure
80    {
81        return fn (object $row): Family => $this->make($row->f_id, $tree, $row->f_gedcom);
82    }
83
84    /**
85     * Create a Family object from raw GEDCOM data.
86     *
87     * @param string      $xref
88     * @param string      $gedcom  an empty string for new/pending records
89     * @param string|null $pending null for a record with no pending edits,
90     *                             empty string for records with pending deletions
91     * @param Tree        $tree
92     *
93     * @return Family
94     */
95    public function new(string $xref, string $gedcom, ?string $pending, Tree $tree): Family
96    {
97        return new Family($xref, $gedcom, $pending, $tree);
98    }
99
100    /**
101     * Fetch GEDCOM data from the database.
102     *
103     * @param string $xref
104     * @param Tree   $tree
105     *
106     * @return string|null
107     */
108    protected function gedcom(string $xref, Tree $tree): ?string
109    {
110        return DB::table('families')
111            ->where('f_id', '=', $xref)
112            ->where('f_file', '=', $tree->id())
113            ->value('f_gedcom');
114    }
115}
116