to() public méthode

Adds conversions checks to ensure certain class types and embedded values are properly cast.
public to ( string $format, array $options = [] ) : mixed
$format string Currently only `array` is supported.
$options array
Résultat mixed
Exemple #1
0
 public function testMap()
 {
     $collection = new DocumentSet();
     $collection->set(array('title' => 'Lorem Ipsum', 'key' => 'value', 'foo' => 'bar'));
     $results = $collection->map(function ($value) {
         return $value . ' test';
     });
     $expected = array('Lorem Ipsum test', 'value test', 'bar test');
     $this->assertEqual($results->to('array'), $expected);
     $this->assertNotEqual($results->to('array'), $collection->to('array'));
 }
 public function testHandlers()
 {
     $model = $this->_model;
     $schema = new DocumentSchema(array('fields' => array('_id' => array('type' => 'id'), 'date' => array('type' => 'date')), 'types' => array('date' => 'date'), 'handlers' => array('date' => function ($v) {
         return (object) $v;
     })));
     $handlers = array('stdClass' => function ($value) {
         return date('d/m/Y H:i', strtotime($value->scalar));
     });
     $array = new DocumentSet(compact('model', 'schema', 'handlers') + array('data' => array(array('_id' => '2', 'date' => '2013-06-06 13:00:00'), array('_id' => '3', 'date' => '2013-06-06 12:00:00'), array('_id' => '4', 'date' => '2013-06-06 11:00:00'))));
     $expected = array(array('_id' => '2', 'date' => '06/06/2013 13:00'), array('_id' => '3', 'date' => '06/06/2013 12:00'), array('_id' => '4', 'date' => '06/06/2013 11:00'));
     $this->assertIdentical($expected, $array->to('array', array('indexed' => false)));
 }
 public function testTo()
 {
     Collection::formats('lithium\\net\\http\\Media');
     $resource = new MockResult();
     $doc = new DocumentSet(array('model' => $this->_model, 'result' => $resource));
     $expected = array('4c8f86167675abfabdbf0300' => array('_id' => '4c8f86167675abfabdbf0300', 'title' => 'bar'), '5c8f86167675abfabdbf0301' => array('_id' => '5c8f86167675abfabdbf0301', 'title' => 'foo'), '6c8f86167675abfabdbf0302' => array('_id' => '6c8f86167675abfabdbf0302', 'title' => 'dib'));
     $this->assertEqual($expected, $doc->to('array'));
     $expected = array(array('_id' => '4c8f86167675abfabdbf0300', 'title' => 'bar'), array('_id' => '5c8f86167675abfabdbf0301', 'title' => 'foo'), array('_id' => '6c8f86167675abfabdbf0302', 'title' => 'dib'));
     $this->assertEqual($expected, $doc->to('array', array('indexed' => false)));
 }