forked from DesignPatternsPHP/DesignPatternsPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectCollection.php
More file actions
43 lines (35 loc) · 799 Bytes
/
ObjectCollection.php
File metadata and controls
43 lines (35 loc) · 799 Bytes
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
<?php
namespace DesignPatterns\Behavioral\Strategy;
class ObjectCollection
{
/**
* @var array
*/
private $elements;
/**
* @var ComparatorInterface
*/
private $comparator;
/**
* @param array $elements
*/
public function __construct(array $elements = [])
{
$this->elements = $elements;
}
public function sort(): array
{
if (!$this->comparator) {
throw new \LogicException('Comparator is not set');
}
uasort($this->elements, [$this->comparator, 'compare']);
return $this->elements;
}
/**
* @param ComparatorInterface $comparator
*/
public function setComparator(ComparatorInterface $comparator)
{
$this->comparator = $comparator;
}
}