lithium\net\http\Media::to PHP Method

to() public static method

Alias for encode(); included for interface compatibility with lithium\util\Collection::to(), which allows a collection object to be exported to any format supported by a Media handler. See the documentation for Collection::to() for more information.
public static to ( mixed $format, mixed $data, array $options = [] ) : mixed
$format mixed Format into which data will be converted, i.e. `'json'`.
$data mixed Either an array or object (usually an instance of `Collection`) which will be converted into the specified format.
$options array Additional handler-specific options to pass to the content handler.
return mixed
    public static function to($format, $data, array $options = array())
    {
        return static::encode($format, $data, $options);
    }

Usage Example

Example #1
0
 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));
     $this->assertNull(Media::encode('badness', $data));
     $result = Media::decode('json', $expected);
     $this->assertEqual($data, $result);
 }