BaseConfig::getBoolean()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
rs 10
1
<?php
2
3
/**
4
 * Base class for configurations. Contains methods for accessing RDF
5
 * Resources, handling literals and booleans.
6
 */
7
abstract class BaseConfig extends DataObject
8
{
9
10
	/**
11
     * Returns a boolean value based on a literal value from the config.ttl configuration.
12
     * @param string $property the property to query
13
     * @param boolean $default the default value if the value is not set in configuration
14
     * @return boolean the boolean value for the given property, or the default value if not found
15
     */
16
    protected function getBoolean($property, $default = false)
17
    {
18
        $val = $this->getResource()->getLiteral($property);
19
        if ($val) {
0 ignored issues
show
introduced by
$val is of type EasyRdf\Literal, thus it always evaluated to true.
Loading history...
20
            return filter_var($val->getValue(), FILTER_VALIDATE_BOOLEAN);
21
        }
22
        return $default;
23
    }
24
25
    /**
26
     * Returns an array of URIs based on a property from the config.ttl configuration.
27
     * @param string $property the property to query
28
     * @return string[] List of URIs
29
     */
30
    protected function getResources($property)
31
    {
32
        $resources = $this->getResource()->allResources($property);
33
        $ret = array();
34
        foreach ($resources as $res) {
35
            $ret[] = $res->getURI();
36
        }
37
        return $ret;
38
    }
39
40
    /**
41
     * Returns a string value based on a literal value from the config.ttl configuration.
42
     * @param string $property the property to query
43
     * @param string $default default value
44
     * @param string $lang preferred language for the literal
45
     * @return string string value for the given property, or the default value if not found
46
     */
47
    protected function getLiteral($property, $default=null, $lang=null)
48
    {
49
        if (!isset($lang)) {
50
            $lang = $this->getEnvLang();
51
        }
52
53
        $literal = $this->getResource()->getLiteral($property, $lang);
54
        if ($literal) {
0 ignored issues
show
introduced by
$literal is of type EasyRdf\Literal, thus it always evaluated to true.
Loading history...
55
            return $literal->getValue();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $literal->getValue() also could return the type DateTime|boolean which is incompatible with the documented return type string.
Loading history...
56
        }
57
58
        // not found with selected language, try to find one without a language tag
59
        foreach ($this->getResource()->allLiterals($property) as $literal) {
60
            if ($literal->getLang() === null) {
61
                return $literal->getValue();
62
            }
63
        }
64
65
        // not found, return the default value
66
        return $default;
67
    }
68
69
}
70