forked from FriendsOfSymfony/FOSCommentBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortingFactory.php
More file actions
68 lines (56 loc) · 1.54 KB
/
SortingFactory.php
File metadata and controls
68 lines (56 loc) · 1.54 KB
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
<?php
/**
* This file is part of the FOSCommentBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace FOS\CommentBundle\Sorting;
use InvalidArgumentException;
use RuntimeException;
/**
* Sorting Factory.
*
* @author Tim Nagel <[email protected]>
*/
class SortingFactory
{
/**
* @var array of SortingInterface
*/
private $sorters;
/**
* @var string Default SortingInterface alias
*/
private $defaultSorter;
/**
* @param array $sorters An array of SortingInterfaces
* @param string $defaultSorter The alias of the sorter to use by default
*/
public function __construct(array $sorters, $defaultSorter)
{
foreach ($sorters as $alias => $sorter) {
if (!$sorter instanceof SortingInterface) {
throw new InvalidArgumentException('Sorters must implement SortingInterface');
}
$this->sorters[$alias] = $sorter;
}
$this->defaultSorter = $defaultSorter;
}
public function getSorter($alias = null)
{
if (null === $alias) {
$alias = $this->defaultSorter;
}
if (!$this->sorters[$alias]) {
throw new RuntimeException(sprintf("Unknown sorting alias '%s'", $alias));
}
return $this->sorters[$alias];
}
public function getAvailableSorters()
{
return array_keys($this->sorters);
}
}