Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.65% covered (success)
95.65%
22 / 23
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
LexemeMetaTagsCreator
95.65% covered (success)
95.65%
22 / 23
75.00% covered (warning)
75.00%
3 / 4
8
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getMetaTags
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
 getTitleText
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 getDescriptionText
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Wikibase\Lexeme\Presentation\View;
4
5use Wikibase\DataModel\Entity\EntityDocument;
6use Wikibase\Lexeme\Domain\Model\Lexeme;
7use Wikibase\Lib\Store\FallbackLabelDescriptionLookup;
8use Wikibase\View\EntityMetaTagsCreator;
9use Wikimedia\Assert\Assert;
10
11/**
12 * Class for creating meta tags (i.e. title and description) for Lexemes
13 * @license GPL-2.0-or-later
14 */
15class LexemeMetaTagsCreator implements EntityMetaTagsCreator {
16
17    /** @var string */
18    private $lemmaSeparator;
19    private FallbackLabelDescriptionLookup $labelDescriptionLookup;
20
21    /**
22     * @param string $lemmaSeparator
23     * @param FallbackLabelDescriptionLookup $labelDescriptionLookup
24     */
25    public function __construct(
26        $lemmaSeparator,
27        FallbackLabelDescriptionLookup $labelDescriptionLookup
28    ) {
29        Assert::parameterType( 'string', $lemmaSeparator, '$lemmaSeparator' );
30
31        $this->lemmaSeparator = $lemmaSeparator;
32        $this->labelDescriptionLookup = $labelDescriptionLookup;
33    }
34
35    public function getMetaTags( EntityDocument $entity ): array {
36        Assert::parameterType( Lexeme::class, $entity, '$entity' );
37        '@phan-var Lexeme $entity';
38
39        /** @var Lexeme $entity */
40        $metaTags = [
41            'title' => $this->getTitleText( $entity ),
42            'og:title' => $this->getTitleText( $entity ),
43            'twitter:card' => 'summary',
44        ];
45
46        $description = $this->getDescriptionText( $entity );
47        if ( $description !== '' ) {
48            $metaTags[ 'description' ] = $description;
49            $metaTags[ 'og:description' ] = $description;
50
51        }
52
53        return $metaTags;
54    }
55
56    /**
57     * @param Lexeme $entity
58     *
59     * @return null|string
60     */
61    private function getTitleText( Lexeme $entity ) {
62        $lemmas = $entity->getLemmas()->toTextArray();
63        if ( !$lemmas ) {
64            return $entity->getId()->getSerialization();
65        }
66        return implode( $this->lemmaSeparator, $lemmas );
67    }
68
69    private function getDescriptionText( Lexeme $entity ): string {
70        $language = $this->labelDescriptionLookup->getLabel( $entity->getLanguage() );
71        $category = $this->labelDescriptionLookup->getLabel( $entity->getLexicalCategory() );
72
73        if ( !$language || !$category ) {
74            return '';
75        }
76
77        return $language->getText() . ' ' . $category->getText();
78    }
79
80}