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._
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. |
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)); }
/** * Tests that various types of handlers can be registered with `Collection::formats()`, and * that collection instances are converted correctly. * * @return void */ public function testCollectionFormatConversion() { Collection::formats('lithium\\net\\http\\Media'); $data = array('hello', 'goodbye', 'foo' => array('bar', 'baz' => 'dib')); $collection = new Collection(compact('data')); $expected = json_encode($data); $result = $collection->to('json'); $this->assertEqual($expected, $result); $this->assertNull($collection->to('badness')); Collection::formats(false); $this->assertNull($collection->to('json')); Collection::formats('json', function ($collection, $options) { return json_encode($collection->to('array')); }); $result = $collection->to('json'); $this->assertEqual($expected, $result); $result = $collection->to(function ($collection) { $value = array_map(function ($i) { return is_array($i) ? join(',', $i) : $i; }, $collection->to('array')); return join(',', $value); }); $expected = 'hello,goodbye,bar,dib'; $this->assertEqual($expected, $result); }
public function testUnsetInForeach() { $data = array('Hello', 'Delete me', 'Delete me', 'Delete me', 'Delete me', 'Delete me', 'Hello again!', 'Delete me'); $collection = new Collection(array('data' => $data)); $this->assertIdentical($data, $collection->to('array')); foreach ($collection as $i => $word) { if ($word == 'Delete me') { unset($collection[$i]); } } $expected = array(0 => 'Hello', 6 => 'Hello again!'); $results = $collection->to('array'); $this->assertIdentical($expected, $results); }
/** * Converts the data in the record set to a different format, i.e. an array. * * @param string $format * @param array $options * @return mixed */ public function to($format, $options = array()) { $defaults = array('indexed' => true); $options += $defaults; $result = null; $this->offsetGet(null); switch ($format) { case 'array': $result = array_map(function ($r) { return $r->to('array'); }, $this->_items); if (is_scalar(current($this->_index)) && $options['indexed']) { $result = array_combine($this->_index, $result); } break; default: $result = parent::to($format, $options); break; } return $result; }
public function testCollectionFormatConversion() { Collection::formats('\\lithium\\http\\Media'); $items = array('hello', 'goodbye', 'foo' => array('bar', 'baz' => 'dib')); $collection = new Collection(compact('items')); $expected = json_encode($items); $result = $collection->to('json'); $this->assertEqual($result, $expected); $this->assertNull($collection->to('badness')); }
/** * Tests that the Collection::sort method works appropriately. * * @return void */ public function testCollectionSort() { // Typical numeric sort using the default "sort" PHP function $collection = new Collection(array('data' => array(5,3,4,1,2))); $collection->sort(); $expected = array(1,2,3,4,5); $this->assertEqual($expected, $collection->to('array')); // String sort using "sort" $collection = new Collection(array('data' => array('alan','dave','betsy','carl'))); $collection->sort(); $expected = array('alan','betsy','carl','dave'); $this->assertEqual($expected, $collection->to('array')); // String sort using strcasecmp $collection = new Collection(array('data' => array('Alan','Dave','betsy','carl'))); $collection->sort('strcasecmp'); $expected = array('Alan','betsy','carl','Dave'); $this->assertEqual($expected, $collection->to('array')); // Numeric sort using custom function $collection = new Collection(array('data' => array(5,3,4,1,2))); $collection->sort(function ($a,$b) { if ($a == $b) { return 0; } return ($b > $a ? 1 : -1); }); $expected = array(5,4,3,2,1); $this->assertEqual($expected, $collection->to('array')); // Test fail $collection = new Collection(array('data' => array(5,3,4,1,2))); $result = $collection->sort('blahgah'); $this->assertEqual($collection->to('array'), $result->to('array')); }