public static function encode($format, $data, $options = [], $message = null)
    {
        $definition = static::get($format);

        if (empty($definition['encode'])) {
            if (is_string($data)) {
                return $data;
            }
            throw new NetException("The `$format` format requires data needs to be a string.");
        }

        $cast = function($data) {
            return method_exists($data, 'data') ? $data->data() : get_object_vars($data);
        };

        if (!empty($definition['cast'])) {
            $data = is_object($data) ? $cast($data) : $data;
        }

        $handler = $definition['encode'];
        return is_string($handler) ? $handler($data) : $handler($data, $definition + $options, $message);
    }
public static function encode($format, $data, $options = [], $message = null)
    {
        $definition = static::get($format);

        if (empty($definition['encode'])) {
            if (is_string($data)) {
                return $data;
            }
            throw new NetException("The `$format` format requires data needs to be a string.");
        }

        $cast = function($data) {
            return method_exists($data, 'data') ? $data->data() : get_object_vars($data);
        };

        if (!empty($definition['cast'])) {
            $data = is_object($data) ? $cast($data) : $data;
        }

        $handler = $definition['encode'];
        return is_string($handler) ? $handler($data) : $handler($data, $definition + $options, $message);
    }
public static function encode ( $ format , $ data , $ options = [ ] , $ message = null ) { $ definition = static :: get ( $ format ) ; if ( empty ( $ definition [ 'encode' ] ) ) { if ( is_string ( $ data ) ) { return $ data ; } throw new NetException ( "The `$format` format requires data needs to be a string." ) ; } $ cast = function ( $ data ) { return method_exists ( $ data , 'data' ) ? $ data -> data ( ) : get_object_vars ( $ data ) ; } ; if ( ! empty ( $ definition [ 'cast' ] ) ) { $ data = is_object ( $ data ) ? $ cast ( $ data ) : $ data ; } $ handler = $ definition [ 'encode' ] ; return is_string ( $ handler ) ? $ handler ( $ data ) : $ handler ( $ data , $ definition + $ options , $ message ) ; }
Encodes data according to the specified format.

@param  string $format  The format into which `$data` will be encoded.
@param  mixed  $data    Arbitrary data you wish to encode.
@param  array  $options Handler-specific options.
@param  object $message A message reference.
@return string          The encoded string data.
Encodes data according to the specified format .