Client::getSynsets()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 6
1
<?php
2
namespace Bedd\BabelNet;
3
4
/**
5
 * Client for the BabelNet HTTP Api
6
 * 
7
 * @see http://babelnet.org/guide
8
 */
9
class Client
10
{
11
    /**
12
     * API Key
13
     * 
14
     * @var string
15
     */
16
    private $api_key = '';
17
    
18
    /**
19
     * Base URL
20
     * 
21
     * @var string
22
     */
23
    private $base_url = 'https://babelnet.io/v4/';
24
    
25
    /**
26
     * @var array
27
     */
28
    private $defaultParams = [];
29
    
30
    /**
31
     * Constructor
32
     * 
33
     * @param string $api_key
34
     * @param array $defaultParams
35
     */
36
    public function __construct($api_key, array $defaultParams = [])
37
    {
38
        $this->api_key = $api_key;
39
        $this->defaultParams = $defaultParams;
40
    }
41
42
    /**
43
     * Executes an api call
44
     * @param string $service
45
     * @param array $params
46
     * @throws \Exception
47
     * @return mixed
48
     */
49
    private function exec($service, $params = array())
50
    {
51
        //add key
52
        $params['key'] = $this->api_key;
53
        //make request
54
        $url = $this->base_url.$service.'?'.http_build_query($params);
55
        $ch = curl_init();
56
        curl_setopt($ch, CURLOPT_URL, $url);
57
        curl_setopt($ch, CURLOPT_POST, false);
58
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
59
        curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
60
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
61
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
62
        $response = json_decode(curl_exec($ch), true);
63
        $info = curl_getinfo($ch);
64
        if ($info['http_code'] !== 200) {
65
            throw new \Exception($response['message'], $info['http_code']);
66
        }
67
        curl_close($ch);
68
        return $response;
69
    }
70
    
71
    /**
72
     * Returns a param list from a method-reflection and his params
73
     * 
74
     * @param string $method
75
     * @param array $args
76
     * @return array
77
     */
78
    private function getParamsByArguments($method, $args)
79
    {
80
        $params = [];
81
        $m = new \ReflectionMethod($method);
82
        foreach ($m->getParameters() as $param) {
83
            $value = isset($args[$param->getPosition()]) ? $args[$param->getPosition()] : null;
84
            $name = $param->getName();
0 ignored issues
show
Bug introduced by
Consider using $param->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
85
            if ($value === null && $param->isOptional()) {
86
                if ($param->isDefaultValueAvailable() && ($new_value = $param->getDefaultValue()) !== null) {
87
                    $value = $new_value;
88
                } else if (isset($this->defaultParams[$name])) {
89
                    $value = $this->defaultParams[$name];
90
                }
91
            }
92
            if ($value === null) {
93
                continue;
94
            }
95
            $params[$name] = $value;
96
        }
97
        return $params;
98
    }
99
    
100
    /**
101
     * @see http://babelnet.org/guide#RetrieveBabelNetversion
102
     * @throws \Exception
103
     * @return string
104
     */
105
    public function getVersion()
106
    {
107
        $res = $this->exec('getVersion');
108
        return isset($res['version']) ? $res['version'] : false;
109
    }
110
    
111
    /**
112
     * @see http://babelnet.org/guide#RetrievetheIDsoftheBabelsynsets(concepts)denotedbyagivenword
113
     * @param string $word
114
     * @param string $lang
115
     * @param string $filterLangs
116
     * @param string $pos
117
     * @param string $source
118
     * @param string $normalizer
119
     * @throws \Exception
120
     * @return string[]
121
     */
122
    public function getSynsetIds($word, $lang = null, $filterLangs = null, $pos = null, $source = null, $normalizer = null)
0 ignored issues
show
Unused Code introduced by
The parameter $word is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $lang is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $filterLangs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pos is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $source is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $normalizer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
123
    {
124
        return array_column($this->exec('getSynsetIds', $this->getParamsByArguments(__METHOD__, func_get_args())), 'id');
125
    }
126
    
127
    /**
128
     * @see http://babelnet.org/guide#Retrievetheinformationofagivensynset
129
     * @param string $id
130
     * @param string $filterLangs
131
     * @throws \Exception
132
     * @return array
133
     */
134
    public function getSynsetById($id, $filterLangs = null)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $filterLangs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
135
    {
136
        return $this->exec('getSynset', $this->getParamsByArguments(__METHOD__, func_get_args()));
137
    }
138
    
139
    /**
140
     * @see http://babelnet.org/guide#RetrievetheIDsoftheBabelsynsets(concepts)denotedbyagivenword
141
     * @param string $word
142
     * @param string $lang
143
     * @param string $filterLangs
144
     * @param string $pos
145
     * @param string $source
146
     * @param string $normalizer
147
     * @throws \Exception
148
     * @return array
149
     */
150
    public function getSynsets($word, $lang = null, $filterLangs = null, $pos = null, $source = null, $normalizer = null)
151
    {
152
        $synsets = [];
153
        foreach ($this->getSynsetIds($word, $lang, $filterLangs, $pos, $source, $normalizer) as $synset_id) {
154
            $synsets[] = $this->getSynsetById($synset_id, $filterLangs);
155
        }
156
        return $synsets;
157
    }
158
    
159
    /**
160
     * @see http://babelnet.org/guide#Retrievethesensesofagivenword
161
     * @param string $word
162
     * @param string $lang
163
     * @param string $filterLangs
164
     * @param string $pos
165
     * @param string $source
166
     * @param string $normalizer
167
     * @throws \Exception
168
     * @return array
169
     */
170
    public function getSenses($word, $lang = null, $filterLangs = null, $pos = null, $source = null, $normalizer = null)
0 ignored issues
show
Unused Code introduced by
The parameter $word is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $lang is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $filterLangs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pos is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $source is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $normalizer is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
171
    {
172
        return $this->exec('getSenses', $this->getParamsByArguments(__METHOD__, func_get_args()));
173
    }
174
    
175
    /**
176
     * @see http://babelnet.org/guide#RetrieveedgesofagivenBabelNetsynset6
177
     * @param string $id
178
     * @throws \Exception
179
     * @return array
180
     */
181
    public function getEdges($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
182
    {
183
        return $this->exec('getEdges', $this->getParamsByArguments(__METHOD__, func_get_args()));
184
    }
185
}
186