Ouzo\Utilities\Json::safeDecode PHP Méthode

safeDecode() public static méthode

Decodes a JSON string
public static safeDecode ( string $string, boolean $asArray = false ) : mixed
$string string
$asArray boolean
Résultat mixed
    public static function safeDecode($string, $asArray = false)
    {
        if ($string === '' || $string === null) {
            // for PHP 7 compatibility
            $string = json_encode(null);
        }
        return json_decode($string, $asArray);
    }

Usage Example

Exemple #1
0
 /**
  * @test
  */
 public function shouldResetJsonError()
 {
     //given
     Json::safeDecode("error");
     Json::safeDecode("");
     //when
     $error = Json::lastError();
     //then
     $this->assertEquals(JSON_ERROR_NONE, $error);
 }