forked from DesignPatternsPHP/DesignPatternsPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectCollection.php
52 lines (44 loc) · 931 Bytes
/
ObjectCollection.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
<?php
namespace DesignPatterns\Behavioral\Strategy;
/**
* Class ObjectCollection
*/
class ObjectCollection
{
/**
* @var array
*/
private $elements;
/**
* @var ComparatorInterface
*/
private $comparator;
/**
* @param array $elements
*/
public function __construct(array $elements = array())
{
$this->elements = $elements;
}
/**
* @return array
*/
public function sort()
{
if (!$this->comparator) {
throw new \LogicException("Comparator is not set");
}
$callback = array($this->comparator, 'compare');
uasort($this->elements, $callback);
return $this->elements;
}
/**
* @param ComparatorInterface $comparator
*
* @return void
*/
public function setComparator(ComparatorInterface $comparator)
{
$this->comparator = $comparator;
}
}