1<?php
2/**
3 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved.
4 *
5 * This source code is licensed under the GPL license found in the
6 * COPYING  file in the root directory of this source tree.
7 *
8 * @license  GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html)
9 * @author   ComboStrap <support@combostrap.com>
10 *
11 */
12
13namespace ComboStrap;
14
15
16use ComboStrap\Web\Url;
17
18/**
19 * Class InternalMedia
20 * Represent a HTML markup link
21 *
22 *
23 * @package ComboStrap
24 *
25 *
26 *
27 * This is a link to a media (pdf, image, ...).
28 * (ie img, svg, a)
29 */
30abstract class MediaLink
31{
32
33
34    const CANONICAL = "image";
35
36
37    /**
38     * @deprecated 2021-06-12
39     */
40    const LINK_PATTERN = "{{\s*([^|\s]*)\s*\|?.*}}";
41    protected MediaMarkup $mediaMarkup;
42
43
44    public function __construct(MediaMarkup $mediaMarkup)
45    {
46        $this->mediaMarkup = $mediaMarkup;
47    }
48
49
50    /**
51     * @param MediaMarkup $mediaMarkup
52     * @return RasterImageLink|SvgImageLink|ThirdMediaLink|MediaLink
53     * @throws ExceptionBadArgument
54     * @throws ExceptionBadSyntax
55     * @throws ExceptionNotExists
56     * @throws ExceptionNotFound
57     */
58    public static function createFromMediaMarkup(MediaMarkup $mediaMarkup)
59    {
60
61        /**
62         * Processing
63         */
64        $mime = $mediaMarkup->getFetcher()->getMime();
65        switch ($mime->toString()) {
66            case Mime::SVG:
67                return new SvgImageLink($mediaMarkup);
68            default:
69                if (!$mime->isImage()) {
70                    return new ThirdMediaLink($mediaMarkup);
71                } else {
72                    return new RasterImageLink($mediaMarkup);
73                }
74        }
75
76
77    }
78
79
80    /**
81     * @return string - the HTML of the image
82     */
83    public abstract function renderMediaTag(): string;
84
85
86    public function getFetchUrl(): Url
87    {
88        return $this->mediaMarkup->getFetchUrl();
89    }
90
91
92}
93