1 <?php
 2 
 3 namespace PayPal\Validation;
 4 
 5 /**
 6  * Class JsonValidator
 7  *
 8  * @package PayPal\Validation
 9  */
10 class JsonValidator
11 {
12 
13     /**
14      * Helper method for validating if string provided is a valid json.
15      *
16      * @param string $string String representation of Json object
17      * @param bool $silent Flag to not throw \InvalidArgumentException
18      * @return bool
19      */
20     public static function validate($string, $silent = false)
21     {
22         @json_decode($string);
23         if (json_last_error() != JSON_ERROR_NONE) {
24             if ($string === '' || $string === null) {
25                 return true;
26             }
27             if ($silent == false) {
28                 //Throw an Exception for string or array
29                 throw new \InvalidArgumentException("Invalid JSON String");
30             }
31             return false;
32         }
33         return true;
34     }
35 }
36