FluentDOM Package
Current file: /tmp/FluentDOM/FluentDOM/Loader/PDO.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 4 / 4
100.00%100.00%
100.00% 47 / 47
 
FluentDOMLoaderPDO
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 4 / 4
100.00%100.00%
100.00% 47 / 47
 public function setTagNames($root, $record)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 3 / 3
 public function load($source, $contentType)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 40 / 40
 protected function _normalizeColumnName($name)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 1 / 1
 protected function _getNodeType($columnData)
100.00%100.00%
100.00% 1 / 1
100.00%100.00%
100.00% 3 / 3


       1                 : <?php                                                                        
       2                 : /**                                                                          
       3                 : * Load FluentDOM from pdo result                                             
       4                 : *                                                                            
       5                 : * @version $Id: PDO.php 431 2010-03-29 20:42:04Z subjective $                
       6                 : * @license http://www.opensource.org/licenses/mit-license.php The MIT License
       7                 : * @copyright Copyright (c) 2009 Bastian Feder, Thomas Weinert                
       8                 : *                                                                            
       9                 : * @package FluentDOM                                                         
      10                 : * @subpackage Loaders                                                        
      11                 : */                                                                           
      12                 :                                                                              
      13                 : /**                                                                          
      14                 : * include interface                                                          
      15                 : */                                                                           
      16                 : require_once(dirname(__FILE__).'/../Loader.php');                            
      17                 :                                                                              
      18                 : /**                                                                          
      19                 : * Load FluentDOM from pdo result                                             
      20                 : *                                                                            
      21                 : * @package FluentDOM                                                         
      22                 : * @subpackage Loaders                                                        
      23                 : */                                                                           
      24                 : class FluentDOMLoaderPDO implements FluentDOMLoader {                        
      25                 :                                                                              
      26                 :   /**                                                                        
      27                 :   * Element type value                                                       
      28                 :   * @var integer                                                             
      29                 :   */                                                                         
      30                 :   const ELEMENT_VALUE = 1;                                                   
      31                 :   /**                                                                        
      32                 :   * Element type attribute                                                   
      33                 :   * @var integer                                                             
      34                 :   */                                                                         
      35                 :   const ATTRIBUTE_VALUE = 2;                                                 
      36                 :                                                                              
      37                 :   /**                                                                        
      38                 :   * root element name                                                        
      39                 :   * @var string                                                              
      40                 :   */                                                                         
      41                 :   protected $_tagNameRoot = 'records';                                       
      42                 :   /**                                                                        
      43                 :   * record element name                                                      
      44                 :   * @var unknown_type                                                        
      45                 :   */                                                                         
      46                 :   protected $_tagNameRecord = 'record';                                      
      47                 :                                                                              
      48                 :   /**                                                                        
      49                 :   * set root and record tag name for xml elements.                           
      50                 :   *                                                                          
      51                 :   * @param string $root                                                      
      52                 :   * @param string $record                                                    
      53                 :   */                                                                         
      54                 :   public function setTagNames($root, $record) {                              
      55               1 :     $this->_tagNameRoot = $root;                                             
      56               1 :     $this->_tagNameRecord = $record;                                         
      57               1 :   }                                                                          
      58                 :                                                                              
      59                 :   /**                                                                        
      60                 :   * Load DOMDocument from xml string                                         
      61                 :   *                                                                          
      62                 :   * @param string $source xml string                                         
      63                 :   * @param string $contentType                                               
      64                 :   * @return DOMDocument|FALSE                                                
      65                 :   */                                                                         
      66                 :   public function load($source, $contentType) {                              
      67               2 :     if (is_object($source) &&                                                
      68               2 :         $source instanceof PDOStatement) {                                   
      69               1 :       $source->setFetchMode(PDO::FETCH_NUM);                                 
      70               1 :       $columnCount = $source->columnCount();                                 
      71               1 :       $columns = array();                                                    
      72               1 :       for ($i = 0; $i < $columnCount; $i++) {                                
      73               1 :         $columnData = $source->getColumnMeta($i);                            
      74               1 :         $columns[$i] = array(                                                
      75               1 :           'name' => $this->_normalizeColumnName($columnData['name']),        
      76               1 :           'type' => $this->_getNodeType($columnData)                         
      77               1 :         );                                                                   
      78               1 :       }                                                                      
      79               1 :       $dom = new DOMDocument();                                              
      80               1 :       $dom->formatOutput = TRUE;                                             
      81               1 :       $dom->appendChild(                                                     
      82               1 :         $rootNode = $dom->createElement($this->_tagNameRoot)                 
      83               1 :       );                                                                     
      84               1 :       foreach ($source as $row) {                                            
      85               1 :         $rootNode->appendChild(                                              
      86               1 :           $recordNode = $dom->createElement($this->_tagNameRecord)           
      87               1 :         );                                                                   
      88               1 :         foreach ($row as $columnId => $value) {                              
      89               1 :           switch ($columns[$columnId]['type']) {                             
      90               1 :           case self::ATTRIBUTE_VALUE :                                       
      91               1 :             $recordNode->setAttribute(                                       
      92               1 :               $columns[$columnId]['name'],                                   
      93                 :               $value                                                         
      94               1 :             );                                                               
      95               1 :             break;                                                           
      96               1 :           default :                                                          
      97               1 :             $recordNode->appendChild(                                        
      98               1 :               $valueNode = $dom->createElement(                              
      99               1 :                 $columns[$columnId]['name'],                                 
     100                 :                 $value                                                       
     101               1 :               )                                                              
     102               1 :             );                                                               
     103               1 :             break;                                                           
     104               1 :           }                                                                  
     105               1 :         }                                                                    
     106               1 :       }                                                                      
     107               1 :       return $dom;                                                           
     108                 :     }                                                                        
     109               1 :     return FALSE;                                                            
     110                 :   }                                                                          
     111                 :                                                                              
     112                 :   /**                                                                        
     113                 :   * normalize column for tag name use                                        
     114                 :   *                                                                          
     115                 :   * @param string $name                                                      
     116                 :   */                                                                         
     117                 :   protected function _normalizeColumnName($name) {                           
     118               1 :     return preg_replace('([:|+~\s]+)', '-', $name);                          
     119                 :   }                                                                          
     120                 :                                                                              
     121                 :   /**                                                                        
     122                 :   * get node type (attribute or element)                                     
     123                 :   *                                                                          
     124                 :   * @param array $columnData                                                 
     125                 :   */                                                                         
     126                 :   protected function _getNodeType($columnData) {                             
     127               1 :     if ($columnData['native_type'] == 'string') {                            
     128               1 :       return self::ELEMENT_VALUE;                                            
     129                 :     } else {                                                                 
     130               1 :       return self::ATTRIBUTE_VALUE;                                          
     131                 :     }                                                                        
     132                 :   }                                                                          
     133                 : }                                                                            
     134                 :                                                                              

Generated by PHPUnit 3.4.12 and Xdebug 2.0.5 using PHP 5.2.13 at Sun Sep 26 1:00:19 CEST 2010.