public function encode($data, array $spec, $throw = false) {
        $str = json_encode($data, JSON_UNESCAPED_SLASHES);

        $first = true;
        foreach ($spec as $name => $password) {
            if ($name === self::STRICT) {
                // Strict is just an option so continue.
                continue;
            }

            $supported = $this->supportedInfo($name, $throw);
            if ($supported === null) {
                return null;
            }

            list($encode,, $method, $encodeFirst) = $supported;

            if ($first) {
                if ($encodeFirst) {
                    $str = static::base64urlEncode($str);
                }
                $this->pushString($str, self::EOS);
            }

            switch ($encode) {
                case 'encrypt':
                    $str = $this->encrypt($str, $method, $password, '', $throw);
                    break;
                case 'hmac':
                    $str = $this->hmac($str, $method, $password, 0, $throw);
                    break;
                default:
                    return $this->exception($throw, "Invalid method $encode.", 500);
            }
            // Return on error.
            if (!$str) {
                return null;
            }

            $this->pushString($str, $name);

            $first = false;
        }

        return $str;
    }
public function encode($data, array $spec, $throw = false) {
        $str = json_encode($data, JSON_UNESCAPED_SLASHES);

        $first = true;
        foreach ($spec as $name => $password) {
            if ($name === self::STRICT) {
                // Strict is just an option so continue.
                continue;
            }

            $supported = $this->supportedInfo($name, $throw);
            if ($supported === null) {
                return null;
            }

            list($encode,, $method, $encodeFirst) = $supported;

            if ($first) {
                if ($encodeFirst) {
                    $str = static::base64urlEncode($str);
                }
                $this->pushString($str, self::EOS);
            }

            switch ($encode) {
                case 'encrypt':
                    $str = $this->encrypt($str, $method, $password, '', $throw);
                    break;
                case 'hmac':
                    $str = $this->hmac($str, $method, $password, 0, $throw);
                    break;
                default:
                    return $this->exception($throw, "Invalid method $encode.", 500);
            }
            // Return on error.
            if (!$str) {
                return null;
            }

            $this->pushString($str, $name);

            $first = false;
        }

        return $str;
    }
public function encode ( $ data , array $ spec , $ throw = false ) { $ str = json_encode ( $ data , JSON_UNESCAPED_SLASHES ) ; $ first = true ; foreach ( $ spec as $ name => $ password ) { if ( $ name === self :: STRICT ) { // Strict is just an option so continue. continue ; } $ supported = $ this -> supportedInfo ( $ name , $ throw ) ; if ( $ supported === null ) { return null ; } list ( $ encode , , $ method , $ encodeFirst ) = $ supported ; if ( $ first ) { if ( $ encodeFirst ) { $ str = static :: base64urlEncode ( $ str ) ; } $ this -> pushString ( $ str , self :: EOS ) ; } switch ( $ encode ) { case 'encrypt' : $ str = $ this -> encrypt ( $ str , $ method , $ password , '' , $ throw ) ; break ; case 'hmac' : $ str = $ this -> hmac ( $ str , $ method , $ password , 0 , $ throw ) ; break ; default : return $ this -> exception ( $ throw , "Invalid method $encode." , 500 ) ; } // Return on error. if ( ! $ str ) { return null ; } $ this -> pushString ( $ str , $ name ) ; $ first = false ; } return $ str ; }
Encode a string using a secure specification.

@param mixed $data The data to encode. This must be data that supports json serialization.
@param array $spec An array specifying how the string should be encoded.
The array should be in the form ['spec1' => 'password', 'spec2' => 'password'].
@param bool $throw Whether or not to throw an exception on error.
@return string|null Returns the encoded string or null if there is an error.
Encode a string using a secure specification .