/**
* Convert the query's properties to the data sources' syntax and return it as an array.
*
* @param object $dataSource Instance of `lithium\data\Source` to use for conversion.
* @param array $options Options to use when exporting the data.
* @return array Returns an array containing a data source-specific representation of a query.
*/
public function export(Source $dataSource, array $options = array())
{
$defaults = array('keys' => array());
$options += $defaults;
$keys = $options['keys'] ?: array_keys($this->_config);
$methods = $dataSource->methods();
$results = array('type' => $this->_type);
$apply = array_intersect($keys, $methods);
$copy = array_diff($keys, $apply);
foreach ($apply as $item) {
$results[$item] = $dataSource->{$item}($this->{$item}(), $this);
}
foreach ($copy as $item) {
if (in_array($item, $keys)) {
$results[$item] = $this->_config[$item];
}
}
if (in_array('data', $keys)) {
$results['data'] = $this->_exportData();
}
if (isset($results['source'])) {
$results['source'] = $dataSource->name($results['source']);
}
if (!isset($results['fields'])) {
return $results;
}
$created = array('fields', 'values');
if (is_array($results['fields']) && array_keys($results['fields']) == $created) {
$results = $results['fields'] + $results;
}
return $results;
}