|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use Brendt\Stitcher\Exception\ConfigurationException; |
|
6
|
|
|
use Brendt\Stitcher\Factory\AdapterFactory; |
|
7
|
|
|
use Brendt\Stitcher\Site\Page; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Order a collection of variables by a specified field. |
|
11
|
|
|
* |
|
12
|
|
|
* Sample configuration: |
|
13
|
|
|
* |
|
14
|
|
|
* /examples: |
|
15
|
|
|
* template: examples/detail |
|
16
|
|
|
* data: |
|
17
|
|
|
* example: collection.yml |
|
18
|
|
|
* adapters: |
|
19
|
|
|
* order: |
|
20
|
|
|
* variableName: |
|
21
|
|
|
* field: id |
|
22
|
|
|
* direction: desc |
|
23
|
|
|
*/ |
|
24
|
|
|
class OrderAdapter extends AbstractAdapter |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* A collection of keywords which would reverse the data set. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
const REVERSE = [ |
|
33
|
|
|
'desc', |
|
34
|
|
|
'DESC', |
|
35
|
|
|
'-', |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Page $page |
|
40
|
|
|
* @param string|null $filter |
|
41
|
|
|
* |
|
42
|
|
|
* @return Page[] |
|
43
|
|
|
*/ |
|
44
|
|
|
public function transformPage(Page $page, $filter = null) : array { |
|
45
|
|
|
$config = $page->getAdapterConfig(AdapterFactory::ORDER_ADAPTER); |
|
46
|
|
|
$config = isset($config['variable']) ? [$config['variable'] => $config] : $config; |
|
47
|
|
|
|
|
48
|
|
|
foreach ($config as $variable => $order) { |
|
49
|
|
|
$this->validateConfig($order); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
$field = $order['field']; |
|
52
|
|
|
$direction = $order['direction'] ?? null; |
|
53
|
|
|
|
|
54
|
|
|
$entries = $this->getData($page->getVariable($variable)); |
|
55
|
|
|
|
|
56
|
|
|
uasort($entries, function ($a, $b) use ($field) { |
|
57
|
|
|
return strcmp($a[$field], $b[$field]); |
|
58
|
|
|
}); |
|
59
|
|
|
|
|
60
|
|
|
if (in_array($direction, self::REVERSE)) { |
|
61
|
|
|
$entries = array_reverse($entries, true); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$page->setVariableValue($variable, $entries) |
|
65
|
|
|
->setVariableIsParsed($variable); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** @var Page[] $result */ |
|
69
|
|
|
$result = [$page->getId() => $page]; |
|
70
|
|
|
|
|
71
|
|
|
return $result; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param array $config |
|
76
|
|
|
* |
|
77
|
|
|
* @throws ConfigurationException |
|
78
|
|
|
*/ |
|
79
|
|
|
private function validateConfig(array $config) { |
|
80
|
|
|
if (!isset($config['field'])) { |
|
81
|
|
|
throw ConfigurationException::requiredAdapterOptions(AdapterFactory::ORDER_ADAPTER, 'field'); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.