Passed
Push — master ( 00b783...b7fb13 )
by Caen
03:02 queued 11s
created

Image::fromSource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace Hyde\Framework\Models;
4
5
use Hyde\Framework\Actions\FindsContentLengthForImageObject;
6
use Hyde\Framework\Hyde;
7
8
/**
9
 * Holds the information for an image.
10
 *
11
 * $schema = [
12
 *    'path'         => '?string',
13
 *    'uri'          => '?string',
14
 *    'description'  => '?string',
15
 *    'title'        => '?string',
16
 *    'copyright'    => '?string',
17
 *    'license'      => '?string',
18
 *    'licenseUrl'   => '?string',
19
 *    'author'       => '?string',
20
 *    'credit'       => '?string'
21
 * ];
22
 */
23
class Image
24
{
25
    /**
26
     * The image's path (if it is stored locally).
27
     * Example: _media/image.jpg.
28
     *
29
     * @var string|null
30
     */
31
    public ?string $path;
32
33
    /**
34
     * The image's URI (if stored externally).
35
     * Example: https://example.com/media/image.jpg.
36
     *
37
     * Will override the path property if both are set.
38
     *
39
     * @var string|null
40
     */
41
    public ?string $uri;
42
43
    /**
44
     * The image's description. (Used for alt text for screen readers.)
45
     * You should always set this to provide accessibility.
46
     * Example: "This is an image of a cat sitting in a basket.".
47
     *
48
     * @var string|null
49
     */
50
    public ?string $description;
51
52
    /**
53
     * The image's title. (Shows a tooltip on hover.)
54
     * Example: "My Cat Archer".
55
     *
56
     * @var string|null
57
     */
58
    public ?string $title;
59
60
    /**
61
     * The image's copyright.
62
     * Example: "Copyright (c) 2020 John Doe".
63
     *
64
     * @var string|null
65
     */
66
    public ?string $copyright;
67
68
    /**
69
     * The image's license name.
70
     * Example: "CC BY-NC-SA 4.0".
71
     *
72
     * @var string|null
73
     */
74
    public ?string $license;
75
76
    /**
77
     * The image's license URL.
78
     * Example: "https://creativecommons.org/licenses/by-nc-sa/4.0/".
79
     *
80
     * @var string|null
81
     */
82
    public ?string $licenseUrl;
83
84
    /**
85
     * The image's author.
86
     * Example: "John Doe".
87
     *
88
     * @var string|null
89
     */
90
    public ?string $author;
91
92
    /**
93
     * The image's source (for attribution).
94
     * Example: "https://unsplash.com/photos/example".
95
     *
96
     * @var string|null
97
     */
98
    public ?string $credit = null;
99
100
    public function __construct(array $data = [])
101
    {
102
        foreach ($data as $key => $value) {
103
            $this->{$key} = $value;
104
        }
105
    }
106
107
    /** Dynamically create an image based on string or front matter array */
108
    public static function make(string|array $data): static
109
    {
110
        if (is_string($data)) {
0 ignored issues
show
introduced by
The condition is_string($data) is always false.
Loading history...
111
            return static::fromSource($data);
112
        }
113
114
        return new static($data);
115
    }
116
117
    public static function fromSource(string $image): static
118
    {
119
        return str_starts_with($image, 'http')
120
            ? new static(['uri' => $image])
121
            : new static(['path' => $image]);
122
    }
123
124
    public function getSource(): ?string
125
    {
126
        return $this->uri ?? $this->path ?? null;
127
    }
128
129
    public function getLink(): string
130
    {
131
        return Hyde::image($this->getSource() ?? '');
132
    }
133
134
    public function getContentLength(): int
135
    {
136
        return (new FindsContentLengthForImageObject($this))->execute();
137
    }
138
139
    public function getImageAuthorAttributionString(): string|null
140
    {
141
        if (isset($this->author)) {
142
            if (isset($this->credit)) {
143
                return '<span itemprop="creator" itemscope="" itemtype="http://schema.org/Person"><a href="'.e($this->credit).'" rel="author noopener nofollow" itemprop="url"><span itemprop="name">'.e($this->author).'</span></a></span>';
144
            } else {
145
                return '<span itemprop="creator" itemscope="" itemtype="http://schema.org/Person"><span itemprop="name">'.e($this->author).'</span></span>';
146
            }
147
        }
148
149
        return null;
150
    }
151
152
    public function getCopyrightString(): string|null
153
    {
154
        if (isset($this->copyright)) {
155
            return '<span itemprop="copyrightNotice">'.e($this->copyright).'</span>';
156
        }
157
158
        return null;
159
    }
160
161
    public function getLicenseString(): string|null
162
    {
163
        if (isset($this->license) && isset($this->licenseUrl)) {
164
            return '<a href="'.e($this->licenseUrl).'" rel="license nofollow noopener" itemprop="license">'.e($this->license).'</a>';
165
        }
166
167
        if (isset($this->license)) {
168
            return '<span itemprop="license">'.e($this->license).'</span>';
169
        }
170
171
        return null;
172
    }
173
174
    public function getFluentAttribution(): string
175
    {
176
        $attribution = [];
177
178
        $getImageAuthorAttributionString = $this->getImageAuthorAttributionString();
179
        if ($getImageAuthorAttributionString !== null) {
180
            $attribution[] = 'Image by '.$getImageAuthorAttributionString;
181
        }
182
183
        $getCopyrightString = $this->getCopyrightString();
184
        if ($getCopyrightString !== null) {
185
            $attribution[] = $getCopyrightString;
186
        }
187
188
        $getLicenseString = $this->getLicenseString();
189
        if ($getLicenseString !== null) {
190
            $attribution[] = 'License '.$getLicenseString;
191
        }
192
193
        return implode('. ', $attribution);
194
    }
195
196
    /**
197
     * Used in resources\views\components\post\image.blade.php to add meta tags with itemprop attributes.
198
     *
199
     * @return array
200
     */
201
    public function getMetadataArray(): array
202
    {
203
        $metadata = [];
204
205
        if (isset($this->description)) {
206
            $metadata['text'] = $this->description;
207
        }
208
209
        if (isset($this->title)) {
210
            $metadata['name'] = $this->title;
211
        }
212
213
        $metadata['url'] = $this->getSource();
214
        $metadata['contentUrl'] = $this->getSource();
215
216
        return $metadata;
217
    }
218
}
219