-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
313 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.idea | ||
vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
language: php | ||
|
||
php: | ||
- 5.4 | ||
- 5.5 | ||
|
||
before_script: | ||
- composer self-update | ||
- composer install --no-interaction --prefer-source --dev | ||
- ./vendor/bin/parallel-lint -e php,phpt --exclude vendor . | ||
|
||
script: VERBOSE=true ./vendor/bin/tester -c ./tests/php.ini-unix -j 40 ./tests |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,29 @@ | ||
{ | ||
"name": "ondrs/hi", | ||
"type": "library", | ||
"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": [{ | ||
"name": "Ondřej Plšek", | ||
"homepage": "http://ondraplsek.cz", | ||
"role": "Developer" | ||
}], | ||
"require": { | ||
"php": ">=5.4.0" | ||
}, | ||
"autoload": { | ||
"classmap": ["src/"] | ||
"name": "ondrs/hi", | ||
"type": "library", | ||
"description": "Czech names and surnames greeting generator API PHP wrapper", | ||
"keywords": ["czech", "greeting", "generator", "name", "surname", "php", "api", "wrapper"], | ||
"homepage": "http://ondraplsek.cz", | ||
"license": ["MIT"], | ||
"authors": [ | ||
{ | ||
"name": "Ondřej Plšek", | ||
"homepage": "http://ondraplsek.cz", | ||
"role": "Developer" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.4.0", | ||
"kdyby/curl": "2.2.0", | ||
"nette/caching": "2.2.*@dev", | ||
"nette/utils": "2.2.*@dev" | ||
}, | ||
"require-dev": { | ||
"nette/tester": "1.2.*", | ||
"janmarek/mockista": "dev-master", | ||
"jakub-onderka/php-parallel-lint": "0.*" | ||
}, | ||
"autoload": { | ||
"classmap": ["src/"] | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
<?php | ||
/** | ||
* Created by JetBrains PhpStorm. | ||
* User: ondrs | ||
* Date: 13.10.13 | ||
* Time: 18:53 | ||
* To change this template use File | Settings | File Templates. | ||
*/ | ||
|
||
namespace ondrs\Hi; | ||
|
||
|
||
use Kdyby\Curl\CurlException; | ||
use Kdyby\Curl\CurlSender; | ||
use Kdyby\Curl\Request; | ||
use Nette\Caching\Cache; | ||
use Nette\Caching\Storages\FileStorage; | ||
use Nette\Utils\FileSystem; | ||
use Nette\Utils\Json; | ||
use Nette\Utils\JsonException; | ||
use Nette\Utils\Strings; | ||
|
||
class Hi | ||
{ | ||
|
||
/** @var \Nette\Caching\Cache */ | ||
private $cache; | ||
|
||
/** @var CurlSender */ | ||
private $curlSender; | ||
|
||
/** @var null|string */ | ||
private $type; | ||
|
||
const | ||
API_URL = 'http://hi.ondraplsek.cz', | ||
TYPE_NAME = 'name', | ||
TYPE_SURNAME = 'surname', | ||
|
||
GENDER_MALE = 'male', | ||
GENDER_FEMALE = 'female'; | ||
|
||
|
||
/** | ||
* @param $cacheDir | ||
* @param CurlSender|NULL $curlSender | ||
*/ | ||
public function __construct($cacheDir, CurlSender $curlSender = NULL) | ||
{ | ||
FileSystem::createDir($cacheDir); | ||
|
||
$storage = new FileStorage($cacheDir); | ||
$this->cache = new Cache($storage); | ||
|
||
if ($curlSender === NULL) { | ||
$this->curlSender = new CurlSender(); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @param null|string $type | ||
*/ | ||
public function setType($type) | ||
{ | ||
$this->type = $type; | ||
} | ||
|
||
|
||
/** | ||
* @return null|string | ||
*/ | ||
public function getType() | ||
{ | ||
return $this->type; | ||
} | ||
|
||
|
||
/** | ||
* @param string $name | ||
* @return bool|string | ||
*/ | ||
public function mr($name) | ||
{ | ||
return $this->to($name, self::GENDER_MALE); | ||
} | ||
|
||
|
||
/** | ||
* @param string $name | ||
* @return bool|string | ||
*/ | ||
public function ms($name) | ||
{ | ||
return $this->to($name, self::GENDER_FEMALE); | ||
} | ||
|
||
|
||
/** | ||
* @param string $name | ||
* @param null|string $gender | ||
* @return bool|string | ||
*/ | ||
public function to($name, $gender = NULL) | ||
{ | ||
$name = Strings::fixEncoding($name); | ||
$name = Strings::trim($name); | ||
$name = Strings::lower($name); | ||
|
||
$url = self::API_URL . '?name=' . urlencode($name); | ||
|
||
if ($this->type !== NULL) { | ||
$url .= '&type=' . urlencode($this->type); | ||
} | ||
|
||
if ($gender !== NULL) { | ||
$url .= '&gender=' . urlencode($gender); | ||
} | ||
|
||
return $this->cache->load($url, function ($dependencies) use ($url) { | ||
|
||
$data = $this->fetchUrl($url); | ||
$json = $this->parseJson($data); | ||
|
||
$result = $json->success ? $json->results[0] : FALSE; | ||
|
||
$this->cache->save($url, $result, $dependencies); | ||
|
||
return $result; | ||
}); | ||
} | ||
|
||
|
||
/** | ||
* @param string $url | ||
* @return \stdClass | ||
* @throws Exception | ||
*/ | ||
public function fetchUrl($url) | ||
{ | ||
try { | ||
$request = new Request($url); | ||
$response = $this->curlSender->send($request); | ||
|
||
return $response->getResponse(); | ||
} catch (CurlException $e) { | ||
throw new Exception($e->getMessage()); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* @param $data | ||
* @return mixed | ||
* @throws Exception | ||
*/ | ||
public function parseJson($data) | ||
{ | ||
try { | ||
return Json::decode($data); | ||
} catch (JsonException $e) { | ||
throw new Exception($e->getMessage()); | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
class Exception extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage.dat | ||
*.actual | ||
*.expected |
Oops, something went wrong.