|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
Loader::load('utility', 'Content'); |
|
4
|
|
|
|
|
5
|
|
|
final class FixPhotoContent extends Content |
|
|
|
|
|
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
private static $PHOTO_PLACEHOLDER_MATCH = '@{{photo="(.*)"}}@'; |
|
9
|
|
|
private static $ERROR_CONTENT = '<div class="photo-holder"><p class="photo-caption">Image (%s) could not be found!</p></div>'; |
|
|
|
|
|
|
10
|
|
|
private static $PHOTO_CONTENT = '<div class="photo-holder"><img src="%sphoto/%s/%s-size-%s.jpg" height="%d" width="%d" alt="%s" /><p class="photo-caption">%s</p></div>'; |
|
11
|
|
|
|
|
12
|
|
|
protected function execute($is_absolute = false, $size = 'medium') |
|
13
|
|
|
{ |
|
14
|
|
|
preg_match_all(self::$PHOTO_PLACEHOLDER_MATCH, $this->content, $matches); |
|
15
|
|
|
foreach($matches[1] as $key => $match) |
|
16
|
|
|
{ |
|
17
|
|
|
$photo_content = $this->get_photo_content($match, $size, $is_absolute); |
|
18
|
|
|
$this->content = str_replace($matches[0][$key], $photo_content, $this->content); |
|
19
|
|
|
} |
|
20
|
|
|
return; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
private function get_file_path($category, $photo, $size, $extension) |
|
24
|
|
|
{ |
|
25
|
|
|
$path = "{$category}/{$photo}-size-{$size}.{$extension}"; |
|
26
|
|
|
return Loader::getImagePath('photo', $path); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
View Code Duplication |
private function get_photo_content($string, $size, $is_absolute) |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
list($category, $file_name) = explode('/', $string); |
|
32
|
|
|
list($photo, $extension) = explode('.', $file_name); |
|
33
|
|
|
|
|
34
|
|
|
$file_path = $this->get_file_path($category, $photo, $size, $extension); |
|
35
|
|
|
|
|
36
|
|
|
$file_size = getimagesize($file_path); |
|
37
|
|
|
|
|
38
|
|
|
Loader::load('collector', 'image/PhotoCollector'); |
|
39
|
|
|
$photo_result = PhotoCollector::fetchRow($category, $photo); |
|
40
|
|
|
if($photo_result == false) |
|
41
|
|
|
return ''; |
|
42
|
|
|
|
|
43
|
|
|
$height = $file_size[1]; |
|
44
|
|
|
$width = $file_size[0]; |
|
45
|
|
|
$description = $photo_result->description; |
|
46
|
|
|
|
|
47
|
|
|
$domain = '/'; |
|
48
|
|
|
if($is_absolute) |
|
49
|
|
|
$domain = Loader::getRootUrl('blog'); |
|
50
|
|
|
|
|
51
|
|
|
return sprintf(self::$PHOTO_CONTENT, $domain, $category, $photo, $size, $height, $width, $description, $description); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.