Completed
Push — master ( 2c8400...fa2a9a )
by Henri
02:11
created

DataObject::getExternalNotation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
1
<?php
2
3
/**
4
 * Dataobject wraps EasyRdf resources and provides access to the data.
5
 */
6
class DataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
     */
26 View Code Duplication
    public function __construct($model, $resource)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28
        if (!($model instanceof Model) || !($resource instanceof EasyRdf_Resource)) {
29
            throw new Exception('Invalid constructor parameter given to DataObject.');
30
        }
31
32
        $this->model = $model;
33
        $this->resource = $resource;
34
        $this->order = array();
35
    }
36
37
    /**
38
     * Generates and makes a query into a external vocabulary for an exact
39
     * match for a particular concept.
40
     * @param Vocabulary $exvoc external vocabulary to query
41
     * @param string $exuri resource URI
42
     * @param string $lang language of label to query for
43
     * @return EasyRdf_Literal label, or null if not found in vocabulary
44
     */
45
    protected function getExternalLabel($exvoc, $exuri, $lang)
46
    {
47
        if ($exvoc) {
48
            $exsparql = $exvoc->getSparql();
49
            $results = $exsparql->queryLabel($exuri, $lang);
50
51
            return isset($results[$lang]) ? $results[$lang] : null;
52
        }
53
        return null;
54
    }
55
56
    /**
57
     * Generates and makes a query into a external vocabulary for the notation of an exact
58
     * match for a particular concept.
59
     * @param Vocabulary $exvoc external vocabulary to query
60
     * @param string $exuri resource URI
61
     */
62
    protected function getExternalNotation($exvoc, $exuri)
63
    {
64
        if ($exvoc) {
65
            $exsparql = $exvoc->getSparql();
66
            $results = $exsparql->queryNotation($exuri);
67
            return isset($results) ? $results : null;
68
        }
69
        return null;
70
    }
71
72
    /**
73
     * Sorting the result list to a arbitrary order defined below in mycompare()
74
     * @param array $sortable
75
     */
76
    protected function arbitrarySort($sortable)
77
    {
78
        // sorting the result list to a arbitrary order defined below in mycompare()
79
        if ($sortable !== null) {
80
            uksort($sortable, array($this, 'mycompare'));
81
            foreach ($sortable as $prop => $vals) {
82
                if (is_array($prop)) // the ConceptProperty objects have their own sorting methods
83
                {
84
                    ksort($sortable[$prop]);
85
                }
86
            }
87
        }
88
        return $sortable;
89
    }
90
91
    /**
92
     * Compares the given objects and returns -1 or 1 depending which ought to be first.
93
     * $order defines the priorities of the different properties possible in the array.
94
     * @param string $a the first item to be compared
95
     * @param string $b the second item to be compared
96
     */
97
    protected function mycompare($a, $b)
98
    {
99
        if ($a === $b) {
100
            return 0;
101
        }
102
        $order = $this->order;
103
        $position = array_search($a, $order);
104
        $position2 = array_search($b, $order);
105
106
        //if both are in the $order, then sort according to their order in $order...
107
        if ($position2 !== false && $position !== false) {
108
            return ($position < $position2) ? -1 : 1;
109
        }
110
        //if only one is in $order, then sort to put the one in $order first...
111
        if ($position !== false) {
112
            return -1;
113
        }
114
        if ($position2 !== false) {
115
            return 1;
116
        }
117
118
        //if neither in $order, then a simple alphabetic sort...
119
        return ($a < $b) ? -1 : 1;
120
    }
121
        
122
    /**
123
     * Getter function to retrieve the ui language from the locale.
124
     */
125
    public function getEnvLang()    
126
    {
127
       // get language from locale, same as used by gettext, set by Controller
128
       return substr(getenv("LC_ALL"), 0, 2); // @codeCoverageIgnore
129
    }
130
}
131