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