1bb03c9f0SGreg Roach<?php 2bb03c9f0SGreg Roach 3bb03c9f0SGreg Roach/** 4bb03c9f0SGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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 1589f7189bSGreg Roach * along with this program. If not, see <https://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\HeaderFactoryInterface; 24bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Header; 256b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry; 26bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Tree; 27bb03c9f0SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 28bb03c9f0SGreg Roachuse stdClass; 29bb03c9f0SGreg Roach 30bb03c9f0SGreg Roachuse function assert; 31bb03c9f0SGreg Roachuse function preg_match; 32bb03c9f0SGreg Roach 33bb03c9f0SGreg Roach/** 34bb03c9f0SGreg Roach * Make a header object. 35bb03c9f0SGreg Roach */ 36bb03c9f0SGreg Roachclass HeaderFactory extends AbstractGedcomRecordFactory implements HeaderFactoryInterface 37bb03c9f0SGreg Roach{ 38bb03c9f0SGreg Roach private const TYPE_CHECK_REGEX = '/^0 ' . Header::RECORD_TYPE . '/'; 39bb03c9f0SGreg Roach 40bb03c9f0SGreg Roach /** 41bb03c9f0SGreg Roach * Create a header. 42bb03c9f0SGreg Roach * 43bb03c9f0SGreg Roach * @param string $xref 44bb03c9f0SGreg Roach * @param Tree $tree 45bb03c9f0SGreg Roach * @param string|null $gedcom 46bb03c9f0SGreg Roach * 47bb03c9f0SGreg Roach * @return Header|null 48bb03c9f0SGreg Roach */ 49bb03c9f0SGreg Roach public function make(string $xref, Tree $tree, string $gedcom = null): ?Header 50bb03c9f0SGreg Roach { 516b9cb339SGreg Roach return Registry::cache()->array()->remember(__CLASS__ . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) { 52bb03c9f0SGreg Roach $gedcom = $gedcom ?? $this->gedcom($xref, $tree); 53bb03c9f0SGreg Roach $pending = $this->pendingChanges($tree)->get($xref); 54bb03c9f0SGreg Roach 55bb03c9f0SGreg Roach if ($gedcom === null && ($pending === null || !preg_match(self::TYPE_CHECK_REGEX, $pending))) { 56bb03c9f0SGreg Roach return null; 57bb03c9f0SGreg Roach } 58bb03c9f0SGreg Roach 59bb03c9f0SGreg Roach $xref = $this->extractXref($gedcom ?? $pending, $xref); 60bb03c9f0SGreg Roach 61*b27dac11SGreg Roach return $this->new($xref, $gedcom ?? '', $pending, $tree); 62bb03c9f0SGreg Roach }); 63bb03c9f0SGreg Roach } 64bb03c9f0SGreg Roach 65bb03c9f0SGreg Roach /** 66bb03c9f0SGreg Roach * Create a header from a row in the database. 67bb03c9f0SGreg Roach * 68bb03c9f0SGreg Roach * @param Tree $tree 69bb03c9f0SGreg Roach * 70bb03c9f0SGreg Roach * @return Closure 71bb03c9f0SGreg Roach */ 72bb03c9f0SGreg Roach public function mapper(Tree $tree): Closure 73bb03c9f0SGreg Roach { 74bb03c9f0SGreg Roach return function (stdClass $row) use ($tree): Header { 75bb03c9f0SGreg Roach $submitter = $this->make($row->o_id, $tree, $row->o_gedcom); 76bb03c9f0SGreg Roach assert($submitter instanceof Header); 77bb03c9f0SGreg Roach 78bb03c9f0SGreg Roach return $submitter; 79bb03c9f0SGreg Roach }; 80bb03c9f0SGreg Roach } 81bb03c9f0SGreg Roach 82bb03c9f0SGreg Roach /** 83bb03c9f0SGreg Roach * Create a header from raw GEDCOM data. 84bb03c9f0SGreg Roach * 85bb03c9f0SGreg Roach * @param string $xref 86bb03c9f0SGreg Roach * @param string $gedcom an empty string for new/pending records 87bb03c9f0SGreg Roach * @param string|null $pending null for a record with no pending edits, 88bb03c9f0SGreg Roach * empty string for records with pending deletions 89bb03c9f0SGreg Roach * @param Tree $tree 90bb03c9f0SGreg Roach * 91bb03c9f0SGreg Roach * @return Header 92bb03c9f0SGreg Roach */ 93bb03c9f0SGreg Roach public function new(string $xref, string $gedcom, ?string $pending, Tree $tree): Header 94bb03c9f0SGreg Roach { 95bb03c9f0SGreg Roach return new Header($xref, $gedcom, $pending, $tree); 96bb03c9f0SGreg Roach } 97bb03c9f0SGreg Roach 98bb03c9f0SGreg Roach /** 99bb03c9f0SGreg Roach * Fetch GEDCOM data from the database. 100bb03c9f0SGreg Roach * 101bb03c9f0SGreg Roach * @param string $xref 102bb03c9f0SGreg Roach * @param Tree $tree 103bb03c9f0SGreg Roach * 104bb03c9f0SGreg Roach * @return string|null 105bb03c9f0SGreg Roach */ 106bb03c9f0SGreg Roach protected function gedcom(string $xref, Tree $tree): ?string 107bb03c9f0SGreg Roach { 108bb03c9f0SGreg Roach return DB::table('other') 109bb03c9f0SGreg Roach ->where('o_id', '=', $xref) 110bb03c9f0SGreg Roach ->where('o_file', '=', $tree->id()) 111bb03c9f0SGreg Roach ->where('o_type', '=', Header::RECORD_TYPE) 112bb03c9f0SGreg Roach ->value('o_gedcom'); 113bb03c9f0SGreg Roach } 114bb03c9f0SGreg Roach} 115