EscapingEntityIdFormatter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 41
ccs 9
cts 10
cp 0.9
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A formatEntityId() 0 5 1
1
<?php
2
3
namespace Wikibase\DataModel\Services\EntityId;
4
5
use InvalidArgumentException;
6
use Wikibase\DataModel\Entity\EntityId;
7
8
/**
9
 * EscapingEntityIdFormatter wraps another EntityIdFormatter and
10
 * applies a transformation (escaping) to that formatter's output.
11
 *
12
 * @since 1.1
13
 *
14
 * @license GPL-2.0-or-later
15
 * @author Daniel Kinzler
16
 */
17
class EscapingEntityIdFormatter implements EntityIdFormatter {
18
19
	/**
20
	 * @var EntityIdFormatter
21
	 */
22
	private $formatter;
23
24
	/**
25
	 * @var callable
26
	 */
27
	private $escapeCallback;
28
29
	/**
30
	 * @param EntityIdFormatter $formatter A formatter returning plain text.
31
	 * @param callable $escapeCallback A callable taking plain text and returning escaped text.
32
	 *
33
	 * @throws InvalidArgumentException
34 1
	 */
35 1
	public function __construct( EntityIdFormatter $formatter, $escapeCallback ) {
36
		if ( !is_callable( $escapeCallback ) ) {
37
			throw new InvalidArgumentException( '$escapeCallback must be callable' );
38
		}
39 1
40 1
		$this->formatter = $formatter;
41 1
		$this->escapeCallback = $escapeCallback;
42
	}
43
44
	/**
45
	 * @see EntityIdFormatter::formatEntityId
46
	 *
47
	 * @param EntityId $value
48
	 *
49 1
	 * @return string Typically wikitext or HTML, depending on the $escapeCallback provided.
50 1
	 */
51 1
	public function formatEntityId( EntityId $value ) {
52 1
		$text = $this->formatter->formatEntityId( $value );
53
		$escaped = call_user_func( $this->escapeCallback, $text );
54
		return $escaped;
55
	}
56
57
}
58