| 1 |
<?php |
| 2 |
|
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | Copyright 2013 Madpixels (email : visualizer@madpixels.net) | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | This program is free software; you can redistribute it and/or modify | |
| 7 |
// | it under the terms of the GNU General Public License, version 2, as | |
| 8 |
// | published by the Free Software Foundation. | |
| 9 |
// | | |
| 10 |
// | This program is distributed in the hope that it will be useful, | |
| 11 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 |
// | GNU General Public License for more details. | |
| 14 |
// | | |
| 15 |
// | You should have received a copy of the GNU General Public License | |
| 16 |
// | along with this program; if not, write to the Free Software | |
| 17 |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, | |
| 18 |
// | MA 02110-1301 USA | |
| 19 |
// +----------------------------------------------------------------------+ |
| 20 |
// | Author: Eugene Manuilov <eugene@manuilov.org> | |
| 21 |
// +----------------------------------------------------------------------+ |
| 22 |
|
| 23 |
/** |
| 24 |
* Source manager for CSV files. |
| 25 |
* |
| 26 |
* @category Visualizer |
| 27 |
* @package Source |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
*/ |
| 31 |
class Visualizer_Source_Csv extends Visualizer_Source { |
| 32 |
|
| 33 |
/** |
| 34 |
* The path to the file with data. |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
* |
| 38 |
* @access private |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
private $_filename; |
| 42 |
|
| 43 |
/** |
| 44 |
* Constructor. |
| 45 |
* |
| 46 |
* @since 1.0.0 |
| 47 |
* |
| 48 |
* @access public |
| 49 |
* @param string $filename The path to the file. |
| 50 |
*/ |
| 51 |
public function __construct( $filename ) { |
| 52 |
$this->_filename = $filename; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Fetches series information. |
| 57 |
* |
| 58 |
* @since 1.0.0 |
| 59 |
* |
| 60 |
* @access private |
| 61 |
* @param resource $handle The file handle resource. |
| 62 |
*/ |
| 63 |
private function _fetchSeries( $handle ) { |
| 64 |
// read column titles |
| 65 |
$labels = fgetcsv( $handle ); |
| 66 |
|
| 67 |
// read series types |
| 68 |
$types = fgetcsv( $handle ); |
| 69 |
|
| 70 |
if ( !$labels || !$types ) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) { |
| 75 |
$this->_series[] = array( |
| 76 |
'label' => $labels[$i], |
| 77 |
'type' => isset( $types[$i] ) ? $types[$i] : 'string', |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Fetches information from source, parses it and builds series and data arrays. |
| 86 |
* |
| 87 |
* @since 1.0.0 |
| 88 |
* |
| 89 |
* @access public |
| 90 |
* @return boolean TRUE on success, otherwise FALSE. |
| 91 |
*/ |
| 92 |
public function fetch() { |
| 93 |
// set line endings auto detect mode |
| 94 |
@ini_set( 'auto_detect_line_endings', true ); |
| 95 |
|
| 96 |
// read file and fill arrays |
| 97 |
$handle = fopen( $this->_filename, 'rb' ); |
| 98 |
if ( $handle ) { |
| 99 |
// fetch series |
| 100 |
if ( !$this->_fetchSeries( $handle ) ) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
// fetch data |
| 105 |
while ( ( $data = fgetcsv( $handle ) ) !== false ) { |
| 106 |
$this->_data[] = $this->_normalizeData( $data ); |
| 107 |
} |
| 108 |
|
| 109 |
// close file handle |
| 110 |
fclose( $handle ); |
| 111 |
} |
| 112 |
|
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Returns source name. |
| 118 |
* |
| 119 |
* @since 1.0.0 |
| 120 |
* |
| 121 |
* @access public |
| 122 |
* @return string The name of source. |
| 123 |
*/ |
| 124 |
public function getSourceName() { |
| 125 |
return __CLASS__; |
| 126 |
} |
| 127 |
|
| 128 |
} |