xref: /webtrees/app/Factories/FamilyFactory.php (revision 6b9cb339562c8aa4681c7eff2bf3bdf401cc9edd)
1bb03c9f0SGreg Roach<?php
2bb03c9f0SGreg Roach
3bb03c9f0SGreg Roach/**
4bb03c9f0SGreg Roach * webtrees: online genealogy
569675509SGreg Roach * Copyright (C) 2020 webtrees development team
6bb03c9f0SGreg Roach * This program is free software: you can redistribute it and/or modify
7bb03c9f0SGreg Roach * it under the terms of the GNU General Public License as published by
8bb03c9f0SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9bb03c9f0SGreg Roach * (at your option) any later version.
10bb03c9f0SGreg Roach * This program is distributed in the hope that it will be useful,
11bb03c9f0SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12bb03c9f0SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13bb03c9f0SGreg Roach * GNU General Public License for more details.
14bb03c9f0SGreg Roach * You should have received a copy of the GNU General Public License
15bb03c9f0SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16bb03c9f0SGreg Roach */
17bb03c9f0SGreg Roach
18bb03c9f0SGreg Roachdeclare(strict_types=1);
19bb03c9f0SGreg Roach
20bb03c9f0SGreg Roachnamespace Fisharebest\Webtrees\Factories;
21bb03c9f0SGreg Roach
22bb03c9f0SGreg Roachuse Closure;
23bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Contracts\FamilyFactoryInterface;
24bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Family;
25bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Gedcom;
26*6b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
27bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Tree;
28bb03c9f0SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
29bb03c9f0SGreg Roachuse stdClass;
30bb03c9f0SGreg Roach
31bb03c9f0SGreg Roachuse function assert;
32bb03c9f0SGreg Roachuse function preg_match;
33bb03c9f0SGreg Roach
34bb03c9f0SGreg Roach/**
35bb03c9f0SGreg Roach * Make a Family object.
36bb03c9f0SGreg Roach */
37bb03c9f0SGreg Roachclass FamilyFactory extends AbstractGedcomRecordFactory implements FamilyFactoryInterface
38bb03c9f0SGreg Roach{
39bb03c9f0SGreg Roach    private const TYPE_CHECK_REGEX = '/^0 @[^@]+@ ' . Family::RECORD_TYPE . '/';
40bb03c9f0SGreg Roach
41bb03c9f0SGreg Roach    /**
42bb03c9f0SGreg Roach     * Create a family.
43bb03c9f0SGreg Roach     *
44bb03c9f0SGreg Roach     * @param string      $xref
45bb03c9f0SGreg Roach     * @param Tree        $tree
46bb03c9f0SGreg Roach     * @param string|null $gedcom
47bb03c9f0SGreg Roach     *
48bb03c9f0SGreg Roach     * @return Family|null
49bb03c9f0SGreg Roach     */
50bb03c9f0SGreg Roach    public function make(string $xref, Tree $tree, string $gedcom = null): ?Family
51bb03c9f0SGreg Roach    {
52*6b9cb339SGreg Roach        return Registry::cache()->array()->remember(__CLASS__ . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) {
53bb03c9f0SGreg Roach            $gedcom  = $gedcom ?? $this->gedcom($xref, $tree);
54bb03c9f0SGreg Roach            $pending = $this->pendingChanges($tree)->get($xref);
55bb03c9f0SGreg Roach
56bb03c9f0SGreg Roach            if ($gedcom === null && ($pending === null || !preg_match(self::TYPE_CHECK_REGEX, $pending))) {
57bb03c9f0SGreg Roach                return null;
58bb03c9f0SGreg Roach            }
59bb03c9f0SGreg Roach
60bb03c9f0SGreg Roach            $xref = $this->extractXref($gedcom ?? $pending, $xref);
61bb03c9f0SGreg Roach
62bb03c9f0SGreg Roach            // Preload all the family members using a single database query.
63bb03c9f0SGreg Roach            preg_match_all('/\n1 (?:HUSB|WIFE|CHIL) @(' . Gedcom::REGEX_XREF . ')@/', $gedcom . "\n" . $pending, $match);
64bb03c9f0SGreg Roach            DB::table('individuals')
65bb03c9f0SGreg Roach                ->where('i_file', '=', $tree->id())
66bb03c9f0SGreg Roach                ->whereIn('i_id', $match[1])
67bb03c9f0SGreg Roach                ->get()
68*6b9cb339SGreg Roach                ->map(Registry::individualFactory()->mapper($tree));
69bb03c9f0SGreg Roach
70bb03c9f0SGreg Roach            return new Family($xref, $gedcom ?? '', $pending, $tree);
71bb03c9f0SGreg Roach        });
72bb03c9f0SGreg Roach    }
73bb03c9f0SGreg Roach
74bb03c9f0SGreg Roach    /**
75bb03c9f0SGreg Roach     * Create a Family object from a row in the database.
76bb03c9f0SGreg Roach     *
77bb03c9f0SGreg Roach     * @param Tree $tree
78bb03c9f0SGreg Roach     *
79bb03c9f0SGreg Roach     * @return Closure
80bb03c9f0SGreg Roach     */
81bb03c9f0SGreg Roach    public function mapper(Tree $tree): Closure
82bb03c9f0SGreg Roach    {
83bb03c9f0SGreg Roach        return function (stdClass $row) use ($tree): Family {
84bb03c9f0SGreg Roach            $family = $this->make($row->f_id, $tree, $row->f_gedcom);
85bb03c9f0SGreg Roach            assert($family instanceof Family);
86bb03c9f0SGreg Roach
87bb03c9f0SGreg Roach            return $family;
88bb03c9f0SGreg Roach        };
89bb03c9f0SGreg Roach    }
90bb03c9f0SGreg Roach
91bb03c9f0SGreg Roach    /**
92bb03c9f0SGreg Roach     * Create a Family object from raw GEDCOM data.
93bb03c9f0SGreg Roach     *
94bb03c9f0SGreg Roach     * @param string      $xref
95bb03c9f0SGreg Roach     * @param string      $gedcom  an empty string for new/pending records
96bb03c9f0SGreg Roach     * @param string|null $pending null for a record with no pending edits,
97bb03c9f0SGreg Roach     *                             empty string for records with pending deletions
98bb03c9f0SGreg Roach     * @param Tree        $tree
99bb03c9f0SGreg Roach     *
100bb03c9f0SGreg Roach     * @return Family
101bb03c9f0SGreg Roach     */
102bb03c9f0SGreg Roach    public function new(string $xref, string $gedcom, ?string $pending, Tree $tree): Family
103bb03c9f0SGreg Roach    {
104bb03c9f0SGreg Roach        return new Family($xref, $gedcom, $pending, $tree);
105bb03c9f0SGreg Roach    }
106bb03c9f0SGreg Roach
107bb03c9f0SGreg Roach    /**
108bb03c9f0SGreg Roach     * Fetch GEDCOM data from the database.
109bb03c9f0SGreg Roach     *
110bb03c9f0SGreg Roach     * @param string $xref
111bb03c9f0SGreg Roach     * @param Tree   $tree
112bb03c9f0SGreg Roach     *
113bb03c9f0SGreg Roach     * @return string|null
114bb03c9f0SGreg Roach     */
115bb03c9f0SGreg Roach    protected function gedcom(string $xref, Tree $tree): ?string
116bb03c9f0SGreg Roach    {
117bb03c9f0SGreg Roach        return DB::table('families')
118bb03c9f0SGreg Roach            ->where('f_id', '=', $xref)
119bb03c9f0SGreg Roach            ->where('f_file', '=', $tree->id())
120bb03c9f0SGreg Roach            ->value('f_gedcom');
121bb03c9f0SGreg Roach    }
122bb03c9f0SGreg Roach}
123