Advertisement
Guest User

Untitled

a guest
Jan 25th, 2012
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.73 KB | None | 0 0
  1. <?php
  2.  
  3. namespace com\foo;
  4.  
  5. use \Exception;
  6.  
  7. /**
  8.  * A class for handling tabular data
  9.  *
  10.  * @package com.foo
  11.  * @author Tomas Sandven
  12.  **/
  13. class Tabular_Data
  14. {
  15.     // Constants
  16.     const SUPPORTED_FILE_TYPES = 'csv,xls,xlsx';
  17.  
  18.     // Fields
  19.     private $data = false;
  20.     private $initiated = false;
  21.  
  22.     /**
  23.      * Constructor for Tabular_Data class. Use factory functions.
  24.      *
  25.      * @author Tomas Sandven
  26.      **/
  27.     public function __construct() {}
  28.  
  29.     /**
  30.      * Creates a new Tabular_Data object and attempts to load data from the
  31.      * input file. Throws exceptions upon errors. Error codes:
  32.      * - 1001: Input file does not exist
  33.      * - 1002: No read access of input file
  34.      * - 1003: Input file type is not supported
  35.      * - 1004: Error reading file content, could be corrupt file or a code bug
  36.      *
  37.      * Supported file types:
  38.      * - *.csv
  39.      * - *.xls
  40.      * - *.xlsx
  41.      *
  42.      * @param $filepath: The path of the tabular data file
  43.      * @param $columns: An array of the column names to include. This can be
  44.      *                  omitted to load all columns.
  45.      *
  46.      * @return Tabular_Data
  47.      * @author Tomas Sandven
  48.      **/
  49.     public function loadFromFile($filepath)
  50.     {
  51.         // Prepare locals necessary for validation
  52.         $filename = @end(explode('/', $filepath));
  53.         $extension = @end(explode('.', $filename));
  54.         $exts = explode(',', self::SUPPORTED_FILE_TYPES);
  55.  
  56.         // Don't let this class be initiated twice
  57.         if($this->initiated) throw new Exception(
  58.             'Already initiated! Only use factory functions!', 1);
  59.        
  60.         // Check if the file exists
  61.         if(!is_file($filepath)) throw new Exception(
  62.             'Input file does not exist', 1001);
  63.        
  64.         // File must be readable
  65.         if(!is_readable($filepath)) throw new Exception(
  66.             'Lacking permission to read the input file', 1002);
  67.        
  68.         // File type must be supported
  69.         if(!in_array($extension, $exts))
  70.             throw new Exception('File type not supported', 1003);
  71.        
  72.         // TODO
  73.     }
  74.  
  75.     /**
  76.      * Loads tabular data using the PHPExcel library.
  77.      *
  78.      * @param $filepath: The path of the tabular data file
  79.      * @param $columns: An array of the column names to include. This can be
  80.      *                  omitted to load all columns.
  81.      *
  82.      * @return array
  83.      * @author Tomas Sandven
  84.     **/
  85.     public function _loadExcelData($filepath, $columns = false)
  86.     {
  87.        
  88.     }
  89.  
  90.     // #############################
  91.     // ##### FACTORY FUNCTIONS #####
  92.     // #############################
  93.  
  94.     /**
  95.      * Shortcut for running creating a Tabular_Data instance from a file
  96.      *
  97.      * @author Tomas Sandven
  98.      **/
  99.     public static function fromFile($filepath, $columns = false)
  100.     {
  101.         $td = new Tabular_Data;
  102.         $td->loadFromFile($filepath, $columns);
  103.  
  104.         return $td;
  105.     }
  106. }
  107.  
  108. // Debug
  109. if(!defined('BITO'))
  110. {
  111.     $td = Tabular_Data::fromFile('/home/codemonkey/foo/bison.xlsx');
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement