<?php

namespace common\components;

class XLSUtil {
	
	public $objPHPExcel;
	
	public function loadFromFileName($inputFileName) {
		// Read your Excel workbook
		try {
			$inputFileType = \PHPExcel_IOFactory::identify ( $inputFileName );
			$objReader = \PHPExcel_IOFactory::createReader ( $inputFileType );
			$this->objPHPExcel = $objReader->load ( $inputFileName );
			echo "type is: " . $inputFileType;
		} catch ( Exception $e ) {
			\Yii::error ( "failed to read xls" );
			throw $e;
		}
	}
	
	/**
	 * first sheet to array
	 */
	public function toArray( ) {
		$objPHPExcel = $this->objPHPExcel;
		$array = [ ];
		// Get worksheet dimensions
		$sheet = $objPHPExcel->getActiveSheet();
		//$highestColumn = $sheet->getHighestColumn();
		$hr = $sheet->getHighestRow();
		foreach ( $sheet->getRowIterator () as $row ) {
			$arrayRow = []; 
			$cellIterator = $row->getCellIterator ();
			$cellIterator->setIterateOnlyExistingCells ( false ); // Loop all cells, even if it is not set
			foreach ( $cellIterator as $cell ) {
				$arrayRow [] = $cell->getCalculatedValue();
			}
			$array[] = $arrayRow;
		}
		
		return $array;
	}
}