Passed
Push — master ( 67a964...2aff1a )
by Konrad
04:08
created

Literal::withValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace sweetrdf\InMemoryStoreSqlite\Rdf;
4
5
/*
6
 * This file is part of the sweetrdf/InMemoryStoreSqlite package and licensed under
7
 * the terms of the GPL-3 license.
8
 *
9
 * (c) Konrad Abicht <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
use Exception;
16
use rdfInterface\Literal as iLiteral;
17
use rdfInterface\Term;
18
use Stringable;
19
use sweetrdf\InMemoryStoreSqlite\NamespaceHelper;
20
21
class Literal implements iLiteral
22
{
23
    private int | float | string | bool | Stringable $value;
24
25
    private ?string $lang;
26
27
    private ?string $datatype;
28
29 5
    public function __construct(
30
        int | float | string | bool | Stringable $value,
31
        ?string $lang = null,
32
        ?string $datatype = null
33
    ) {
34 5
        $this->value = $value;
35
36
        /*
37
         * @see https://www.w3.org/TR/rdf11-concepts/#section-Graph-Literal
38
         */
39 5
        if (!empty($lang)) {
40 3
            $this->lang = $lang;
41 3
            $this->datatype = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString';
42
        } else {
43 5
            $this->lang = null;
44 5
            $this->datatype = $datatype ?? NamespaceHelper::NAMESPACE_XSD.'string';
45
        }
46 5
    }
47
48 1
    public function __toString(): string
49
    {
50 1
        $langtype = '';
51 1
        if (!empty($this->lang)) {
52 1
            $langtype = '@'.$this->lang;
53 1
        } elseif (!empty($this->datatype)) {
54 1
            $langtype = "^^<$this->datatype>";
55
        }
56
57 1
        return '"'.$this->value.'"'.$langtype;
58
    }
59
60 1
    public function getValue(): int | float | string | bool | Stringable
61
    {
62 1
        return $this->value;
63
    }
64
65 1
    public function getLang(): ?string
66
    {
67 1
        return $this->lang;
68
    }
69
70 1
    public function getDatatype(): string
71
    {
72 1
        return $this->datatype;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->datatype could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
73
    }
74
75
    public function getType(): string
76
    {
77
        return \rdfInterface\TYPE_LITERAL;
78
    }
79
80
    public function equals(Term $term): bool
81
    {
82
        return $this == $term;
83
    }
84
85
    public function withValue(int | float | string | bool | Stringable $value): self
86
    {
87
        throw new Exception('withValue not implemented yet');
88
    }
89
90
    public function withLang(?string $lang): self
91
    {
92
        throw new Exception('withLang not implemented yet');
93
    }
94
95
    public function withDatatype(?string $datatype): self
96
    {
97
        throw new Exception('withDatatype not implemented yet');
98
    }
99
}
100