xref: /webtrees/app/Factories/AbstractGedcomRecordFactory.php (revision 0e2e57bd5c8dad261d043770bf646c2285b4eaca)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Factories;
21
22use Fisharebest\Webtrees\Cache;
23use Fisharebest\Webtrees\Gedcom;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\Tree;
26use Illuminate\Database\Capsule\Manager as DB;
27use Illuminate\Support\Collection;
28use stdClass;
29
30/**
31 * Make a GedcomRecord object.
32 */
33abstract class AbstractGedcomRecordFactory
34{
35    /** @var Cache  */
36    protected $cache;
37
38    /**
39     * GedcomRecordFactory constructor.
40     *
41     * @deprecated since 2.0.8 - will be removed in 2.1.0
42     */
43    public function __construct()
44    {
45        $this->cache = Registry::cache()->array();
46    }
47
48    /**
49     * @param Tree $tree
50     *
51     * @return Collection<stdClass>
52     */
53    protected function pendingChanges(Tree $tree): Collection
54    {
55        // Caution - this cache can be overwritten by GedcomExportService
56        return Registry::cache()->array()->remember(__CLASS__ . $tree->id(), static function () use ($tree): Collection {
57            return DB::table('change')
58                ->where('gedcom_id', '=', $tree->id())
59                ->where('status', '=', 'pending')
60                ->orderBy('change_id')
61                ->pluck('new_gedcom', 'xref');
62        });
63    }
64
65    /**
66     * We may have searched for X123, but found the record for x123.
67     *
68     * @param string $gedcom
69     * @param string $xref
70     *
71     * @return mixed|string
72     */
73    protected function extractXref(string $gedcom, string $xref)
74    {
75        if (preg_match('/^0 @(' . Gedcom::REGEX_XREF . ')@/', $gedcom, $match)) {
76            return $match[1];
77        }
78
79        return $xref;
80    }
81}
82