Skip to content

Commit fc5dcc1

Browse files
committed
Add TextRuApi get method and tests
1 parent c3f5a3b commit fc5dcc1

File tree

2 files changed

+53
-14
lines changed

2 files changed

+53
-14
lines changed

src/TextRuApi.php

+41-14
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,49 @@ public static function add($userkey, $text, $options = [])
3939
$post_options = ["userkey" => $userkey, "text" => $text];
4040
if (!empty($options)) $post_options = array_merge($post_options, $options);
4141

42-
return $this::sendCurl($post_options);
42+
$answer_decoded = self::sendCurl($post_options);
43+
44+
$result = [
45+
"error" => ["code" => null, "desc" => null],
46+
"text_uid" => null
47+
];
48+
49+
if (isset($answer_decoded->error_code)) $result["error"]["code"] = $answer_decoded->error_code;
50+
if (isset($answer_decoded->error_desc)) $result["error"]["desc"] = $answer_decoded->error_desc;
51+
if (isset($answer_decoded->text_uid)) $result["text_uid"] = $answer_decoded->text_uid;
52+
53+
return $result;
4354

4455
}
4556

57+
public static function get($userkey, $uid, $jsonvisible = null)
58+
{
59+
if ((empty($userkey)) || (empty($uid))) throw new WrongParameterException("Required params is empty", 400131);
60+
61+
$post_options = ["userkey" => $userkey, "uid" => $uid];
62+
if (!is_null($jsonvisible)) $post_options["jsonvisible"] = "detail";
63+
64+
$answer_decoded = self::sendCurl($post_options);
65+
66+
$result = [
67+
"error" => ["code" => null, "desc" => null],
68+
"text_unique" => null,
69+
"result_json" => null,
70+
"spell_check" => null,
71+
"seo_check" => null
72+
];
73+
74+
if (isset($answer_decoded->error_code)) $result["error"]["code"] = $answer_decoded->error_code;
75+
if (isset($answer_decoded->error_desc)) $result["error"]["desc"] = $answer_decoded->error_desc;
76+
77+
if (isset($answer_decoded->text_unique)) $result["text_unique"] = $answer_decoded->text_unique;
78+
if (isset($answer_decoded->result_json)) $result["result_json"] = $answer_decoded->result_json;
79+
if (isset($answer_decoded->spell_check)) $result["spell_check"] = $answer_decoded->spell_check;
80+
if (isset($answer_decoded->seo_check)) $result["seo_check"] = $answer_decoded->seo_check;
81+
82+
return $result;
83+
}
84+
4685
public static function sendCurl($postfields, $url = 'http://api.text.ru/post')
4786
{
4887
if (is_array($postfields)) $postfields = http_build_query($postfields, '', '&');
@@ -55,20 +94,8 @@ public static function sendCurl($postfields, $url = 'http://api.text.ru/post')
5594
$answer = curl_exec($ch);
5695
$errno = curl_errno($ch);
5796

58-
$result = [
59-
"error" => ["code" => null, "desc" => null],
60-
"text_uid" => null
61-
];
62-
6397
if ($errno) throw new CurlRequestException(curl_error($ch), $errno);
6498

65-
$answer_decoded = json_decode($answer);
66-
67-
if (isset($answer_decoded->error_code)) $result["error"]["code"] = $answer_decoded->error_code;
68-
if (isset($answer_decoded->error_desc)) $result["error"]["desc"] = $answer_decoded->error_desc;
69-
if (isset($answer_decoded->text_uid)) $result["text_uid"] = $answer_decoded->text_uid;
70-
71-
return $result;
72-
99+
return json_decode($answer);
73100
}
74101
}

tests/AddMethodTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,16 @@ public function test_empty_userkey_rise_exception()
1818
$this->expectExceptionCode(400123);
1919
TextRuApi::add("","Test text");
2020
}
21+
22+
public function test_too_short_text()
23+
{
24+
$result = TextRuApi::add("afldkfjlas","Short test text");
25+
$this->assertEquals($result["error"]["code"],112);
26+
}
27+
28+
public function test_wrong_userkey()
29+
{
30+
$result = TextRuApi::add("php_unit_test","Test test test test test test test test test test test test test test test test test test test test test");
31+
$this->assertEquals($result["error"]["code"],140);
32+
}
2133
}

0 commit comments

Comments
 (0)