<?php
/*=====================================================================*
|| ###################################################################
|| # WebFreeChart
|| # Copyright (c)2002-2007 Blue Static
|| #
|| # This program is free software; you can redistribute it and/or modify
|| # it under the terms of the GNU General Public License as published by
|| # the Free Software Foundation; version 2 of the License.
|| #
|| # This program is distributed in the hope that it will be useful, but
|| # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|| # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|| # more details.
|| #
|| # You should have received a copy of the GNU General Public License along
|| # with this program; if not, write to the Free Software Foundation, Inc.,
|| # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|| ###################################################################
\*=====================================================================*/
include_once WFCDIR . '/Data/Abstract/DataSet.php';
/**
* This is a concrete implementation of DataSet using key-value pairs
* in the form of an array
*
* @author Blue Static
* @copyright Copyright (c)2002 - 2007, Blue Static
* @package WebFreeChart
*
*/
class ArrayDataSet implements DataSet
{
/**
* Array of key-value pairs
* @var array
*/
private $data;
// ###################################################################
/**
* Constructs a new KeyedValues object with an optional data array
*
* @param array $data
*/
public function __construct($data = array())
{
$this->data = (array)$data;
}
// ###################################################################
/**
* Returns the number of items in the collection
*
* @return integer
*/
public function getItemCount()
{
return sizeof($this->data);
}
// ###################################################################
/**
* Returns the key of the nth element in the data array
*
* @throws DataSetException if the index is out of bounds
*
* @return mixed
*/
public function getKey($index)
{
if ($index > $this->getItemCount() OR $index < 0)
{
throw new DataSetException('Array index out of bounds');
}
$index = -$index;
foreach ($this->data AS $key => $value)
{
if ($index == 0)
{
return $key;
}
$index++;
}
}
// ###################################################################
/**
* Returns the numerical index for a given key, or -1 if the key is
* not found
*
* @param mixed $key
*
* @return integer
*/
public function getIndex($key)
{
$i = 0;
foreach ($this->data AS $_key => $_value)
{
if ($key == $_key)
{
return $i;
}
$i++;
}
return -1;
}
// ###################################################################
/**
* Returns an array of all the keys in the dataset
*
* @return array
*/
public function getKeys()
{
return array_keys($this->data);
}
// ###################################################################
/**
* Returns a value for the given key
*
* @throws DataSetException if the key does not exist
*
* @param mixed $key
*
* @return mixed
*/
public function getValue($key)
{
if (!isset($this->data[$key]))
{
throw new DataSetException('Unknown key in ' . __CLASS__);
}
return $this->data[$key];
}
// ###################################################################
/**
* Sets the given key to the given value
*
* @param mixed $key
* @param mixed $value
*/
public function setValue($key, $value)
{
$this->data[$key] = $value;
}
// ###################################################################
/**
* Removes a given key from the dataset
*
* @param mixed $key
*/
public function remove($key)
{
unset($this->data[$key]);
}
// ###################################################################
/**
* Calls a sorting function on the array (default is ksort)
*
* @param string $function
*/
public function sort($function = 'ksort')
{
$function($this->data);
}
}
?>