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