encode() public static method

For media types registered in $_handlers which include an 'encode' setting, encodes data according to the specified media type.
See also: lithium\net\http\Media::type()
public static encode ( mixed $handler, mixed $data, object &$response = null ) : mixed
$handler mixed Specifies the media type into which `$data` will be encoded. This media type must have an `'encode'` setting specified in `Media::$_handlers`. Alternatively, `$type` can be an array, in which case it is used as the type handler configuration. See the `type()` method for information on adding type handlers, and the available configuration keys.
$data mixed Arbitrary data you wish to encode. Note that some encoders can only handle arrays or objects.
$response object A reference to the `Response` object for this dispatch cycle.
return mixed Returns the result of `$data`, encoded with the encoding configuration specified by `$type`, the result of which is usually a string.
Beispiel #1
0
 public function testEncodeRecordSet()
 {
     $data = new RecordSet(array('data' => array(1 => new Record(array('data' => array('id' => 1, 'foo' => 'bar'))), 2 => new Record(array('data' => array('id' => 2, 'foo' => 'baz'))), 3 => new Record(array('data' => array('id' => 3, 'baz' => 'dib'))))));
     $json = '{"1":{"id":1,"foo":"bar"},"2":{"id":2,"foo":"baz"},"3":{"id":3,"baz":"dib"}}';
     $this->assertEqual($json, Media::encode(array('encode' => 'json_encode'), $data));
 }
Beispiel #2
0
 /**
  * Instantiates a request object (usually an instance of `http\Request`) and tests its
  * properties based on the request type and data to be sent.
  *
  * @param string $method The HTTP method of the request, i.e. `'GET'`, `'HEAD'`, `'OPTIONS'`,
  *        etc. Can be passed in upper- or lower-case.
  * @param string $path The
  * @param string $data
  * @param string $options
  * @return object Returns an instance of `http\Request`, configured with an HTTP method, query
  *         string or POST/PUT data, and URL.
  */
 protected function _request($method, $path, $data, $options)
 {
     $defaults = array('type' => 'form');
     $options += $defaults + $this->_config;
     $request = $this->_instance('request', $options);
     $request->path = str_replace('//', '/', "{$request->path}{$path}");
     $request->method = $method = strtoupper($method);
     $media = $this->_classes['media'];
     $type = null;
     if (in_array($options['type'], $media::types()) && $data && !is_string($data)) {
         $type = $media::type($options['type']);
         $contentType = (array) $type['content'];
         $request->headers(array('Content-Type' => current($contentType)));
         $data = Media::encode($options['type'], $data, $options);
     }
     in_array($method, array('POST', 'PUT')) ? $request->body($data) : ($request->params = $data);
     return $request;
 }
Beispiel #3
0
 public function testEncodeNotCallable()
 {
     $data = array('foo' => 'bar');
     $result = Media::encode(array('encode' => false), $data);
     $this->assertNull($result);
 }
 public function testMediaEncoding()
 {
     $data = array('hello', 'goodbye', 'foo' => array('bar', 'baz' => 'dib'));
     $expected = json_encode($data);
     $result = Media::encode('json', $data);
     $this->assertEqual($expected, $result);
     $this->assertEqual($result, Media::to('json', $data));
     $result = Media::encode('badness', $data);
     $this->assertNull($result);
 }