1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 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\MediaFactoryInterface; 24use Fisharebest\Webtrees\Media; 25use Fisharebest\Webtrees\Registry; 26use Fisharebest\Webtrees\Tree; 27use Illuminate\Database\Capsule\Manager as DB; 28 29use function preg_match; 30 31/** 32 * Make a Media object. 33 */ 34class MediaFactory extends AbstractGedcomRecordFactory implements MediaFactoryInterface 35{ 36 private const TYPE_CHECK_REGEX = '/^0 @[^@]+@ ' . Media::RECORD_TYPE . '/'; 37 38 /** 39 * Create a individual. 40 * 41 * @param string $xref 42 * @param Tree $tree 43 * @param string|null $gedcom 44 * 45 * @return Media|null 46 */ 47 public function make(string $xref, Tree $tree, string $gedcom = null): ?Media 48 { 49 return Registry::cache()->array()->remember(self::class . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) { 50 $gedcom = $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 57 $xref = $this->extractXref($gedcom ?? $pending, $xref); 58 59 return $this->new($xref, $gedcom ?? '', $pending, $tree); 60 }); 61 } 62 63 /** 64 * Create a media object from a row in the database. 65 * 66 * @param Tree $tree 67 * 68 * @return Closure 69 */ 70 public function mapper(Tree $tree): Closure 71 { 72 return fn (object $row): Media => $this->make($row->m_id, $tree, $row->m_gedcom); 73 } 74 75 /** 76 * Create a media object from raw GEDCOM data. 77 * 78 * @param string $xref 79 * @param string $gedcom an empty string for new/pending records 80 * @param string|null $pending null for a record with no pending edits, 81 * empty string for records with pending deletions 82 * @param Tree $tree 83 * 84 * @return Media 85 */ 86 public function new(string $xref, string $gedcom, ?string $pending, Tree $tree): Media 87 { 88 return new Media($xref, $gedcom, $pending, $tree); 89 } 90 91 /** 92 * Fetch GEDCOM data from the database. 93 * 94 * @param string $xref 95 * @param Tree $tree 96 * 97 * @return string|null 98 */ 99 protected function gedcom(string $xref, Tree $tree): ?string 100 { 101 return DB::table('media') 102 ->where('m_id', '=', $xref) 103 ->where('m_file', '=', $tree->id()) 104 ->value('m_gedcom'); 105 } 106} 107