Currency::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the light/apistore.
5
 *
6
 * (c) lichunqiang <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace light\apistore\apis;
13
14
/**
15
 * 汇率转换.
16
 *
17
 * @see http://apistore.baidu.com/apiworks/servicedetail/119.html
18
 */
19
class Currency extends Api
20
{
21
    /**
22
     * @var string 汇率转换地址
23
     */
24
    private $address = 'http://apis.baidu.com/apistore/currencyservice/currency?';
25
26
    /**
27
     * @var string 币种查询地址
28
     */
29
    private $fetchTypesUrl = 'http://apis.baidu.com/apistore/currencyservice/type?';
30
31
    /**
32
     * 获取汇率转换结果.
33
     *
34
     * @param array $queryParams
35
     *
36
     * ~~~
37
     * $queryParams = [
38
     *     'fromCurrency' => 'CNY', //待转化的币种
39
     *     'toCurrency' => 'USD',   //转化后的币种
40
     *     'amount' => 2            //转化金额
41
     * ];
42
     * ~~~
43
     *
44
     * 或者
45
     *
46
     * ~~~
47
     * $queryParams = ['CNY', 'USD', 2];
48
     * ~~~
49
     *
50
     * @return array
51
     */
52 3
    public function get($queryParams)
53
    {
54 3
        if (!isset($queryParams['fromCurrency'])) {
55 3
            $queryParams = array_combine(['fromCurrency', 'toCurrency', 'amount'], $queryParams);
56 3
        }
57
58 3
        return $this->fetch($this->address . http_build_query($queryParams));
59
    }
60
61 1
    public function getTypes()
62
    {
63 1
        return $this->fetch($this->fetchTypesUrl);
64
    }
65
}
66