summaryrefslogtreecommitdiffstats
path: root/src/PurpleCode/PCurl/PCurlJsonResponse.php
blob: df36ea4c2526c952e6774d8f61606b1cd489667f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php

/**
 * PCurl is a REST client libary for PHP.
 *
 * See http://github.com/purplecode/php.curl for details.
 *
 * This code is licensed for use, modification, and distribution
 * under the terms of the MIT License (see http://en.wikipedia.org/wiki/MIT_License)
 */

namespace PurpleCode\PCurl;

use PurpleCode\PCurl\PCurlResponse;

class PCurlJsonResponse extends PCurlResponse {
  private $parsedResponse;

  public function __construct($header, $body, $arrayResponse){
    if(! PCurlJsonResponse::isValidJson($body))
      throw new PCurlException("Invalid JSON response format");
    parent::__construct($header, json_decode($body, $arrayResponse));
  }

  /**
  * Fast way to check if response body is in JSon format - see RFC4627, regexp part. 
  */
  private static function isValidJson($text){
    return !preg_match('/[^,:{}\\[\\]0-9.\\-+Eaeflnr-u \\n\\r\\t]/', preg_replace('/"(\\.|[^"\\\\])*"/', '', $text));
  }

}