-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportCollection.php
More file actions
45 lines (37 loc) · 940 Bytes
/
ImportCollection.php
File metadata and controls
45 lines (37 loc) · 940 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
44
45
<?php
declare(strict_types=1);
namespace Auroro\Code;
final class ImportCollection
{
/** @var array<string, true> */
private array $modules = [];
/**
* Add a module to the collection (deduplicates).
*/
public function add(string $module): void
{
$this->modules[$module] = true;
}
/**
* Check if a module is in the collection.
*/
public function has(string $module): bool
{
return isset($this->modules[$module]);
}
/**
* Render all imports sorted alphabetically, one per line.
*/
public function render(string $keyword = 'import'): string
{
if ([] === $this->modules) {
return '';
}
$names = array_keys($this->modules);
sort($names);
return implode("\n", array_map(
static fn(string $name): string => $keyword . ' ' . $name . ';',
$names,
));
}
}