cvs: pear /Structures_DataGrid/DataGrid/DataSource Array.php

From: Date: Wed, 18 Aug 2004 18:47:55 +0000
Subject: cvs: pear /Structures_DataGrid/DataGrid/DataSource Array.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-23083@lists.php.net to get a copy of this message
asnagy Wed Aug 18 14:47:55 2004 EDT Modified files: /pear/Structures_DataGrid/DataGrid/DataSource Array.php Log: Removed sort and limit method Added staticFetch method Fetch method now handles sorting and limiting http://cvs.php.net/diff.php/pear/Structures_DataGrid/DataGrid/DataSource/Array.php?r1=1.3&r2=1.4&ty=u Index: pear/Structures_DataGrid/DataGrid/DataSource/Array.php diff -u pear/Structures_DataGrid/DataGrid/DataSource/Array.php:1.3 pear/Structures_DataGrid/DataGrid/DataSource/Array.php:1.4 --- pear/Structures_DataGrid/DataGrid/DataSource/Array.php:1.3 Tue Aug 17 13:46:54 2004 +++ pear/Structures_DataGrid/DataGrid/DataSource/Array.php Wed Aug 18 14:47:55 2004 @@ -25,7 +25,7 @@ * * This class is a data source driver for a 2D Array * - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ * @author Olivier Guilyardi <olivier@samalyse.com> * @access public * @package Structures_DataGrid @@ -40,9 +40,6 @@ * @access private */ var $_ar; - - var $_offset = 0; - var $_limit = null; function Structures_DataGrid_DataSource_Array() { @@ -56,8 +53,15 @@ * @access public * @return mixed True on success, PEAR_Error on failure */ - function bind($ar) + function bind($ar, $options=array()) { + if (count($options)) { + $test = $this->_setOptions($options); + if (PEAR::isError($test)) { + return $test; + } + } + if (is_array($ar)) { $this->_ar = $ar; return true; @@ -67,37 +71,6 @@ } /** - * Sort - * - * @access public - * @param string $field The field to sort by - * @param string $direction The direction to sort, either ASC or DESC - */ - function sort($field, $direction='ASC') - { - $numRows = count($this->_ar); - $sortAr = array(); - for ($i = 0; $i < $numRows; $i++) { - $sortAr[$i] = $this->_ar[$i][$field]; - } - $direction = strtoupper($direction) == 'ASC' ? SORT_ASC : SORT_DESC; - array_multisort($sortAr, $direction, $this->_ar); - } - - /** - * Limit - * - * @access public - * @param int $offset The count offset - * @param int $length The amount to limit to - */ - function limit($offset, $length) - { - $this->_offset = $offset; - $this->_limit = $length; - } - - /** * Count * * @access public @@ -111,18 +84,74 @@ /** * Fetch * + * @param integer $offset Limit offset (starting from 0) + * @param integer $len Limit length + * @param string $sortField Field to sort by + * @param string $sortDir Sort direction : 'ASC' or 'DESC' * @access public * @return array The 2D Array of the records */ - function &fetch() + function &fetch($offset=0, $len=null, $sortField='', $sortDir='ASC') { - if (is_null($this->_limit)) { - $slice = array_slice($this->_ar, $this->_offset); + if ($this->_ar && !$this->_options['fields']) { + $this->_setOptions(array('fields' => array_keys($this->_ar[0]))); + } + $records =& $this->staticFetch($this->_ar, $this->_options['fields'], + $offset, $len, $sortField, $sortDir); + return $records; + } + + /** + * Reusable static fetch method + * + * Since many drivers end up needing this array driver's features, + * the following method is provided in order to avoid subclassing + * this class. + * + * @param integer $offset Limit offset (starting from 0) + * @param integer $len Limit length + * @param string $sortField Field to sort by + * @param string $sortDir Sort direction : 'ASC' or 'DESC' + * @static + */ + function &staticFetch($ar, $fieldList, $offset=0, $len=null, + $sortField=null, $sortDir='ASC') + { + // sorting + if ($sortField) { + $numRows = count($ar); + $sortAr = array(); + for ($i = 0; $i < $numRows; $i++) { + $sortAr[$i] = $ar[$i][$sortField]; + } + $sortDir = strtoupper($sortDir) == 'ASC' ? SORT_ASC : SORT_DESC; + array_multisort($sortAr, $sortDir, $ar); + } + + // slicing + if (is_null($len)) { + $slice = array_slice($ar, $ofs); } else { - $slice = array_slice($this->_ar, $this->_offset, $this->_limit); + $slice = array_slice($ar, $ofs, $len); } - return array('Records' => $slice); + // filtering fields + // + // With the new array_intersect_key() the following would be : + // $records = array_intersect_key($slice, array_flip ($fieldList)); + // One line... And faster... But this function is cvs-only. + $records = array(); + foreach ($slice as $rec) { + $buf = array(); + foreach ($rec as $key => $val) { + if (in_array($key, $fieldList)) { + $buf[$key] = $val; + } + } + $records[] = $buf; + } + + return $records; } }

« previous php.pear.cvs (#23083) next »