1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sunlight-php |
4
|
|
|
* |
5
|
|
|
* PHP Version 7.0.1 |
6
|
|
|
* |
7
|
|
|
* @category Sunlight_Foundation_API |
8
|
|
|
* @package ContactMyReps |
9
|
|
|
* @author edfialk <[email protected]> |
10
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
11
|
|
|
* @link https://github.com/contactmyreps/sunlight-php |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace ContactMyReps\Sunlight\api; |
15
|
|
|
|
16
|
|
|
use ContactMyReps\Sunlight\api\BaseAPI; |
17
|
|
|
use GuzzleHttp\Client; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Sunlight Foundation OpenStates API wrapper |
21
|
|
|
* For more information see https://sunlightlabs.github.io/openstates-api |
22
|
|
|
* |
23
|
|
|
* @category Sunlight_Foundation_API |
24
|
|
|
* @package ContactMyReps |
25
|
|
|
* @author edfialk <[email protected]> |
26
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
27
|
|
|
* @link https://github.com/contactmyreps/sunlight-php |
28
|
|
|
*/ |
29
|
|
|
class OpenStates extends BaseAPI |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
const BASE_URI = 'http://openstates.org/api/v1/'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create OpenStates API |
36
|
|
|
* |
37
|
|
|
* @param array $options api options (json, sync, etc.) |
38
|
|
|
*/ |
39
|
|
|
public function __construct($options = []) |
40
|
|
|
{ |
41
|
|
|
parent::__construct($options); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create Guzzle Client |
46
|
|
|
* |
47
|
|
|
* @param string $key Sunlight Foundation API key |
48
|
|
|
* |
49
|
|
|
* @return null |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function setClient($key) |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this->client = new Client( |
|
|
|
|
54
|
|
|
[ |
55
|
|
|
'base_uri' => self::BASE_URI, |
56
|
|
|
'headers' => [ |
57
|
|
|
'X-APIKEY' => $key |
58
|
|
|
] |
59
|
|
|
] |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Return Guzzle Client |
65
|
|
|
* |
66
|
|
|
* @return GuzzleHTTP/Client OpenStates Guzzle Client |
|
|
|
|
67
|
|
|
*/ |
68
|
|
|
public function getClient() |
69
|
|
|
{ |
70
|
|
|
return $this->client; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Search OpenStates Legislators |
75
|
|
|
* |
76
|
|
|
* @param array $search search fields |
77
|
|
|
* @param array $fields display fields |
78
|
|
|
* |
79
|
|
|
* @return Response Guzzle Response |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function legislators($search = [], $fields = []) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$url = 'legislators/?'; |
84
|
|
|
if (count($search) > 0) { |
85
|
|
|
$p = []; |
86
|
|
|
foreach ($search as $k => $v) { |
87
|
|
|
$p[] = "$k=$v"; |
88
|
|
|
} |
89
|
|
|
$url .= implode("&", $p); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->get($url, $fields); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Lookup legislators that serve districts containing a given point |
97
|
|
|
* |
98
|
|
|
* @param float $lat Latitude |
99
|
|
|
* @param float $lng Longitude |
100
|
|
|
* @param array $fields display fields |
101
|
|
|
* |
102
|
|
|
* @return Response Guzzle Response |
103
|
|
|
*/ |
104
|
|
|
public function geoLookup($lat, $lng, $fields = []) |
105
|
|
|
{ |
106
|
|
|
$url = 'legislators/geo/?lat='.$lat.'&long='.$lng; |
107
|
|
|
|
108
|
|
|
return $this->get($url, $fields); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
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.