Source for file Array.php

Documentation is available at Array.php

  1. <?php
  2. /**
  3.  * This file contains the class XML_Query2XML_Driver_Array.
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * @category  XML
  8.  * @package   XML_Query2XML
  9.  * @author    Lukas Feiler <lukas.feiler@lukasfeiler.com>
  10.  * @copyright 2008 Lukas Feiler
  11.  * @license   http://www.gnu.org/copyleft/lesser.html  LGPL Version 2.1
  12.  * @version   CVS: $Id: Array.php 259514 2008-05-10 21:04:44Z lukasfeiler $
  13.  * @link      http://pear.php.net/package/XML_Query2XML
  14.  */
  15.  
  16. /**
  17.  * XML_Query2XML_Driver_Array extends XML_Query2XML_Driver.
  18.  */
  19. require_once 'XML/Query2XML.php';
  20.  
  21. /**
  22.  * Array-based driver.
  23.  *
  24.  * usage:
  25.  * <code>
  26.  * $arrayDriver = XML_Query2XML_Driver_Array::factory();
  27.  * $arrayDriver->addResultSet(
  28.  *  'RESULT_SET_NAME', // to be used for $sql or $options['sql']
  29.  *  array(
  30.  *    array('column' => 'value', ...), // record #1
  31.  *    array('column' => 'value', ...)  // record #2
  32.  *  )
  33.  * );
  34.  * </code>
  35.  * Usage scenario:
  36.  * - Transform an array of associative arrays to XML:
  37.  *   $query2xml->getFlatXML('RESULT_SET_ARTISTS_FLAT');
  38.  * - Transform an array of index arrays to XML:
  39.  *   $dom = $query2xml->getXML(
  40.  *       'RESULT_SET_ARTISTS_FLAT',
  41.  *       array(
  42.  *          'rootTag' => 'data',
  43.  *          'rowTag' => 'row',
  44.  *          'idColumn' => false,
  45.  *          'elements' => array(
  46.  *              'name' => '0',
  47.  *              'ID' => '1',
  48.  *              'info' => '2'
  49.  *          )
  50.  *       )
  51.  *   );
  52.  * - Easy integration of other data sources without having
  53.  *   to write a separate driver.
  54.  * - Easy lookup table for static data. $options['sql']['data']
  55.  *   can be used to specify the matching record.
  56.  *
  57.  * @category  XML
  58.  * @package   XML_Query2XML
  59.  * @author    Lukas Feiler <lukas.feiler@lukasfeiler.com>
  60.  * @copyright 2008 Lukas Feiler
  61.  * @license   http://www.gnu.org/copyleft/lesser.html  LGPL Version 2.1
  62.  * @version   Release: @package_version@
  63.  * @link      http://pear.php.net/package/XML_Query2XML
  64.  * @since     Release 1.8.0RC1
  65.  */
  66. {
  67.     /**
  68.      * An associative array of result sets.
  69.      * @var array An associative array.
  70.      */
  71.     private $_resultSets null;
  72.     
  73.     /**
  74.      * Add a named result set.
  75.      *
  76.      * @param string $name      The name of the result set.
  77.      * @param array  $resultSet An indexed array of associative arrays.
  78.      *
  79.      * @return void 
  80.      */
  81.     public function addResultSet($namearray $resultSet)
  82.     {
  83.         $this->_resultSets[$name$resultSet;
  84.     }
  85.     
  86.     /**
  87. /**
  88.      * Remove a named result set.
  89.      *
  90.      * @param string $name The name of the result set to remove
  91.      *
  92.      * @return void 
  93.      */
  94.     public function removeResultSet($name)
  95.     {
  96.         unset($this->_resultSets[$name]);
  97.     }
  98.     
  99.     /**
  100.      * Factory method
  101.      *
  102.      * @param array  $resultSet     An index array of associative arrays.
  103.      *                               This argument is optional.
  104.      * @param string $resultSetName The name of the result set. This argument
  105.      *                               is optional. The default is an empty string.
  106.      *
  107.      * @return XML_Query2XML_Driver_Array 
  108.      */
  109.     public static function factory(array $resultSet null$resultSetName '')
  110.     {
  111.         $driver new XML_Query2XML_Driver_Array();
  112.         if (is_array($resultSet)) {
  113.             $driver->addResultSet($resultSetName$resultSet);
  114.         }
  115.         return $driver;
  116.     }
  117.     
  118.     /**
  119.      * Returns all records from a named result set.
  120.      *
  121.      * @param array  $query      An array with at least one element: 'query'.
  122.      * @param string $configPath The configuration path.
  123.      *
  124.      * @return array An index array of associative arrays.
  125.      */
  126.     public function getAllRecords($query$configPath)
  127.     {
  128.         $resultSetName $query['query'];
  129.         if (!isset($this->_resultSets[$resultSetName])) {
  130.             throw new XML_Query2XML_ArrayException(
  131.                 $configPath ': the result set "' $resultSetName '" '
  132.                 . 'does not exist'
  133.             );
  134.         }
  135.         $resultSet $this->_resultSets[$resultSetName];
  136.         if (array_key_exists('data'$query)) {
  137.             /*
  138.              * Only return records that have a corresponding
  139.              * column for each element key of $query['data']
  140.              * and have the same column value as the array
  141.              * element.
  142.              */
  143.             $filteredResultSet array();
  144.             for ($i 0$i count($resultSet)$i++{
  145.                 $include true;
  146.                 foreach ($query['data'as $key => $value{
  147.                     if (array_key_exists($key$resultSet[$i])) {
  148.                         if ($resultSet[$i][$key!= $value{
  149.                             $include false;
  150.                             break;
  151.                         }
  152.                     else {
  153.                         $include false;
  154.                     }
  155.                 }
  156.                 if ($include{
  157.                     $filteredResultSet[$resultSet[$i];
  158.                 }
  159.             }
  160.             $resultSet $filteredResultSet;
  161.         }
  162.         
  163.         return $resultSet;
  164.     }
  165. }
  166.  
  167. /**
  168.  * Exception for errors related to the array driver.
  169.  *
  170.  * @category XML
  171.  * @package  XML_Query2XML
  172.  * @author   Lukas Feiler <lukas.feiler@lukasfeiler.com>
  173.  * @license  http://www.gnu.org/copyleft/lesser.html  LGPL Version 2.1
  174.  * @link     http://pear.php.net/package/XML_Query2XML
  175.  * @since    Release 1.8.0RC1
  176.  */
  177. {
  178.     /**
  179.      * Constructor
  180.      *
  181.      * @param string $message The error message.
  182.      */
  183.     public function __construct($message)
  184.     {
  185.         parent::__construct($message);
  186.     }
  187. }
  188. ?>

Documentation generated on Sun, 03 Apr 2011 13:13:07 +0200 by phpDocumentor 1.4.1