/**
* Convert the query's properties to the data sources' syntax and return it as an array.
*
* @param object $source Instance of the data source (`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 $source, array $options = array())
{
$defaults = array('keys' => array());
$options += $defaults;
if ($options['keys']) {
$keys = array_flip($options['keys']);
} else {
$keys =& $this->_config;
}
$results = array('type' => $this->_type);
$apply = array_intersect_key($keys, array_flip($source->methods()));
$copy = array_diff_key($keys, $apply);
if (isset($keys['with'])) {
$this->applyStrategy($source);
}
foreach ($apply as $item => $value) {
$results[$item] = $source->{$item}($this->{$item}(), $this);
}
foreach ($copy as $item => $value) {
$results[$item] = $this->_config[$item];
}
if (array_key_exists('data', $keys)) {
$results['data'] = $this->_exportData();
}
if (array_key_exists('source', $keys)) {
$results['source'] = $source->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;
}