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. - `'indexed'` _boolean|null_: Allows to control how converted data is keyed. When set to `true` will force indexed conversion of the collection (the default) even if the collection has a parent. When `false` will convert without indexing. Provide `null` as a value to this option to only index when the collection has no parent. |
리턴 | mixed | The object converted to the value specified in `$format`; usually an array or string. |
/** * Adds conversions checks to ensure certain class types and embedded values are properly cast. * * @param string $format Currently only `array` is supported. * @param array $options * @return mixed */ public function to($format, array $options = array()) { $options += array('handlers' => array('MongoId' => function ($value) { return (string) $value; }, 'MongoDate' => function ($value) { return $value->sec; })); $this->offsetGet(null); return parent::to($format, $options); }
/** * Adds conversions checks to ensure certain class types and embedded values are properly cast. * * @param string $format Currently only `array` is supported. * @param array $options * @return mixed */ public function to($format, array $options = array()) { $defaults = array('handlers' => array( 'MongoId' => function($value) { return (string) $value; }, 'MongoDate' => function($value) { return $value->sec; } )); if ($format == 'array') { $options += $defaults; return Collection::toArray($this->_data, $options); } return parent::to($format, $options); }
/** * Adds conversions checks to ensure certain class types and embedded values are properly cast. * * @param string $format Currently only `array` is supported. * @param array $options * @return mixed */ public function to($format, array $options = array()) { $this->offsetGet(null); return parent::to($format, $options); }
/** * 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, array $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->_data); if (is_scalar(current($this->_index)) && $options['indexed']) { if (!empty($this->_index) && !empty($result)) { $result = array_combine($this->_index, $result); } else { $result = array(); } } break; default: $result = parent::to($format, $options); break; } return $result; }