forked from wbobeirne/Phlickr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhotoSorter.php
114 lines (103 loc) · 2.95 KB
/
PhotoSorter.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* @version $Id$
* @author Andrew Morton <[email protected]>
* @license http://opensource.org/licenses/lgpl-license.php
GNU Lesser General Public License, Version 2.1
* @package Phlickr
*/
/**
* Phlickr_Api includes the core classes.
*/
require_once dirname(__FILE__) . '/Api.php';
/**
* This class accepts IPhotoSortStrategy objects as parameters.
*/
require_once dirname(__FILE__) . '/Framework/IPhotoSortStrategy.php';
/**
* An object to sorts Phlickr_Photos using a given
* Phlickr_Framework_IPhotoComparator.
*
* @author Andrew Morton <[email protected]>
* @package Phlickr
* @since 0.2.3
* @see Phlickr_Framework_IPhotoComparator, Phlickr_Framework_IPhotoList
*/
class Phlickr_PhotoSorter {
/**
* Should the photo be sorted in reverse order?
*
* @var boolean
*/
private $_inReverse;
/**
* How should the photos be sorted?
*
* @var object Phlickr_Framework_IPhotoSortStrategy
*/
private $_sortStrategy;
/**
* Constructor.
*
* @param object Phlickr_Framework_IPhotoComparator $comparator Specifies
* how the sorting will be done.
* @param boolean $inReverse Should the photos be sorted in reverse order.
* @see isInReverse()
*/
function __construct(Phlickr_Framework_IPhotoSortStrategy $strategy,
$inReverse = false)
{
$this->_strategy = $strategy;
$this->_inReverse = (boolean) $inReverse;
}
/**
* Will the sort be done in reverse order?
*
* @return boolean
* @see __construct()
*/
function isInReverse() {
return $this->_inReverse;
}
/**
* Sort an array of photos using the comparator.
*
* Keep in mind a couple of other classes that implement
* Phlickr_Framework_IPhotoList:
* - Phlickr_PhotosetPhotoList all the photos in a photoset.
* - Phlickr_PhotoListIterator will pull all pages from a photo list.
*
* @param object Phlickr_Framework_IPhotoList $photoslist
* @return array Phlickr_Photo Sorted array of photos
*/
function sort(Phlickr_Framework_IPhotoList $photolist) {
foreach ($photolist->getPhotos() as $photo) {
$id = $photo->getId();
$keys[$id] = $this->_strategy->stringFromPhoto($photo);
$photos[$id] = $photo;
}
if ($this->isInReverse()) {
arsort($keys);
} else {
asort($keys);
}
$ret = array();
foreach($keys as $id => $key) {
$ret[] = $photos[$id];
}
return $ret;
}
/**
* Return an array of ids from an array of Phlickr_Photos.
*
* @param array object Phlickr_Photo
* @return array of string ids
*/
static function idsFromPhotos($photos) {
$ids = array();
foreach ($photos as $photo) {
$ids[] = $photo->getId();
}
return $ids;
}
}