Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
FormIdTextFormatter
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 formatEntityId
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare( strict_types = 1 );
4
5namespace Wikibase\Lexeme\Presentation\Formatters;
6
7use InvalidArgumentException;
8use Wikibase\DataModel\Entity\EntityId;
9use Wikibase\DataModel\Services\EntityId\EntityIdFormatter;
10use Wikibase\DataModel\Services\Lookup\UnresolvedEntityRedirectException;
11use Wikibase\Lexeme\Domain\Model\Form;
12use Wikibase\Lexeme\Domain\Model\FormId;
13use Wikibase\Lib\Store\EntityRevisionLookup;
14use Wikibase\View\LocalizedTextProvider;
15
16/**
17 * @license GPL-2.0-or-later
18 */
19class FormIdTextFormatter implements EntityIdFormatter {
20
21    private const REPRESENTATION_SEPARATOR_I18N =
22        'wikibaselexeme-formidformatter-separator-multiple-representation';
23
24    private EntityRevisionLookup $revisionLookup;
25    private LocalizedTextProvider $localizedTextProvider;
26
27    public function __construct(
28        EntityRevisionLookup $revisionLookup,
29        LocalizedTextProvider $localizedTextProvider
30    ) {
31        $this->revisionLookup = $revisionLookup;
32        $this->localizedTextProvider = $localizedTextProvider;
33    }
34
35    public function formatEntityId( EntityId $formId ): string {
36        if ( !( $formId instanceof FormId ) ) {
37            throw new InvalidArgumentException(
38                'Attemped to format a non-Form entity as a Form: ' . $formId->getSerialization() );
39        }
40        try {
41            $formRevision = $this->revisionLookup->getEntityRevision( $formId );
42        } catch ( UnresolvedEntityRedirectException $exception ) {
43            return $formId->getSerialization();
44        }
45
46        if ( $formRevision === null ) {
47            return $formId->getSerialization();
48        }
49
50        /** @var Form $form */
51        $form = $formRevision->getEntity();
52        '@phan-var Form $form';
53        $representations = $form->getRepresentations();
54        $representationSeparator = $this->localizedTextProvider->get(
55            self::REPRESENTATION_SEPARATOR_I18N
56        );
57
58        $representationString = implode(
59            $representationSeparator,
60            $representations->toTextArray()
61        );
62
63        return $representationString;
64    }
65
66}