xref: /webtrees/app/Factories/MediaFactory.php (revision bb03c9f048b83092098d5e46c2ab323ae7e2b314)
1*bb03c9f0SGreg Roach<?php
2*bb03c9f0SGreg Roach
3*bb03c9f0SGreg Roach/**
4*bb03c9f0SGreg Roach * webtrees: online genealogy
5*bb03c9f0SGreg Roach * Copyright (C) 2019 webtrees development team
6*bb03c9f0SGreg Roach * This program is free software: you can redistribute it and/or modify
7*bb03c9f0SGreg Roach * it under the terms of the GNU General Public License as published by
8*bb03c9f0SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*bb03c9f0SGreg Roach * (at your option) any later version.
10*bb03c9f0SGreg Roach * This program is distributed in the hope that it will be useful,
11*bb03c9f0SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*bb03c9f0SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*bb03c9f0SGreg Roach * GNU General Public License for more details.
14*bb03c9f0SGreg Roach * You should have received a copy of the GNU General Public License
15*bb03c9f0SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*bb03c9f0SGreg Roach */
17*bb03c9f0SGreg Roach
18*bb03c9f0SGreg Roachdeclare(strict_types=1);
19*bb03c9f0SGreg Roach
20*bb03c9f0SGreg Roachnamespace Fisharebest\Webtrees\Factories;
21*bb03c9f0SGreg Roach
22*bb03c9f0SGreg Roachuse Closure;
23*bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Contracts\MediaFactoryInterface;
24*bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Media;
25*bb03c9f0SGreg Roachuse Fisharebest\Webtrees\Tree;
26*bb03c9f0SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
27*bb03c9f0SGreg Roachuse stdClass;
28*bb03c9f0SGreg Roach
29*bb03c9f0SGreg Roachuse function assert;
30*bb03c9f0SGreg Roachuse function preg_match;
31*bb03c9f0SGreg Roach
32*bb03c9f0SGreg Roach/**
33*bb03c9f0SGreg Roach * Make a Media object.
34*bb03c9f0SGreg Roach */
35*bb03c9f0SGreg Roachclass MediaFactory extends AbstractGedcomRecordFactory implements MediaFactoryInterface
36*bb03c9f0SGreg Roach{
37*bb03c9f0SGreg Roach    private const TYPE_CHECK_REGEX = '/^0 @[^@]+@ ' . Media::RECORD_TYPE . '/';
38*bb03c9f0SGreg Roach
39*bb03c9f0SGreg Roach    /**
40*bb03c9f0SGreg Roach     * Create a individual.
41*bb03c9f0SGreg Roach     *
42*bb03c9f0SGreg Roach     * @param string      $xref
43*bb03c9f0SGreg Roach     * @param Tree        $tree
44*bb03c9f0SGreg Roach     * @param string|null $gedcom
45*bb03c9f0SGreg Roach     *
46*bb03c9f0SGreg Roach     * @return Media|null
47*bb03c9f0SGreg Roach     */
48*bb03c9f0SGreg Roach    public function make(string $xref, Tree $tree, string $gedcom = null): ?Media
49*bb03c9f0SGreg Roach    {
50*bb03c9f0SGreg Roach        return $this->cache->remember(__CLASS__ . $xref . '@' . $tree->id(), function () use ($xref, $tree, $gedcom) {
51*bb03c9f0SGreg Roach            $gedcom  = $gedcom ?? $this->gedcom($xref, $tree);
52*bb03c9f0SGreg Roach            $pending = $this->pendingChanges($tree)->get($xref);
53*bb03c9f0SGreg Roach
54*bb03c9f0SGreg Roach            if ($gedcom === null && ($pending === null || !preg_match(self::TYPE_CHECK_REGEX, $pending))) {
55*bb03c9f0SGreg Roach                return null;
56*bb03c9f0SGreg Roach            }
57*bb03c9f0SGreg Roach
58*bb03c9f0SGreg Roach            $xref = $this->extractXref($gedcom ?? $pending, $xref);
59*bb03c9f0SGreg Roach
60*bb03c9f0SGreg Roach            return new Media($xref, $gedcom ?? '', $pending, $tree);
61*bb03c9f0SGreg Roach        });
62*bb03c9f0SGreg Roach    }
63*bb03c9f0SGreg Roach
64*bb03c9f0SGreg Roach    /**
65*bb03c9f0SGreg Roach     * Create a media object from a row in the database.
66*bb03c9f0SGreg Roach     *
67*bb03c9f0SGreg Roach     * @param Tree $tree
68*bb03c9f0SGreg Roach     *
69*bb03c9f0SGreg Roach     * @return Closure
70*bb03c9f0SGreg Roach     */
71*bb03c9f0SGreg Roach    public function mapper(Tree $tree): Closure
72*bb03c9f0SGreg Roach    {
73*bb03c9f0SGreg Roach        return function (stdClass $row) use ($tree): Media {
74*bb03c9f0SGreg Roach            $media = $this->make($row->m_id, $tree, $row->m_gedcom);
75*bb03c9f0SGreg Roach            assert($media instanceof Media);
76*bb03c9f0SGreg Roach
77*bb03c9f0SGreg Roach            return $media;
78*bb03c9f0SGreg Roach        };
79*bb03c9f0SGreg Roach    }
80*bb03c9f0SGreg Roach
81*bb03c9f0SGreg Roach    /**
82*bb03c9f0SGreg Roach     * Create a media object from raw GEDCOM data.
83*bb03c9f0SGreg Roach     *
84*bb03c9f0SGreg Roach     * @param string      $xref
85*bb03c9f0SGreg Roach     * @param string      $gedcom  an empty string for new/pending records
86*bb03c9f0SGreg Roach     * @param string|null $pending null for a record with no pending edits,
87*bb03c9f0SGreg Roach     *                             empty string for records with pending deletions
88*bb03c9f0SGreg Roach     * @param Tree        $tree
89*bb03c9f0SGreg Roach     *
90*bb03c9f0SGreg Roach     * @return Media
91*bb03c9f0SGreg Roach     */
92*bb03c9f0SGreg Roach    public function new(string $xref, string $gedcom, ?string $pending, Tree $tree): Media
93*bb03c9f0SGreg Roach    {
94*bb03c9f0SGreg Roach        return new Media($xref, $gedcom, $pending, $tree);
95*bb03c9f0SGreg Roach    }
96*bb03c9f0SGreg Roach
97*bb03c9f0SGreg Roach    /**
98*bb03c9f0SGreg Roach     * Fetch GEDCOM data from the database.
99*bb03c9f0SGreg Roach     *
100*bb03c9f0SGreg Roach     * @param string $xref
101*bb03c9f0SGreg Roach     * @param Tree   $tree
102*bb03c9f0SGreg Roach     *
103*bb03c9f0SGreg Roach     * @return string|null
104*bb03c9f0SGreg Roach     */
105*bb03c9f0SGreg Roach    protected function gedcom(string $xref, Tree $tree): ?string
106*bb03c9f0SGreg Roach    {
107*bb03c9f0SGreg Roach        return DB::table('media')
108*bb03c9f0SGreg Roach            ->where('m_id', '=', $xref)
109*bb03c9f0SGreg Roach            ->where('m_file', '=', $tree->id())
110*bb03c9f0SGreg Roach            ->value('m_gedcom');
111*bb03c9f0SGreg Roach    }
112*bb03c9f0SGreg Roach}
113