Skip to content

Commit

Permalink
const
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrs committed Oct 13, 2013
1 parent f6462f3 commit dbdbae7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 33 deletions.
4 changes: 2 additions & 2 deletions composer.json
@@ -1,8 +1,8 @@
{
"name": "ondrs/hi",
"type": "library",
"description": "Czech names and surnames greeting generator PHP SDK",
"keywords": ["czech", "greeting", "generator", "name", "surname", "sdk"],
"description": "Czech names and surnames greeting generator API PHP wrapper",
"keywords": ["czech", "greeting", "generator", "name", "surname", "php", "api", "wrapper"],
"homepage": "http://ondraplsek.cz",
"license": ["BSD-3", "GPL-2.0", "GPL-3.0"],
"authors": [{
Expand Down
6 changes: 3 additions & 3 deletions readme.md
@@ -1,7 +1,7 @@
Hi
==============

Czech names and surnames greeting generator PHP SDK.
Czech names and surnames greeting generator API PHP wrapper.
Service is available at the url http://hi.ondraplsek.cz


Expand All @@ -14,10 +14,10 @@ composer.json
"ondrs/hi": "dev-master"


Create new Hi instance. You can spacify if you are looking for a name or a surname. The constructor parameter is optional.
Create a new Hi instance. You can spacify if you are looking for a name or a surname. The constructor parameter is optional.
So if you are not sure, leave it blank.

$hi = new Hi('surname');
$hi = new Hi(Hi::TYPE_SURNAME);

And get your greeting

Expand Down
38 changes: 10 additions & 28 deletions src/Hi.php
Expand Up @@ -16,9 +16,15 @@ class Hi
/** @var string */
private $url = 'http://hi.ondraplsek.cz';

/** @var null|string */
/** @var null|string */
private $type;

const TYPE_NAME = 'name';
const TYPE_SURNAME = 'surname';

const SEX_MALE = 'male';
const SEX_FEMALE = 'female';


/**
* @param null|string $type
Expand Down Expand Up @@ -49,8 +55,8 @@ public function getType()

/**
* @param string $name
* @param string $sex
* @return bool|\stdClass
* @param null|string $sex
* @return bool|string
*/
public function getGreeting($name, $sex = NULL)
{
Expand All @@ -59,10 +65,7 @@ public function getGreeting($name, $sex = NULL)
if($this->type !== NULL)
$url .= '&type=' . urlencode($this->type);

if($sex === NULL)
$sex = $this->detectSex($name);

if($sex)
if($sex !== NULL)
$url .= '&sex=' . urlencode($sex);

$json = $this->fetchUrl($url);
Expand All @@ -74,27 +77,6 @@ public function getGreeting($name, $sex = NULL)
}


/**
* @param $name
* @return bool|string
* @throws Exception
*/
public function detectSex($name)
{
$url = $this->url . '?name=' . urlencode($name);

if($this->type !== NULL)
$url .= '&type=' . urlencode($this->type);

$json = $this->fetchUrl($url);

if($json->success)
return $json->results[0]->sex;
else
return FALSE;
}


/**
* @param string $url
* @return \stdClass
Expand Down

0 comments on commit dbdbae7

Please sign in to comment.