Skip to content

Commit

Permalink
Moved json_parse to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-konshin committed Aug 5, 2015
1 parent 2743d30 commit dfca766
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 28 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
"autoload": {
"psr-4": {
"RingCentral\\": "lib/"
}
},
"files": [
"src/core/functions.php"
]
}
}
2 changes: 2 additions & 0 deletions src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ function RingCentralAutoLoader($class)

}

require_once('core/functions.php');

spl_autoload_register('RingCentralAutoLoader', true);
38 changes: 38 additions & 0 deletions src/core/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace RingCentral\core;

use InvalidArgumentException;

function json_parse($json, $assoc = false, $depth = 512)
{

$parsed = \json_decode($json, $assoc, $depth);

$error = json_last_error();

switch ($error) {
case JSON_ERROR_NONE:
break;
case JSON_ERROR_DEPTH:
throw new InvalidArgumentException('JSON Error: Maximum stack depth exceeded');
break;
case JSON_ERROR_CTRL_CHAR:
throw new InvalidArgumentException('JSON Error: Unexpected control character found');
break;
case JSON_ERROR_SYNTAX:
throw new InvalidArgumentException('JSON Error: Syntax error, malformed JSON');
break;
default:
throw new InvalidArgumentException('JSON Error: Unknown error');
break;
}

// This is a courtesy by PHP JSON parser to parse "null" into null, but this is an error situation
if (empty($parsed)) {
throw new InvalidArgumentException('JSON Error: Result is empty after parsing');
}

return $parsed;

}
26 changes: 1 addition & 25 deletions src/http/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,38 +86,14 @@ public function getJson($asObject = true)

if (($asObject && empty($this->jsonAsObject)) || (!$asObject && empty($this->jsonAsArray))) {

$json = json_decode((string)$this->response->getBody(), !$asObject);
$json = \RingCentral\core\json_parse((string)$this->response->getBody(), !$asObject);

if ($asObject) {
$this->jsonAsObject = $json;
} else {
$this->jsonAsArray = $json;
}

$error = json_last_error();

switch ($error) {
case JSON_ERROR_NONE:
break;
case JSON_ERROR_DEPTH:
throw new Exception('JSON Error: Maximum stack depth exceeded');
break;
case JSON_ERROR_CTRL_CHAR:
throw new Exception('JSON Error: Unexpected control character found');
break;
case JSON_ERROR_SYNTAX:
throw new Exception('JSON Error: Syntax error, malformed JSON');
break;
default:
throw new Exception('JSON Error: Unknown error');
break;
}

// This is a courtesy by PHP JSON parser to parse "null" into null, but this is an error situation
if (empty($json)) {
throw new Exception('JSON Error: Result is empty after parsing');
}

}

return $asObject ? $this->jsonAsObject : $this->jsonAsArray;
Expand Down
2 changes: 1 addition & 1 deletion src/mocks/SubscriptionMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct($expiresIn = 54000)
public function getResponse(RequestInterface $request)
{

$body = json_decode((string)$request->getBody(), true);
$body = \RingCentral\core\json_parse((string)$request->getBody(), true);

return self::createBody(array(
'eventFilters' => $body['eventFilters'],
Expand Down
2 changes: 1 addition & 1 deletion src/subscription/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public function notify($pubnubMessage)
base64_decode($message),
MCRYPT_MODE_ECB);

$message = json_decode($aes->unPadPKCS7($message, 128), true); // PUBNUB itself always decode as array
$message = \RingCentral\core\json_parse($aes->unPadPKCS7($message, 128), true); // PUBNUB itself always decode as array

}

Expand Down

0 comments on commit dfca766

Please sign in to comment.