Passed
Pull Request — master (#1198)
by
unknown
04:08
created

ConceptPropertyValueLiteral::getDatatype()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * Class for handling concept property values.
5
 */
6
class ConceptPropertyValueLiteral extends VocabularyDataObject
7
{
8
    /** the literal object for the property value */
9
    private $literal;
10
    /** property type */
11
    private $type;
12
    /** content language */
13
    private $clang;
14
15
    public function __construct($model, $vocab, $resource, $literal, $prop, $clang = '')
16
    {
17
        parent::__construct($model, $vocab, $resource);
18
        $this->literal = $literal;
19
        $this->type = $prop;
20
        $this->clang = $clang;
21
    }
22
23
    public function __toString()
24
    {
25
        if ($this->getLabel() === null) {
26
            return "";
27
        }
28
29
        return $this->getLabel();
30
    }
31
32
    public function getLang()
33
    {
34
        return $this->literal->getLang();
35
    }
36
37
    /**
38
     * A method for fetching a datatype.
39
     */
40
    public function getDatatype(): ?string
41
    {
42
        $datatype = $this->literal->getDatatype();
43
        if ($datatype === null) {
44
            return null;
45
        }
46
        $graph = $this->resource->getGraph();
47
        $dtLabel = $graph->resource($datatype)->label($this->clang);
48
        return $dtLabel->getValue();
49
    }
50
51
    public function getType()
52
    {
53
        return $this->type;
54
    }
55
56
    public function getContainsHtml() {
57
        return preg_match("/\/[a-z]*>/i", $this->literal->getValue()) != 0;
58
    }
59
60
    public function getLabel()
61
    {
62
        // if the property is a date object converting it to a human readable representation.
63
        if ($this->literal instanceof EasyRdf\Literal\Date) {
64
            try {
65
                $val = $this->literal->getValue();
66
                return Punic\Calendar::formatDate($val, 'short');
0 ignored issues
show
Bug introduced by
$val of type string is incompatible with the type DateTime|DateTimeInterface expected by parameter $value of Punic\Calendar::formatDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
                return Punic\Calendar::formatDate(/** @scrutinizer ignore-type */ $val, 'short');
Loading history...
67
            } catch (Exception $e) {
68
                trigger_error($e->getMessage(), E_USER_WARNING);
69
                return (string) $this->literal;
70
            }
71
        }
72
        return $this->literal->getValue();
73
    }
74
75
    public function getUri()
76
    {
77
        return null;
78
    }
79
80
    public function getNotation()
81
    {
82
        return null;
83
    }
84
85
    public function hasXlProperties()
86
    {
87
        $graph = $this->resource->getGraph();
88
        $resources = $graph->resourcesMatching('skosxl:literalForm', $this->literal);
89
        return !empty($resources);
90
    }
91
92
    public function getXlProperties()
93
    {
94
        $ret = array();
95
        $graph = $this->resource->getGraph();
96
        $resources = $graph->resourcesMatching('skosxl:literalForm', $this->literal);
97
        foreach ($resources as $xlres) {
98
            foreach ($xlres->properties() as $prop) {
99
                foreach($graph->allLiterals($xlres, $prop) as $val) {
100
                    if ($prop !== 'rdf:type') {
101
                        $ret[$prop] = $val;
102
                    }
103
                }
104
            }
105
        }
106
        return $ret;
107
    }
108
109
}
110