Completed
Push — master ( 8673e6...74a95f )
by Greg
06:24
created

AbstractGedcomRecordFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Factories;
21
22
use Fisharebest\Webtrees\Cache;
23
use Fisharebest\Webtrees\Factory;
24
use Fisharebest\Webtrees\Gedcom;
25
use Fisharebest\Webtrees\Tree;
26
use Illuminate\Database\Capsule\Manager as DB;
27
use Illuminate\Support\Collection;
28
use stdClass;
29
30
use function trigger_error;
31
32
use const E_USER_DEPRECATED;
33
34
/**
35
 * Make a GedcomRecord object.
36
 */
37
abstract class AbstractGedcomRecordFactory
38
{
39
    /** @var Cache  */
40
    protected $cache;
41
42
    /**
43
     * GedcomRecordFactory constructor.
44
     *
45
     * @deprecated since 2.0.8 - will be removed in 2.1.0
46
     */
47
    public function __construct()
48
    {
49
        $this->cache = Factory::cache()->array();
50
    }
51
52
    /**
53
     * @param Tree $tree
54
     *
55
     * @return Collection<stdClass>
56
     */
57
    protected function pendingChanges(Tree $tree): Collection
58
    {
59
        // Caution - this cache can be overwritten by GedcomExportService
60
        return Factory::cache()->array()->remember(__CLASS__ . $tree->id(), static function () use ($tree): Collection {
61
            return DB::table('change')
62
                ->where('gedcom_id', '=', $tree->id())
63
                ->where('status', '=', 'pending')
64
                ->orderBy('change_id')
65
                ->pluck('new_gedcom', 'xref');
66
        });
67
    }
68
69
    /**
70
     * We may have searched for X123, but found the record for x123.
71
     *
72
     * @param string $gedcom
73
     * @param string $xref
74
     *
75
     * @return mixed|string
76
     */
77
    protected function extractXref(string $gedcom, string $xref)
78
    {
79
        if (preg_match('/^0 @(' . Gedcom::REGEX_XREF . ')@/', $gedcom, $match)) {
80
            return $match[1];
81
        }
82
83
        return $xref;
84
    }
85
}
86