|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace ApiGen\Parser\Elements; |
|
4
|
|
|
|
|
5
|
|
|
use ApiGen\Contracts\Parser\Elements\ElementsInterface; |
|
6
|
|
|
use ApiGen\Contracts\Parser\Elements\NamespaceSorterInterface; |
|
7
|
|
|
|
|
8
|
|
|
final class NamespaceSorter implements NamespaceSorterInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var mixed[] |
|
12
|
|
|
*/ |
|
13
|
|
|
private $namespaces; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var ElementsInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $elements; |
|
19
|
|
|
|
|
20
|
41 |
|
public function __construct(ElementsInterface $elements) |
|
21
|
|
|
{ |
|
22
|
41 |
|
$this->elements = $elements; |
|
23
|
41 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param mixed[] $namespaces |
|
27
|
|
|
* @return mixed[] |
|
28
|
|
|
*/ |
|
29
|
22 |
|
public function sort(array $namespaces): array |
|
30
|
|
|
{ |
|
31
|
22 |
|
if ($this->isNoneOnly($namespaces)) { |
|
32
|
1 |
|
return []; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
21 |
|
$this->namespaces = $namespaces; |
|
36
|
|
|
|
|
37
|
21 |
|
$namespaceNames = array_keys($namespaces); |
|
38
|
|
|
|
|
39
|
21 |
|
foreach ($namespaceNames as $namespaceName) { |
|
40
|
10 |
|
$this->addMissingParentNamespaces($namespaceName); |
|
41
|
10 |
|
$this->addMissingElementTypes($namespaceName); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
21 |
|
uksort($this->namespaces, function ($one, $two) { |
|
45
|
6 |
|
return $this->compareNamespaceNames($one, $two); |
|
46
|
21 |
|
}); |
|
47
|
|
|
|
|
48
|
21 |
|
return $this->namespaces; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param mixed[] $namespaces |
|
53
|
|
|
*/ |
|
54
|
23 |
|
private function isNoneOnly(array $namespaces): bool |
|
55
|
|
|
{ |
|
56
|
23 |
|
return count($namespaces) === 1 && isset($namespaces['None']); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
11 |
|
private function addMissingParentNamespaces(string $namespaceName): void |
|
60
|
|
|
{ |
|
61
|
11 |
|
$parent = ''; |
|
62
|
11 |
|
foreach (explode('\\', $namespaceName) as $part) { |
|
63
|
11 |
|
$parent = ltrim($parent . '\\' . $part, '\\'); |
|
64
|
|
|
|
|
65
|
11 |
|
if (! isset($this->namespaces[$parent])) { |
|
66
|
6 |
|
$this->namespaces[$parent] = $this->elements->getEmptyList(); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
11 |
|
} |
|
70
|
|
|
|
|
71
|
11 |
|
private function addMissingElementTypes(string $namespaceName): void |
|
72
|
|
|
{ |
|
73
|
11 |
|
foreach ($this->elements->getAll() as $type) { |
|
74
|
11 |
|
if (! isset($this->namespaces[$namespaceName][$type])) { |
|
75
|
10 |
|
$this->namespaces[$namespaceName][$type] = []; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
11 |
|
} |
|
79
|
|
|
|
|
80
|
8 |
|
private function compareNamespaceNames(string $firstNamespace, string $secondNamespace): int |
|
81
|
|
|
{ |
|
82
|
|
|
// \ as separator has to be first |
|
83
|
8 |
|
$firstNamespace = str_replace('\\', ' ', $firstNamespace); |
|
84
|
8 |
|
$secondNamespace = str_replace('\\', ' ', $secondNamespace); |
|
85
|
|
|
|
|
86
|
8 |
|
return strcasecmp($firstNamespace, $secondNamespace); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|