-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChainFormatter.php
More file actions
54 lines (48 loc) · 1.37 KB
/
Copy pathChainFormatter.php
File metadata and controls
54 lines (48 loc) · 1.37 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
<?php declare(strict_types=1);
namespace uuf6429\PhpCsFixerBlockstring\Formatter;
use uuf6429\PhpCsFixerBlockstring\BlockString\BlockString;
/**
* This formatter allows multiple formatters to be applied sequentially – the output of each formatter becomes the
* input of the next one.
*
* Example:
*
* ```php
* return (new PhpCsFixer\Config())
* ->registerCustomFixers([new BlockStringFixer()])
* ->setRules([
* BlockStringFixer::NAME => BlockStringFixer::config([
* 'JSON' => new ChainFormatter(
* new CliPipeFormatter('v1', ['cmd' => 'some-old-tool']),
* new SimpleLineFormatter(4, "\t"),
* ),
* ]),
* ]);
* ```
*/
final class ChainFormatter extends AbstractFormatter
{
/**
* @var list<AbstractFormatter>
*/
private array $formatters;
/**
* @param AbstractFormatter ...$formatters
*/
public function __construct(AbstractFormatter ...$formatters)
{
$this->formatters = array_values($formatters);
parent::__construct([
self::class,
array_map(static fn(AbstractFormatter $formatter) => $formatter->getCacheFingerprint(), $this->formatters),
]);
}
public function formatBlock(BlockString $blockString): BlockString
{
$result = $blockString;
foreach ($this->formatters as $formatter) {
$result = $formatter->formatBlock($result);
}
return $result;
}
}