xref: /webtrees/app/Factories/SharedNoteFactory.php (revision 9991924fa6a0aa95eb3163fcd21c55460086f641)
1701f5d18SGreg Roach<?php
2701f5d18SGreg Roach
3701f5d18SGreg Roach/**
4701f5d18SGreg Roach * webtrees: online genealogy
5701f5d18SGreg Roach * Copyright (C) 2022 webtrees development team
6701f5d18SGreg Roach * This program is free software: you can redistribute it and/or modify
7701f5d18SGreg Roach * it under the terms of the GNU General Public License as published by
8701f5d18SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9701f5d18SGreg Roach * (at your option) any later version.
10701f5d18SGreg Roach * This program is distributed in the hope that it will be useful,
11701f5d18SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12701f5d18SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13701f5d18SGreg Roach * GNU General Public License for more details.
14701f5d18SGreg Roach * You should have received a copy of the GNU General Public License
15701f5d18SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16701f5d18SGreg Roach */
17701f5d18SGreg Roach
18701f5d18SGreg Roachdeclare(strict_types=1);
19701f5d18SGreg Roach
20701f5d18SGreg Roachnamespace Fisharebest\Webtrees\Factories;
21701f5d18SGreg Roach
22701f5d18SGreg Roachuse Closure;
23701f5d18SGreg Roachuse Fisharebest\Webtrees\Contracts\SharedNoteFactoryInterface;
24701f5d18SGreg Roachuse Fisharebest\Webtrees\Registry;
25f0c88a96SGreg Roachuse Fisharebest\Webtrees\SharedNote;
26701f5d18SGreg Roachuse Fisharebest\Webtrees\Tree;
27701f5d18SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
28701f5d18SGreg Roach
29701f5d18SGreg Roachuse function preg_match;
30701f5d18SGreg Roach
31701f5d18SGreg Roach/**
32701f5d18SGreg Roach * Make a SharedNote object.
33701f5d18SGreg Roach */
34701f5d18SGreg Roachclass SharedNoteFactory extends AbstractGedcomRecordFactory implements SharedNoteFactoryInterface
35701f5d18SGreg Roach{
36701f5d18SGreg Roach    private const TYPE_CHECK_REGEX = '/^0 @[^@]+@ ' . SharedNote::RECORD_TYPE . '/';
37701f5d18SGreg Roach
38701f5d18SGreg Roach    /**
39701f5d18SGreg Roach     * Create a note.
40701f5d18SGreg Roach     *
41701f5d18SGreg Roach     * @param string      $xref
42701f5d18SGreg Roach     * @param Tree        $tree
43701f5d18SGreg Roach     * @param string|null $gedcom
44701f5d18SGreg Roach     *
45701f5d18SGreg Roach     * @return SharedNote|null
46701f5d18SGreg Roach     */
47701f5d18SGreg Roach    public function make(string $xref, Tree $tree, string $gedcom = null): ?SharedNote
48701f5d18SGreg Roach    {
49*9991924fSGreg Roach        return Registry::cache()->array()->remember(self::class . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) {
50701f5d18SGreg Roach            $gedcom  = $gedcom ?? $this->gedcom($xref, $tree);
51701f5d18SGreg Roach            $pending = $this->pendingChanges($tree)->get($xref);
52701f5d18SGreg Roach
53701f5d18SGreg Roach            if ($gedcom === null && ($pending === null || !preg_match(self::TYPE_CHECK_REGEX, $pending))) {
54701f5d18SGreg Roach                return null;
55701f5d18SGreg Roach            }
56701f5d18SGreg Roach
57701f5d18SGreg Roach            $xref = $this->extractXref($gedcom ?? $pending, $xref);
58701f5d18SGreg Roach
59701f5d18SGreg Roach            return new SharedNote($xref, $gedcom ?? '', $pending, $tree);
60701f5d18SGreg Roach        });
61701f5d18SGreg Roach    }
62701f5d18SGreg Roach
63701f5d18SGreg Roach    /**
64701f5d18SGreg Roach     * Create a note from a row in the database.
65701f5d18SGreg Roach     *
66701f5d18SGreg Roach     * @param Tree $tree
67701f5d18SGreg Roach     *
68701f5d18SGreg Roach     * @return Closure
69701f5d18SGreg Roach     */
70701f5d18SGreg Roach    public function mapper(Tree $tree): Closure
71701f5d18SGreg Roach    {
72743491b8SGreg Roach        return fn (object $row): SharedNote => $this->make($row->o_id, $tree, $row->o_gedcom);
73701f5d18SGreg Roach    }
74701f5d18SGreg Roach
75701f5d18SGreg Roach    /**
76701f5d18SGreg Roach     * Create a note from raw GEDCOM data.
77701f5d18SGreg Roach     *
78701f5d18SGreg Roach     * @param string      $xref
79701f5d18SGreg Roach     * @param string      $gedcom  an empty string for new/pending records
80701f5d18SGreg Roach     * @param string|null $pending null for a record with no pending edits,
81701f5d18SGreg Roach     *                             empty string for records with pending deletions
82701f5d18SGreg Roach     * @param Tree        $tree
83701f5d18SGreg Roach     *
84701f5d18SGreg Roach     * @return SharedNote
85701f5d18SGreg Roach     */
86701f5d18SGreg Roach    public function new(string $xref, string $gedcom, ?string $pending, Tree $tree): SharedNote
87701f5d18SGreg Roach    {
88701f5d18SGreg Roach        return new SharedNote($xref, $gedcom, $pending, $tree);
89701f5d18SGreg Roach    }
90701f5d18SGreg Roach
91701f5d18SGreg Roach    /**
92701f5d18SGreg Roach     * Fetch GEDCOM data from the database.
93701f5d18SGreg Roach     *
94701f5d18SGreg Roach     * @param string $xref
95701f5d18SGreg Roach     * @param Tree   $tree
96701f5d18SGreg Roach     *
97701f5d18SGreg Roach     * @return string|null
98701f5d18SGreg Roach     */
99701f5d18SGreg Roach    protected function gedcom(string $xref, Tree $tree): ?string
100701f5d18SGreg Roach    {
101701f5d18SGreg Roach        return DB::table('other')
102701f5d18SGreg Roach            ->where('o_id', '=', $xref)
103701f5d18SGreg Roach            ->where('o_file', '=', $tree->id())
104701f5d18SGreg Roach            ->where('o_type', '=', SharedNote::RECORD_TYPE)
105701f5d18SGreg Roach            ->value('o_gedcom');
106701f5d18SGreg Roach    }
107701f5d18SGreg Roach}
108