DataObject   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 143
rs 10
c 0
b 0
f 0
wmc 27

7 Methods

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