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