Issues (160)

src/model/BaseConfig.php (3 issues)

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
     * Returns a boolean value based on a literal value from the config.ttl configuration.
11
     * @param string $property the property to query
12
     * @param boolean $default the default value if the value is not set in configuration
13
     * @return boolean the boolean value for the given property, or the default value if not found
14
     */
15
    protected function getBoolean($property, $default = false)
16
    {
17
        $val = $this->getResource()->getLiteral($property);
18
        if ($val) {
0 ignored issues
show
$val is of type EasyRdf\Literal, thus it always evaluated to true.
Loading history...
19
            return filter_var($val->getValue(), FILTER_VALIDATE_BOOLEAN);
20
        }
21
        return $default;
22
    }
23
24
    /**
25
     * Returns an array of URIs based on a property from the config.ttl configuration.
26
     * @param string $property the property to query
27
     * @return string[] List of URIs
28
     */
29
    protected function getResources($property)
30
    {
31
        $resources = $this->getResource()->allResources($property);
32
        $ret = array();
33
        foreach ($resources as $res) {
34
            $ret[] = $res->getURI();
35
        }
36
        return $ret;
37
    }
38
39
    /**
40
     * Returns a string value based on a literal value from the config.ttl configuration.
41
     * @param string $property the property to query
42
     * @param string $default default value
43
     * @param string $lang preferred language for the literal
44
     * @return string string value for the given property, or the default value if not found
45
     */
46
    protected function getLiteral($property, $default = null, $lang = null)
47
    {
48
        if (!isset($lang)) {
49
            $lang = $this->getLang();
50
        }
51
52
        $literal = $this->getResource()->getLiteral($property, $lang);
53
        if ($literal) {
0 ignored issues
show
$literal is of type EasyRdf\Literal, thus it always evaluated to true.
Loading history...
54
            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...
55
        }
56
57
        // not found with selected language, try to find one without a language tag
58
        foreach ($this->getResource()->allLiterals($property) as $literal) {
59
            if ($literal->getLang() === null) {
60
                return $literal->getValue();
61
            }
62
        }
63
64
        // not found, return the default value
65
        return $default;
66
    }
67
68
}
69