1<?php 2 3namespace dokuwiki\plugin\gallery\classes; 4 5use dokuwiki\Utf8\Sort; 6 7abstract class AbstractGallery 8{ 9 /** @var Image[] */ 10 protected $images = []; 11 /** @var Options */ 12 protected $options; 13 14 /** 15 * Initialize the Gallery 16 * 17 * @param mixed $src The source from where to get the images 18 * @param Options $options Gallery configuration 19 */ 20 public function __construct($src, Options $options) 21 { 22 $this->options = $options; 23 } 24 25 /** 26 * Simple heuristic if something is an image 27 * 28 * @param string $src 29 * @return bool 30 */ 31 public function hasImageExtension($src) 32 { 33 return (bool)preg_match(Image::IMG_REGEX, $src); 34 } 35 36 /** 37 * Get the images of this gallery 38 * 39 * The result will be sorted, reversed and limited according to the options 40 * 41 * @return Image[] 42 */ 43 public function getImages() 44 { 45 $images = $this->images; // create a copy of the array 46 47 switch ($this->options->sort) { 48 case Options::SORT_FILE: 49 usort($images, function ($a, $b) { 50 return Sort::strcmp($a->getFilename(), $b->getFilename()); 51 }); 52 break; 53 case Options::SORT_CTIME: 54 usort($images, function ($a, $b) { 55 return $a->getCreated() - $b->getCreated(); 56 }); 57 break; 58 case Options::SORT_MTIME: 59 usort($images, function ($a, $b) { 60 return $a->getModified() - $b->getModified(); 61 }); 62 break; 63 case Options::SORT_TITLE: 64 usort($images, function ($a, $b) { 65 return Sort::strcmp($a->getTitle(), $b->getTitle()); 66 }); 67 break; 68 case Options::SORT_RANDOM: 69 shuffle($images); 70 break; 71 } 72 if ($this->options->reverse) { 73 $images = array_reverse($images); 74 } 75 if ($this->options->offset) { 76 $images = array_slice($images, $this->options->offset); 77 } 78 if ($this->options->limit) { 79 $images = array_slice($images, 0, $this->options->limit); 80 } 81 82 return $images; 83 } 84} 85