Skip to content

Commit

Permalink
Stricter checking of success and error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
rowan-m committed Mar 16, 2015
1 parent e743b73 commit 0905859
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/ReCaptcha/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,21 @@ class Response
*/
public static function fromJson($json)
{
$answers = json_decode($json, true);
$responseData = json_decode($json, true);

if (!$answers) {
if (!$responseData) {
return new Response(false, array('invalid-json'));
}

if (trim($answers['success']) == true) {
if (isset($responseData['success']) && $responseData['success'] == true) {
return new Response(true);
}

return new Response(false, $answers['error-codes']);
if (isset($responseData['error-codes']) && is_array($responseData['error-codes'])) {
return new Response(false, $responseData['error-codes']);
}

return new Response(false);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/ReCaptcha/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function provideJson()
array('{"success": true}', true, array()),
array('{"success": false, "error-codes": ["test"]}', false, array('test')),
array('{"success": true, "error-codes": ["test"]}', true, array()),
array('{"success": false}', false, array()),
array('BAD JSON', false, array('invalid-json')),
);
}
Expand Down

0 comments on commit 0905859

Please sign in to comment.