Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
78.57% |
11 / 14 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ImageUrlTrait | |
78.57% |
11 / 14 |
|
33.33% |
1 / 3 |
6.35 | |
0.00% |
0 / 1 |
| _getImageUrl | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| __get | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
3.21 | |||
| getImageUrlBySize | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace App\Model\Entity; |
| 5 | |
| 6 | use App\Utility\SettingsManager; |
| 7 | |
| 8 | trait ImageUrlTrait |
| 9 | { |
| 10 | /** |
| 11 | * Retrieves the URL for an image at original size by removing the 'webroot/' prefix from the directory path. |
| 12 | * |
| 13 | * This method constructs the image URL by concatenating the directory and image name, |
| 14 | * and then removes the 'webroot/' part from the path to generate a relative URL. |
| 15 | * |
| 16 | * @return string The relative URL of the image. |
| 17 | */ |
| 18 | protected function _getImageUrl(): string |
| 19 | { |
| 20 | $url = str_replace('webroot/', '', $this->dir . $this->image); |
| 21 | // Ensure URL starts with a slash for absolute path |
| 22 | return '/' . ltrim($url, '/'); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Magic method to dynamically retrieve image URLs based on image size names. |
| 27 | * |
| 28 | * This method checks if the requested attribute name matches the pattern for image URLs |
| 29 | * (e.g., "smallImageUrl") and returns the corresponding image URL if available. |
| 30 | * If the attribute does not match the pattern, it delegates to the parent::__get() method. |
| 31 | * |
| 32 | * @param string $attribute The name of the attribute being accessed. |
| 33 | * @return mixed The URL of the image if the attribute matches the pattern, otherwise the result of parent::__get(). |
| 34 | */ |
| 35 | public function &__get(string $attribute): mixed |
| 36 | { |
| 37 | if (preg_match('/^(.+)ImageUrl$/', $attribute, $matches)) { |
| 38 | $size = lcfirst($matches[1]); |
| 39 | $imageSizes = SettingsManager::read('ImageSizes'); |
| 40 | if (isset($imageSizes[$size])) { |
| 41 | $url = $this->getImageUrlBySize($size); |
| 42 | |
| 43 | return $url; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return parent::__get($attribute); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Retrieves the URL for an image of a specified size. |
| 52 | * |
| 53 | * This method constructs the URL for an image based on the provided size by |
| 54 | * removing the 'webroot/' prefix and appending the directory, size, and image name. |
| 55 | * |
| 56 | * @param string $size The size of the image (e.g., 'thumbnail', 'medium'). |
| 57 | * @return string The constructed URL for the image. |
| 58 | */ |
| 59 | public function getImageUrlBySize(string $size): string |
| 60 | { |
| 61 | $imageSizes = SettingsManager::read('ImageSizes'); |
| 62 | |
| 63 | // Use the width value as directory name (e.g., 100, 300, 600) |
| 64 | if (isset($imageSizes[$size])) { |
| 65 | $url = str_replace('webroot/', '', $this->dir . $imageSizes[$size] . DS . $this->image); |
| 66 | } else { |
| 67 | // Fallback to original image if size not found |
| 68 | $url = str_replace('webroot/', '', $this->dir . $this->image); |
| 69 | } |
| 70 | |
| 71 | // Ensure URL starts with a slash for absolute path |
| 72 | return '/' . ltrim($url, '/'); |
| 73 | } |
| 74 | } |