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

ConceptPropertyValueLiteral   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 98
rs 10
wmc 19

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 3 1
A __construct() 0 6 1
A getContainsHtml() 0 2 1
A getXlProperties() 0 15 5
A getUri() 0 3 1
A getLang() 0 3 1
A getNotation() 0 3 1
A hasXlProperties() 0 5 1
A getDatatype() 0 9 2
A getLabel() 0 13 3
A __toString() 0 7 2
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
    public function getDatatype()
38
    {
39
        $datatype = $this->literal->getDatatype();
40
        if ($datatype === null) {
41
            return null;
42
        }
43
        $graph = $this->resource->getGraph();
44
        $dtLabel = $graph->resource($datatype)->label($this->clang);
45
        return $dtLabel->getValue();
46
    }
47
48
    public function getType()
49
    {
50
        return $this->type;
51
    }
52
53
    public function getContainsHtml() {
54
        return preg_match("/\/[a-z]*>/i", $this->literal->getValue()) != 0;
55
    }
56
57
    public function getLabel()
58
    {
59
        // if the property is a date object converting it to a human readable representation.
60
        if ($this->literal instanceof EasyRdf\Literal\Date) {
61
            try {
62
                $val = $this->literal->getValue();
63
                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

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