Find this useful? Enter your email to receive occasional updates for securing PHP code.

Signing you up...

Thank you for signing up!

PHP Decode

<?php /** * This file is part of PhpRedisJSON library * * @project phpredis-json * @..

Decoded Output download

<?php
/**
 * This file is part of PhpRedisJSON library
 *
 * @project   phpredis-json
 * @author    Rafael Campoy <[email protected]>
 * @copyright 2019 Rafael Campoy <[email protected]>
 * @license   MIT
 * @link      https://github.com/averias/phpredis-json
 *
 * Copyright and license information, is included in
 * the LICENSE file that is distributed with this source code.
 */

namespace Averias\RedisJson\Encoder\Traits;

use Averias\RedisJson\Exception\ResponseException;

trait JsonEncoderTrait
{
    /**
     * @param $value
     * @return string
     * @throws ResponseException
     */
    public function encode($value): string
    {
        // @TODO: when update to PHP 7.3+ wrap it with a try...catch for capturing \JsonException
        // as explained here https://ayesh.me/Upgrade-PHP-7.3#json-exceptions
        $jsonEncodedValue = json_encode($value);
        if (false === $jsonEncodedValue || json_last_error() !== JSON_ERROR_NONE) {
            throw new ResponseException(
                sprintf('value could not be json-encoded properly, reason: %s', json_last_error_msg())
            );
        }

        return $jsonEncodedValue;
    }

    /**
     * @param string $json
     * @return mixed
     * @throws ResponseException
     */
    public function decode(string $json)
    {
        // @TODO: when update to PHP 7.3+ wrap it with a try...catch for capturing \JsonException
        // as explained here https://ayesh.me/Upgrade-PHP-7.3#json-exceptions
        $decodedValue = json_decode($json, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new ResponseException(
                sprintf('"%s" could not be json-decoded properly, reason: %s', $json, json_last_error_msg())
            );
        }

        return $decodedValue;
    }
}
 ?>

Did this file decode correctly?

Original Code

<?php
/**
 * This file is part of PhpRedisJSON library
 *
 * @project   phpredis-json
 * @author    Rafael Campoy <[email protected]>
 * @copyright 2019 Rafael Campoy <[email protected]>
 * @license   MIT
 * @link      https://github.com/averias/phpredis-json
 *
 * Copyright and license information, is included in
 * the LICENSE file that is distributed with this source code.
 */

namespace Averias\RedisJson\Encoder\Traits;

use Averias\RedisJson\Exception\ResponseException;

trait JsonEncoderTrait
{
    /**
     * @param $value
     * @return string
     * @throws ResponseException
     */
    public function encode($value): string
    {
        // @TODO: when update to PHP 7.3+ wrap it with a try...catch for capturing \JsonException
        // as explained here https://ayesh.me/Upgrade-PHP-7.3#json-exceptions
        $jsonEncodedValue = json_encode($value);
        if (false === $jsonEncodedValue || json_last_error() !== JSON_ERROR_NONE) {
            throw new ResponseException(
                sprintf('value could not be json-encoded properly, reason: %s', json_last_error_msg())
            );
        }

        return $jsonEncodedValue;
    }

    /**
     * @param string $json
     * @return mixed
     * @throws ResponseException
     */
    public function decode(string $json)
    {
        // @TODO: when update to PHP 7.3+ wrap it with a try...catch for capturing \JsonException
        // as explained here https://ayesh.me/Upgrade-PHP-7.3#json-exceptions
        $decodedValue = json_decode($json, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new ResponseException(
                sprintf('"%s" could not be json-decoded properly, reason: %s', $json, json_last_error_msg())
            );
        }

        return $decodedValue;
    }
}

Function Calls

None

Variables

None

Stats

MD5 fb205f65301e3af355883cf130d648f8
Eval Count 0
Decode Time 110 ms