1 | <?php |
||
2 | |||
3 | namespace Pixelpeter\Genderize; |
||
4 | |||
5 | use Pixelpeter\Genderize\Models\GenderizeResponse; |
||
6 | use Unirest\Request; |
||
7 | |||
8 | class GenderizeClient |
||
9 | { |
||
10 | /** |
||
11 | * @var array $names Holds one or more names to check |
||
12 | */ |
||
13 | protected $names; |
||
14 | |||
15 | /** |
||
16 | * @var string $lang The language used for the check |
||
17 | */ |
||
18 | protected $lang; |
||
19 | |||
20 | /** |
||
21 | * @var string $country The country used for the check |
||
22 | */ |
||
23 | protected $country; |
||
24 | |||
25 | /** |
||
26 | * @var string $apikey The api key used for the request |
||
27 | */ |
||
28 | protected $apikey; |
||
29 | |||
30 | /** |
||
31 | * @var \Unirest\Request |
||
32 | */ |
||
33 | protected $request; |
||
34 | |||
35 | /** |
||
36 | * @const API_URL The URL of the api endpoint |
||
37 | */ |
||
38 | const API_URL = 'https://api.genderize.io'; |
||
39 | |||
40 | |||
41 | /** |
||
42 | * Create new instance of Pixelpeter\Genderize\GenderizeClient |
||
43 | */ |
||
44 | public function __construct(Request $request) |
||
45 | { |
||
46 | $this->apikey = config('genderize.apikey'); |
||
47 | $this->request = $request; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Fluent setter for names given as string |
||
52 | * |
||
53 | * @param string $name |
||
54 | * @return $this|GenderizeClient |
||
55 | */ |
||
56 | public function name($name = '') |
||
57 | { |
||
58 | if (is_array($name)) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
59 | return $this->names($name); |
||
60 | } |
||
61 | |||
62 | $this->names = [$name]; |
||
63 | |||
64 | return $this; |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Fluent setter for names given as array |
||
69 | * |
||
70 | * @param array $names |
||
71 | * @return $this |
||
72 | */ |
||
73 | public function names(array $names) |
||
74 | { |
||
75 | $this->names = $names; |
||
76 | |||
77 | return $this; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Fluent setter for language |
||
82 | * |
||
83 | * @param null $lang |
||
0 ignored issues
–
show
|
|||
84 | * @return $this |
||
85 | */ |
||
86 | public function lang($lang = null) |
||
87 | { |
||
88 | $this->lang = $lang; |
||
89 | |||
90 | return $this; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Fluent setter for country |
||
95 | * |
||
96 | * @param null $country |
||
0 ignored issues
–
show
|
|||
97 | * @return $this |
||
98 | */ |
||
99 | public function country($country = null) |
||
100 | { |
||
101 | $this->country = $country; |
||
102 | |||
103 | return $this; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Send the request |
||
108 | * |
||
109 | * @return GenderizeResponse |
||
110 | */ |
||
111 | public function get() |
||
112 | { |
||
113 | $headers = array('Accept' => 'application/json'); |
||
114 | |||
115 | $response = $this->request->get(self::API_URL, $headers, $this->buildQuery()); |
||
116 | |||
117 | return new GenderizeResponse($response); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Build the query |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | protected function buildQuery() |
||
126 | { |
||
127 | $query = [ |
||
128 | 'name' => $this->names, |
||
129 | 'country_id' => $this->country, |
||
130 | 'language_id' => $this->lang, |
||
131 | 'apikey' => $this->apikey |
||
132 | ]; |
||
133 | |||
134 | return array_filter($query); |
||
135 | } |
||
136 | } |
||
137 |