Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions src/Base/Recognizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class Recognizer {
protected $_supported_countries = null;
protected $_supported_languages = null;

public function __construct($names = null, $country_id = null, $language_id = null, $api_key = null) {
public function __construct($names = null, $country_id = null, $language_id = null, $api_key = null)
{
$this->set_names($names)
->set_country_id($country_id)
->set_language_id($language_id)
Expand All @@ -37,8 +38,8 @@ public function __construct($names = null, $country_id = null, $language_id = nu
* @return \Genderize\Base\Name
* @throws \Genderize\Exception\NullResponseException
*/
public function recognize($return_as_object = true) {

public function recognize($return_as_object = true)
{
$url = $this->_build_url();
$response = json_decode(file_get_contents($url), true);
if (is_null($response)) {
Expand All @@ -49,7 +50,7 @@ public function recognize($return_as_object = true) {
//If an array of names were passed in, return an array of Name objects
if (is_array($this->get_names())) {
$nameObjects = array();
foreach($response as $nameresponse) {
foreach ($response as $nameresponse) {
$nameObj = new Name();

foreach (['name', 'gender', 'probability', 'count', 'country_id'] as $field) {
Expand All @@ -61,6 +62,7 @@ public function recognize($return_as_object = true) {

$nameObjects[] = $nameObj;
}

return $nameObjects;
} else {
//DEPRECATED: if a single name was passed in, not as part of an array, return a single Name object
Expand All @@ -79,12 +81,18 @@ public function recognize($return_as_object = true) {
//If an array of names were passed in, return an array of name=>gender responses
if (is_array($this->get_names())) {
$genders = array();
foreach($response as $nameresponse) {
foreach ($response as $nameresponse) {
if (isset($nameresponse['name']) && $nameresponse['name']) {
$genderresponse = array('name' => $nameresponse['name']);
if (isset($nameresponse['gender'])) {
$genderresponse['gender'] = $nameresponse['gender'];
}
if (isset($nameresponse['probability'])) {
$genderresponse['probability'] = $nameresponse['probability'];
}
if (isset($nameresponse['count'])) {
$genderresponse['count'] = $nameresponse['count'];
}
$genders[] = $genderresponse;
}
}
Expand All @@ -100,8 +108,8 @@ public function recognize($return_as_object = true) {
* Builds the valid genderize.io API url.
* @return string
*/
protected function _build_url() {

protected function _build_url()
{
$params = array_filter([
'name' => $this->_names,
'country_id' => $this->_country_id,
Expand All @@ -113,6 +121,7 @@ protected function _build_url() {
return $v;
}
});

//Only pass apikey if there is one set
if ($api_key = $this->get_api_key()) {
$params['apikey'] = trim($api_key);
Expand All @@ -127,7 +136,8 @@ protected function _build_url() {
* @param string $country_id
* @return boolean
*/
protected function is_country_supported($country_id) {
protected function is_country_supported($country_id)
{
if (is_null($this->_supported_countries)) {
$Countries = new Countries();
$this->_supported_countries = $Countries->get();
Expand All @@ -142,60 +152,72 @@ protected function is_country_supported($country_id) {
* @param string $language_id
* @return boolean
*/
protected function is_language_supported($language_id) {
protected function is_language_supported($language_id)
{
if (is_null($this->_supported_languages)) {
$Languages = new Languages();
$this->_supported_languages = $Languages->get();
}

return in_array($language_id, $this->_supported_languages);
}

// <editor-fold desc="Setters and getters">

public function set_country_id($country_id, $check_if_valid = true) {
public function set_country_id($country_id, $check_if_valid = true)
{
$country_id = strtoupper(trim($country_id));
if ($check_if_valid) {
if (!empty($country_id) && !$this->is_country_supported($country_id)) {
throw new CountryNotSupportedException('Country ' . $country_id . ' is not supported');
}
}
$this->_country_id = $country_id;

return $this;
}

public function get_country_id() {
public function get_country_id()
{
return $this->_country_id;
}

public function set_language_id($language_id, $check_if_valid = true) {
public function set_language_id($language_id, $check_if_valid = true)
{
$language_id = strtolower(trim($language_id));
if ($check_if_valid) {
if (!empty($language_id) && !$this->is_language_supported($language_id)) {
throw new CountryNotSupportedException('Language ' . $language_id . ' is not supported');
}
}
$this->_language_id = $language_id;

return $this;
}

public function get_language_id() {
public function get_language_id()
{
return $this->_language_id;
}

public function set_names($names) {
public function set_names($names)
{
$this->_names = $names;
return $this;
}

public function get_names() {
public function get_names()
{
return $this->_names;
}

public function set_api_key($api_key) {
public function set_api_key($api_key)
{
$this->_api_key = $api_key;
}

public function get_api_key() {
public function get_api_key()
{
return $this->_api_key;
}

Expand Down