-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractFormatter.php
More file actions
64 lines (57 loc) · 2.1 KB
/
Copy pathAbstractFormatter.php
File metadata and controls
64 lines (57 loc) · 2.1 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
<?php declare(strict_types=1);
namespace uuf6429\PhpCsFixerBlockstring\Formatter;
use JsonSerializable;
use uuf6429\PhpCsFixerBlockstring\BlockString\BlockString;
use uuf6429\PhpCsFixerBlockstring\CacheFingerprintableInterface;
use uuf6429\PhpCsFixerBlockstring\InterpolationCodec\CodecInterface;
/**
* This is the base class of all formatters. In most cases you don't really want to extend this class, since it does
* not handle string interpolation at all – check out {@see AbstractStringFormatter} instead.
*
* Extending this class makes sense in two situations:
*
* 1. If your class is infrastructural, and you don't really need to handle string interpolation - just like
* {@see ChainFormatter}
* 2. Or if, for whatever reason, the {@see CodecInterface} concept does not work for you and you want to write
* something from scratch.
*/
abstract class AbstractFormatter implements CacheFingerprintableInterface, JsonSerializable
{
/**
* @var mixed
* @readonly
*/
private $cacheFingerprint;
/**
* @param mixed $cacheFingerprint A unique representation of the formatter logic and its configuration, used for
* caching purposes. For example, if the formatter executes some cli command with a specific version, the
* fingerprint should contain:
* - the formatter class (to distguish from other formatters)
* - the cli command name (since it's a setting of the formatter)
* - the cli command version (in case the cli command gets updated at some point)
*
* **Important:** Make sure that the fingerprint contains simple values (null, scalar or arrays).
*/
public function __construct($cacheFingerprint)
{
$this->cacheFingerprint = $cacheFingerprint;
}
/**
* Format the provided BlockString accordingly and return a new one.
*/
abstract public function formatBlock(BlockString $blockString): BlockString;
/**
* @return mixed
*/
final public function getCacheFingerprint()
{
return $this->cacheFingerprint;
}
/**
* @return array{cacheFingerprint: mixed, ...}
*/
public function jsonSerialize(): array
{
return ['cacheFingerprint' => $this->getCacheFingerprint()];
}
}