Created
May 2, 2018 21:03
-
-
Save raank/e36505343fe14ed23689c066222af213 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Modules\Tracks\Helpers; | |
use PhpOffice\PhpSpreadsheet\IOFactory; | |
class TracksProcess | |
{ | |
public $file; | |
public $exclude; | |
/** | |
* The constructor method | |
* @param $file | |
* @param $exclude | Exclude with Operator > | |
*/ | |
public function __construct($file, $exclude = 1) | |
{ | |
$this->file = $file; | |
$this->exclude = $exclude; | |
} | |
/** | |
* Read XLSX Excel | |
* @return mixed | |
*/ | |
public function getReader() | |
{ | |
$reader = IOFactory::createReader('Xlsx'); | |
$reader->setLoadSheetsOnly('Pedidos'); | |
$reader = $reader->load($this->file); | |
$worksheet = $reader->getActiveSheet(); | |
return $worksheet->toArray(); | |
} | |
/** | |
* Process data | |
* 1 - Exlude arrays null | |
* 2 - Set headers | |
* 3 - Process the data | |
* @return mixed | |
*/ | |
public function getProcessedData() | |
{ | |
try { | |
$data = $this->exclude($this->getReader()); | |
$header = $this->getHeaders($data); | |
unset($data[0]); // remove headers | |
foreach(array_values($data) as $key => $row) | |
{ | |
$rows[] = $this->processDataItem($row, $header); | |
} | |
unset($rows[0]); | |
return isset($rows) ? array_values($rows) : null; | |
// return $header; | |
} catch (\Exception $e) { | |
return $e->__toString(); | |
} | |
} | |
/** | |
* Exclude items | |
* @param $data array | |
* @return mixed | |
*/ | |
public function exclude($data) | |
{ | |
try { | |
$i = 0; | |
while ($i <= $this->exclude) { | |
unset($data[$i]); | |
$i++; | |
} | |
return $data; | |
} catch (\Exception $e) { | |
return $e->__toString(); | |
} | |
} | |
/** | |
* Process the Headers | |
* @return mixed | |
*/ | |
public function getHeaders($data) | |
{ | |
try { | |
$data = array_values($data); | |
if (in_array('Empresa', $data[0])) { | |
foreach($data[0] as $item) | |
{ | |
$headers[] = [ | |
'name' => $item, | |
'key' => str_slug($item) | |
]; | |
} | |
} | |
return isset($headers) ? $headers : null; | |
} catch (\Exception $e) { | |
return $e->__toString(); | |
} | |
} | |
/** | |
* Process data of row | |
* @param $row | |
* @param $headers | |
* @return mixed | |
*/ | |
public function processDataItem($row, $headers) | |
{ | |
try { | |
foreach($row as $key => $item) | |
{ | |
$slug = $headers[$key]['key']; | |
if ($slug == 'protocolo' || $slug == 'no-pedido') { | |
$items[$slug] = (int) $item; | |
} else { | |
$items[$slug] = $item; | |
} | |
} | |
return isset($items) ? $items : null; | |
} catch (\Exception $e) { | |
return $e->__toString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment