lithium\util\Collection::to PHP Method

to() public method

The supported values of $format depend on the format handlers registered in the static property Collection::$_formats. The Collection class comes with built-in support for array conversion, but other formats may be registered. Once the appropriate handlers are registered, a Collection instance can be converted into any handler-supported format, i.e.: $collection->to('json'); // returns a JSON string $collection->to('xml'); // returns an XML string _Please note that Lithium does not ship with a default XML handler, but one can be configured easily._
See also: lithium\util\Collection::formats()
See also: lithium\util\Collection::$_formats
public to ( string $format, array $options = [] ) : mixed
$format string By default the only supported value is `'array'`. However, additional format handlers can be registered using the `formats()` method.
$options array Options for converting this collection: - `'internal'` _boolean_: Indicates whether the current internal representation of the collection should be exported. Defaults to `false`, which uses the standard iterator interfaces. This is useful for exporting record sets, where records are lazy-loaded, and the collection must be iterated in order to fetch all objects.
return mixed The object converted to the value specified in `$format`; usually an array or string.
    public function to($format, array $options = array())
    {
        $defaults = array('internal' => false);
        $options += $defaults;
        $data = $options['internal'] ? $this->_data : $this;
        return $this->_to($format, $data, $options);
    }

Usage Example

Example #1
0
 function testAdd()
 {
     $scope = __FUNCTION__;
     Redis::config(array('format' => $scope));
     // simplest call
     $expected = array('bar');
     $this->assertEqual(1, Lists::add('foo', 'bar'));
     $this->assertEqual($expected, $this->redis->lRange("{$scope}:lists:foo", 0, -1));
     $expected = array('bar', 'baz');
     $this->assertEqual(2, Lists::add('foo', 'baz'));
     $this->assertEqual($expected, $this->redis->lRange("{$scope}:lists:foo", 0, -1));
     // call with array
     $expected = array('bar', 'baz');
     $this->assertEqual(2, Lists::add('withArray', array('bar', 'baz')));
     $this->assertEqual($expected, $this->redis->lRange("{$scope}:lists:withArray", 0, -1));
     // call with big array
     $expected = array_fill(0, 100, 'blub');
     $this->assertEqual(100, Lists::add('manyItems', $expected));
     $this->assertEqual($expected, $this->redis->lRange("{$scope}:lists:manyItems", 0, -1));
     // call with bigger array
     $expected = array_fill(0, 1000, 'blub');
     $this->assertEqual(1000, Lists::add('lotsItems', $expected));
     $this->assertEqual($expected, $this->redis->lRange("{$scope}:lists:lotsItems", 0, -1));
     // call with collection
     $data = array('lassy', 'barker', 'wuff', 'patty');
     $dogs = new Collection(compact('data'));
     $expected = $dogs->to('array');
     $this->assertEqual(4, Lists::add('dogs', $dogs));
     $this->assertEqual($expected, $this->redis->lRange("{$scope}:lists:dogs", 0, -1));
     // offset
     $this->assertEqual(array('barker'), $this->redis->lRange("{$scope}:lists:dogs", 1, 1));
 }
All Usage Examples Of lithium\util\Collection::to