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