Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
EntitySchemaRdfBuilder
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
3 / 3
8
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
 addValue
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
 getEntitySchemaPrefix
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare( strict_types = 1 );
4
5namespace EntitySchema\Wikibase\Rdf;
6
7use EntitySchema\Wikibase\DataValues\EntitySchemaValue;
8use Wikibase\DataModel\Snak\PropertyValueSnak;
9use Wikibase\Repo\Rdf\RdfVocabulary;
10use Wikibase\Repo\Rdf\ValueSnakRdfBuilder;
11use Wikimedia\Purtle\RdfWriter;
12
13/**
14 * RDF mapping for EntitySchema values.
15 *
16 * @license GPL-2.0-or-later
17 * @author Marius Hoch <mail@mariushoch.de>
18 */
19class EntitySchemaRdfBuilder implements ValueSnakRdfBuilder {
20
21    private RdfVocabulary $vocabulary;
22
23    private string $wikibaseConceptBaseUri;
24
25    private bool $entitySchemaPrefixInitialized = false;
26
27    private ?string $entitySchemaPrefix;
28
29    public function __construct( RdfVocabulary $vocabulary, string $wikibaseConceptBaseUri ) {
30        $this->vocabulary = $vocabulary;
31        $this->wikibaseConceptBaseUri = $wikibaseConceptBaseUri;
32    }
33
34    /**
35     * Adds specific value
36     *
37     * @param RdfWriter $writer
38     * @param string $propertyValueNamespace Property value relation namespace
39     * @param string $propertyValueLName Property value relation name
40     * @param string $dataType Property data type
41     * @param string $snakNamespace
42     * @param PropertyValueSnak $snak
43     */
44    public function addValue(
45        RdfWriter $writer,
46        $propertyValueNamespace,
47        $propertyValueLName,
48        $dataType,
49        $snakNamespace,
50        PropertyValueSnak $snak
51    ) {
52        $entitySchemaPrefix = $this->getEntitySchemaPrefix();
53        $dataValue = $snak->getDataValue();
54        $title = $dataValue->getValue();
55        if ( $dataValue instanceof EntitySchemaValue ) {
56            $title = $dataValue->getSchemaId();
57        }
58        if ( $entitySchemaPrefix ) {
59            $writer->say( $propertyValueNamespace, $propertyValueLName )->is(
60                $entitySchemaPrefix,
61                $title
62            );
63        } else {
64            $writer->say( $propertyValueNamespace, $propertyValueLName )->is(
65                trim( $this->wikibaseConceptBaseUri . $title )
66            );
67        }
68    }
69
70    /**
71     * @return string|null Prefix to use for entity schemas or null if none found.
72     */
73    private function getEntitySchemaPrefix(): ?string {
74        if ( !$this->entitySchemaPrefixInitialized ) {
75            $this->entitySchemaPrefixInitialized = true;
76            $this->entitySchemaPrefix = null;
77
78            foreach ( $this->vocabulary->getNamespaces() as $prefix => $uri ) {
79                if ( $uri === $this->wikibaseConceptBaseUri ) {
80                    $this->entitySchemaPrefix = $prefix;
81                    break;
82                }
83            }
84        }
85
86        return $this->entitySchemaPrefix;
87    }
88
89}