Skip to content

Commit

Permalink
Add TextRuApi Class with add method, exceptions and first test
Browse files Browse the repository at this point in the history
  • Loading branch information
krylov123 committed Jul 31, 2017
1 parent 9a345a1 commit c3f5a3b
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
"description": "Text.ru API's php connector",
"require": {
"php": "5.6.* || >=7.0"
},
"require-dev": {
"ext-curl": "*",
"phpunit/phpunit": "^4.0 || ^5.0"
}
}
8 changes: 8 additions & 0 deletions src/Exception/CurlRequestException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace TextRuApi\Exception;

class CurlRequestException extends \Exception
{

}
8 changes: 8 additions & 0 deletions src/Exception/WrongParameterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace TextRuApi\Exception;

class WrongParameterException extends \Exception
{

}
74 changes: 74 additions & 0 deletions src/TextRuApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace TextRuApi;

use TextRuApi\Exception\WrongParameterException;
use TextRuApi\Exception\CurlRequestException;

class TextRuApi
{

private $userkey;

private static $allowed_options_get = ["exceptdomain", "excepturl", "visible", "copying", "callback"];

private function __construct($userkey)
{
$this->userkey = $userkey;
}

public function userkey($userkey = null)
{
if (is_null($userkey)) return $this->userkey;

$this->userkey = $userkey;

return $this;
}

public static function add($userkey, $text, $options = [])
{
if ((empty($userkey)) || (empty($text))) throw new WrongParameterException("Required params is empty", 400123);

if (!is_array($options)) throw new WrongParameterException("Options param must be array", 400124);

foreach ($options as $key => $value) {
if (!in_array($key, $this::$allowed_options_get)) throw new WrongParameterException("Unknown option " . $key . " provided", 400125);
}

$post_options = ["userkey" => $userkey, "text" => $text];
if (!empty($options)) $post_options = array_merge($post_options, $options);

return $this::sendCurl($post_options);

}

public static function sendCurl($postfields, $url = 'http://api.text.ru/post')
{
if (is_array($postfields)) $postfields = http_build_query($postfields, '', '&');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
$answer = curl_exec($ch);
$errno = curl_errno($ch);

$result = [
"error" => ["code" => null, "desc" => null],
"text_uid" => null
];

if ($errno) throw new CurlRequestException(curl_error($ch), $errno);

$answer_decoded = json_decode($answer);

if (isset($answer_decoded->error_code)) $result["error"]["code"] = $answer_decoded->error_code;
if (isset($answer_decoded->error_desc)) $result["error"]["desc"] = $answer_decoded->error_desc;
if (isset($answer_decoded->text_uid)) $result["text_uid"] = $answer_decoded->text_uid;

return $result;

}
}
21 changes: 21 additions & 0 deletions tests/AddMethodTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace TextRuApi\Tests;

require __DIR__ . "/../src/TextRuApi.php";
require __DIR__ . "/../src/Exception/CurlRequestException.php";
require __DIR__ . "/../src/Exception/WrongParameterException.php";

use TextRuApi\Exception\WrongParameterException;
use TextRuApi\TextRuApi;


class AddMethodTest extends \PHPUnit_Framework_TestCase
{
public function test_empty_userkey_rise_exception()
{
$this->expectException(WrongParameterException::class);
$this->expectExceptionCode(400123);
TextRuApi::add("","Test text");
}
}

0 comments on commit c3f5a3b

Please sign in to comment.