Skip to content

Commit

Permalink
Merge 6869a52 into 2654fb9
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaGot committed Feb 14, 2014
2 parents 2654fb9 + 6869a52 commit 03bac8d
Show file tree
Hide file tree
Showing 12 changed files with 415 additions and 138 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
],
"require": {
"php": ">=5.3.0",
"symfony/yaml": "2.3.*"
"symfony/yaml": "2.3.*",
"phpoffice/phpexcel": "dev-master"
},
"require-dev": {
"satooshi/php-coveralls": "dev-master"
Expand Down
7 changes: 4 additions & 3 deletions src/CSVMapper/Reader/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private function isFileOk()

private function checkColumnsNumber()
{
$fileColumns = count(explode($this->file->getSeparator(),fgets($this->file->getHandler())));
$fileColumns = $this->file->getFileColumns();
$allowedColumns = $this->file->getColumnsAllowed();
$this->file->reset();
if($allowedColumns && $fileColumns != $allowedColumns)
Expand All @@ -58,13 +58,14 @@ private function checkColumnsNumber()

public function getNextRow()
{
$rawRow = explode($this->file->getSeparator(), fgets($this->file->getHandler()));
$rawRow = $this->file->getRawRow();
return $this->parser->parse($rawRow);
}

public function hasNextRow()
{
return !feof($this->file->getHandler());
$hasRow = $this->file->hasRow();
return $hasRow;
}

public function close()
Expand Down
59 changes: 37 additions & 22 deletions src/CSVMapper/Source/CsvFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,58 @@
*
* @author danorler
*/
class CsvFile extends File
{
class CsvFile extends File {

private $separator = null;
private $columnsAllowed = null;

public function getSeparator()
{
if(empty($this->separator))
{

public function getSeparator() {
if (empty($this->separator)) {
return ";";
}
else
{
} else {
return $this->separator;
}
}

public function setSeparator($separator)
{
public function setSeparator($separator) {
$this->separator = $separator;
}

public function getColumnsAllowed()
{
if(empty($this->columnsAllowed))
{

public function getColumnsAllowed() {
if (empty($this->columnsAllowed)) {
return FALSE;
}
else
{
} else {
return $this->columnsAllowed;
}
}

public function setColumnsAllowed($columnsAllowed)
{
public function setColumnsAllowed($columnsAllowed) {
$this->columnsAllowed = $columnsAllowed;
}

public function openFile($path) {
$handler = fopen($path, "r");
return $handler;
}

public function reset() {
if (!empty($this->handler)) {
fseek($this->handler, 0);
}
}

public function getFileColumns() {
$fileColumns = count(explode($this->getSeparator(), fgets($this->getHandler())));
return $fileColumns;
}

public function getRawRow() {
$rawRow = explode($this->getSeparator(), fgets($this->getHandler()));
return $rawRow;
}

public function hasRow() {
return !feof($this->getHandler());
}

}
102 changes: 102 additions & 0 deletions src/CSVMapper/Source/ExcelFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of ExcelFile
*
* @author agottardi
*/

namespace CSVMapper\Source;

use PHPExcel_IOFactory;
use PHPExcel_Cell;

class ExcelFile extends File {

private $rowNumber = 1;

public function getColumnsAllowed() {
if (empty($this->columnsAllowed)) {
return FALSE;
} else {
return $this->columnsAllowed;
}
}

public function setColumnsAllowed($columnsAllowed) {
$this->columnsAllowed = $columnsAllowed;
}

public function openFile($path) {
$inputFileName = $path;
/** Identify the type of $inputFileName * */
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
/** Create a new Reader of the type that has been identified * */
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/** Load $inputFileName to a PHPExcel Object * */
$objPHPExcel = $objReader->load($inputFileName);
$this->rowNumber = 1;
return $objPHPExcel;
}

public function getFileColumns() {
$objPHPExcel = $this->getHandler();
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
return $highestColumnIndex-1;
}

public function getRawRow() {

$values = array();
$objPHPExcel = $this->getHandler();
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestColumnIndex = $this->getFileColumns();


for ($col = 0; $col <= $highestColumnIndex; $col++) {

$cell = $objWorksheet->getCellByColumnAndRow($col, $this->rowNumber);
$cellValue = $cell->getValue();

array_push($values, $cellValue);
}

$this->rowNumber++;

return $values;
}

public function hasRow() {

$objPHPExcel = $this->getHandler();
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);

$highestRow = $objWorksheet->getHighestRow(); // e.g 'F'

if ($highestRow == 1 && $objWorksheet->getCellByColumnAndRow(0, 1)->getValue() == null) {
return false;
} else if ($highestRow >= $this->rowNumber) {
return true;
} else if ($highestRow < $this->rowNumber) {
return false;
}
}

public function reset() {
$this->rowNumber = 1;
}

public function getHighestColumnIndex()
{

}

}
11 changes: 2 additions & 9 deletions src/CSVMapper/Source/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function setPath($path)

public function open()
{
$this->handler = fopen($this->getPath(), "r");
$this->handler = $this->openFile($this->getPath());
return $this->handler;
}

Expand All @@ -75,14 +75,7 @@ public function close()
}
$this->handler = null;
}

public function reset()
{
if(!empty($this->handler))
{
fseek($this->handler,0);
}
}


public function checkProperty($key)
{
Expand Down

0 comments on commit 03bac8d

Please sign in to comment.