PrivateBin\Json::encode PHP Метод

encode() публичный статический Метод

Returns a string containing the JSON representation of the given input
public static encode ( mixed $input ) : string
$input mixed
Результат string
    public static function encode($input)
    {
        $jsonString = json_encode($input);
        $errorCode = json_last_error();
        if ($errorCode === JSON_ERROR_NONE) {
            return $jsonString;
        }
        $message = 'A JSON error occurred';
        if (function_exists('json_last_error_msg')) {
            $message .= ': ' . json_last_error_msg();
        }
        $message .= ' (' . $errorCode . ')';
        throw new Exception($message, 90);
    }

Usage Example

Пример #1
0
 /**
  * Create a comment in a paste.
  *
  * @access public
  * @param  string $pasteid
  * @param  string $parentid
  * @param  string $commentid
  * @param  array  $comment
  * @throws Exception
  * @return bool
  */
 public function createComment($pasteid, $parentid, $commentid, $comment)
 {
     $storagedir = self::_dataid2discussionpath($pasteid);
     $filename = $pasteid . '.' . $commentid . '.' . $parentid;
     if (is_file($storagedir . $filename)) {
         return false;
     }
     if (!is_dir($storagedir)) {
         mkdir($storagedir, 0700, true);
     }
     return (bool) file_put_contents($storagedir . $filename, Json::encode($comment));
 }