Issues (160)

src/model/DataObject.php (3 issues)

Severity
1
<?php
2
3
/**
4
 * Dataobject wraps EasyRdf resources and provides access to the data.
5
 */
6
class DataObject
7
{
8
    /**
9
     * Preferred order of properties, to be set by subclasses
10
     */
11
    protected $order;
12
    /**
13
     * Model instance which created this object
14
     */
15
    protected $model;
16
    /**
17
     * EasyRdf resource representing this object
18
     */
19
    protected $resource;
20
21
    /**
22
     * Initializes the DataObject
23
     * @param Model $model
24
     * @param EasyRdf\Resource $resource
25
     * @throws Exception
26
     */
27
    public function __construct(Model $model, EasyRdf\Resource $resource)
28
    {
29
        $this->model = $model;
30
        $this->resource = $resource;
31
        $this->order = array();
32
    }
33
34
    /**
35
     * Generates and makes a query into a external vocabulary for an exact
36
     * match for a particular concept.
37
     * @param Vocabulary $exvoc external vocabulary to query
38
     * @param string $exuri resource URI
39
     * @param string $lang language of label to query for
40
     * @return EasyRdf\Literal label, or null if not found in vocabulary
41
     */
42
    protected function getExternalLabel($exvoc, $exuri, $lang)
43
    {
44
        if ($exvoc) {
0 ignored issues
show
$exvoc is of type Vocabulary, thus it always evaluated to true.
Loading history...
45
            try {
46
                $exsparql = $exvoc->getSparql();
47
                $results = $exsparql->queryLabel($exuri, $lang);
48
                return isset($results[$lang]) ? $results[$lang] : null;
49
            } catch (EasyRdf\Http\Exception | EasyRdf\Exception | Throwable $e) {
50
                if ($this->model->getConfig()->getLogCaughtExceptions()) {
51
                    error_log('Caught exception: ' . $e->getMessage());
52
                }
53
            }
54
        }
55
        return null;
56
    }
57
58
    /**
59
     * Generates and makes a query into a external vocabulary for the notation of an exact
60
     * match for a particular concept.
61
     * @param Vocabulary $exvoc external vocabulary to query
62
     * @param string $exuri resource URI
63
     */
64
    protected function getExternalNotation($exvoc, $exuri)
65
    {
66
        if ($exvoc) {
0 ignored issues
show
$exvoc is of type Vocabulary, thus it always evaluated to true.
Loading history...
67
            try {
68
                $exsparql = $exvoc->getSparql();
69
                $results = $exsparql->queryNotation($exuri);
70
                return isset($results) ? $results : null;
71
            } catch (EasyRdf\Http\Exception | EasyRdf\Exception | Throwable $e) {
72
                if ($this->model->getConfig()->getLogCaughtExceptions()) {
73
                    error_log('Caught exception: ' . $e->getMessage());
74
                }
75
            }
76
        }
77
        return null;
78
    }
79
80
    /**
81
     * Sorting the result list to a arbitrary order defined below in mycompare()
82
     * @param array $sortable
83
     */
84
    protected function arbitrarySort($sortable)
85
    {
86
        // sorting the result list to a arbitrary order defined below in mycompare()
87
        if ($sortable !== null) {
0 ignored issues
show
The condition $sortable !== null is always true.
Loading history...
88
            uksort($sortable, array($this, 'mycompare'));
89
            foreach ($sortable as $prop => $vals) {
90
                if (is_array($prop)) { // the ConceptProperty objects have their own sorting methods
91
                    ksort($sortable[$prop]);
92
                }
93
            }
94
        }
95
        return $sortable;
96
    }
97
98
    /**
99
     * Compares the given objects and returns -1 or 1 depending which ought to be first.
100
     * $order defines the priorities of the different properties possible in the array.
101
     * @param string $a the first item to be compared
102
     * @param string $b the second item to be compared
103
     */
104
    protected function mycompare($a, $b)
105
    {
106
        if ($a === $b) {
107
            return 0;
108
        }
109
        $order = $this->order;
110
        $position = array_search($a, $order);
111
        $position2 = array_search($b, $order);
112
113
        //if both are in the $order, then sort according to their order in $order...
114
        if ($position2 !== false && $position !== false) {
115
            return ($position < $position2) ? -1 : 1;
116
        }
117
        //if only one is in $order, then sort to put the one in $order first...
118
        if ($position !== false) {
119
            return -1;
120
        }
121
        if ($position2 !== false) {
122
            return 1;
123
        }
124
125
        //if neither in $order, then a simple alphabetic sort...
126
        return ($a < $b) ? -1 : 1;
127
    }
128
129
    /**
130
     * Getter function to retrieve the ui language from Symfony translator.
131
     */
132
    public function getLang()
133
    {
134
        if (!is_null($this->model)) {
135
            return $this->model->getLocale();
136
        }
137
    }
138
139
    /**
140
     * Getter function for retrieving the resource.
141
     */
142
    public function getResource()
143
    {
144
        return $this->resource;
145
    }
146
}
147