| @@ -46,10 +46,10 @@ | ||
| 46 | 46 | * |
| 47 | 47 | * @access public |
| 48 | 48 | * @param string $filename The path to the file. |
| 49 | 49 | */ |
| 50 | - public function __construct( $filename = '' ) { | |
| 51 | - $this->_filename = trim( (string) $filename ); | |
| 50 | + public function __construct( $filename = null ) { | |
| 51 | + $this->_filename = trim( $filename ); | |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | /** |
| 55 | 55 | * Fetches series information. |
| @@ -76,24 +76,26 @@ | ||
| 76 | 76 | // read series types |
| 77 | 77 | $types = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ); |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - $labels = array_filter( $labels ); | |
| 81 | - $types = array_filter( $types ); | |
| 82 | - | |
| 83 | 80 | if ( ! $labels || ! $types ) { |
| 84 | - $this->_error = esc_html__( 'File should have a heading row (1st row) and a data type row (2nd row). Please try again.', 'visualizer' ); | |
| 85 | 81 | return false; |
| 86 | 82 | } |
| 87 | 83 | |
| 84 | + // if no types were setup, re read labels and empty types array | |
| 88 | 85 | $types = array_map( 'trim', $types ); |
| 89 | 86 | if ( ! self::_validateTypes( $types ) ) { |
| 90 | - $this->_error = esc_html__( 'Invalid data types detected in the data type row (2nd row). Please try again.', 'visualizer' ); | |
| 91 | - return false; | |
| 87 | + // re open the file | |
| 88 | + fclose( $handle ); | |
| 89 | + $handle = $this->_get_file_handle(); | |
| 90 | + | |
| 91 | + // re read the labels and empty types array | |
| 92 | + $labels = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ); | |
| 93 | + $types = array(); | |
| 92 | 94 | } |
| 93 | 95 | |
| 94 | 96 | for ( $i = 0, $len = count( $labels ); $i < $len; $i++ ) { |
| 95 | - $default_type = $i === 0 ? 'string' : 'number'; | |
| 97 | + $default_type = $i == 0 ? 'string' : 'number'; | |
| 96 | 98 | |
| 97 | 99 | $labels[ $i ] = $this->toUTF8( $labels[ $i ] ); |
| 98 | 100 | |
| 99 | 101 | $this->_series[] = array( |
| @@ -131,9 +133,8 @@ | ||
| 131 | 133 | */ |
| 132 | 134 | public function fetch() { |
| 133 | 135 | // if filename is empty return false |
| 134 | 136 | if ( empty( $this->_filename ) ) { |
| 135 | - $this->_error = esc_html__( 'No file provided. Please try again.', 'visualizer' ); | |
| 136 | 137 | return false; |
| 137 | 138 | } |
| 138 | 139 | |
| 139 | 140 | // read file and fill arrays |
| @@ -144,10 +145,10 @@ | ||
| 144 | 145 | return false; |
| 145 | 146 | } |
| 146 | 147 | |
| 147 | 148 | // fetch data |
| 148 | - // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition | |
| 149 | 149 | while ( ( $data = fgetcsv( $handle, 0, VISUALIZER_CSV_DELIMITER, VISUALIZER_CSV_ENCLOSURE ) ) !== false ) { |
| 150 | + | |
| 150 | 151 | $this->_data[] = $this->_normalizeData( $data ); |
| 151 | 152 | } |
| 152 | 153 | |
| 153 | 154 | // close file handle |