Exemplo n.º 1
0
 /**
  * Wrapper around json_decode to validate string first.
  * @param $data
  * @return mixed
  */
 public static function jsonDecode($data)
 {
     JsonValidator::validate($data);
     return json_decode($data, true);
 }
Exemplo n.º 2
0
 /**
  * Returns a list of Object from Array or Json String. It is generally used when your json
  * contains an array of this object
  *
  * @param mixed $data Array object or json string representation
  * @return array
  */
 public static function getList($data)
 {
     // Return Null if Null
     if ($data === null) {
         return null;
     }
     if (is_a($data, get_class(new \stdClass()))) {
         //This means, root element is object
         $accessibleProperties = get_object_vars($data);
         if (count($accessibleProperties) == 1 && (isset($accessibleProperties['error']) || isset($accessibleProperties['errors']))) {
             // Object is only an error {"error":"message"} or {"errors":["error1":"message1"]}
             return new Error(JsonConverter::encode($data));
         } else {
             return new static(JsonConverter::encode($data));
         }
     }
     $list = array();
     if (is_array($data)) {
         $data = JsonConverter::encode($data);
     }
     if (JsonValidator::validate($data)) {
         // It is valid JSON
         $decoded = JsonConverter::decode($data);
         if ($decoded === null) {
             return $list;
         }
         if (is_array($decoded)) {
             foreach ($decoded as $k => $v) {
                 $list[] = self::getList($v);
             }
         }
         if (is_a($decoded, get_class(new \stdClass()))) {
             //This means, root element is object
             $list[] = new static(JsonConverter::encode($decoded));
         }
     }
     return $list;
 }
Exemplo n.º 3
0
 /**
  * A pull method which would read the persisted data based on clientId.
  * If clientId is not provided, an array with all the tokens would be passed.
  *
  * @param array|null $config
  * @param string $clientId
  * @return mixed|null
  */
 public static function pull($config = null, $clientId = null)
 {
     // Return if not enabled
     if (!self::isEnabled($config)) {
         return null;
     }
     $tokens = null;
     $cachePath = self::cachePath($config);
     if (file_exists($cachePath)) {
         // Read from the file
         $cachedToken = file_get_contents($cachePath);
         if ($cachedToken && JsonValidator::validate($cachedToken, true)) {
             $tokens = json_decode($cachedToken, true);
             if ($clientId && is_array($tokens) && array_key_exists($clientId, $tokens)) {
                 // If client Id is found, just send in that data only
                 return $tokens[$clientId];
             } else {
                 if ($clientId) {
                     // If client Id is provided, but no key in persisted data found matching it.
                     return null;
                 }
             }
         }
     }
     return $tokens;
 }
Exemplo n.º 4
0
 protected static function printObject($object, $error = null)
 {
     if ($error) {
         echo '<p class="error"><i class="fa fa-exclamation-triangle"></i> ' . $error . '</p>';
     }
     if ($object) {
         if (is_a($object, 'BlockCypher\\Common\\BlockCypherModel')) {
             /** @var $object \BlockCypher\Common\BlockCypherModel */
             echo '<pre class="prettyprint ' . ($error ? 'error' : '') . '">' . $object->toJSON(128) . "</pre>";
         } elseif (is_string($object) && \BlockCypher\Validation\JsonValidator::validate($object, true)) {
             echo '<pre class="prettyprint ' . ($error ? 'error' : '') . '">' . str_replace('\\/', '/', json_encode(json_decode($object), 128)) . "</pre>";
         } elseif (is_string($object)) {
             echo '<pre class="prettyprint ' . ($error ? 'error' : '') . '">' . $object . '</pre>';
         } elseif (is_array($object)) {
             foreach ($object as $item) {
                 self::printObject($item, $error);
             }
         } else {
             echo "<pre>";
             print_r($object);
             echo "</pre>";
         }
     } else {
         echo "<span>No Data</span>";
     }
 }
Exemplo n.º 5
0
 /**
  *
  * @dataProvider invalidProvider
  */
 public function testInvalidJsonSilent($input)
 {
     $this->assertFalse(JsonValidator::validate($input, true));
 }