Skip to content

Commit

Permalink
initiate
Browse files Browse the repository at this point in the history
  • Loading branch information
kentreez committed Jul 28, 2022
1 parent 6fd1a3b commit dc8218a
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.idea
/vendor
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## EasyFileReader
Reading each lines data from file (.txt, .csv, .xls, .xlsx). The library using [PHP Generator](https://www.php.net/manual/en/language.generators.overview.php) then input file will be read line by line to avoid store total file data in memory.

### *** Caution ***
Because of using Generator for iterate over input file's rows. **DO NOT BREAK** the loop while iterate over file's rows it will cause unexpected behavior.

### Usage:
<?php

use Kentreez\EasyFileReader\EasyFileReader;
require_once 'vendor/autoload.php';
$easy = EasyFileReader::Read('example.xlsx');
// count all rows
$easy->count();
// iterate over rows in input file
foreach ($easy->rows() as $rows) {
print_r($rows);
}

?>
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "kentreez/easy-file-reader",
"description": "Simple way for reading .txt, .msv, .xls, .xlsx",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Kentreez\\EasyFileReader\\": "src/"
}
},
"authors": [
{
"name": "Kasidiss Jumrus",
"email": "[email protected]"
}
],
"minimum-stability": "stable",
"require": {
"phpoffice/phpspreadsheet": "^1.24"
}
}
28 changes: 28 additions & 0 deletions src/EasyFileReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Kentreez\EasyFileReader;

use Kentreez\EasyFileReader\Reader\AbstractReader;
use Kentreez\EasyFileReader\Reader\CsvReader;
use Kentreez\EasyFileReader\Reader\TxtReader;
use Kentreez\EasyFileReader\Reader\XlsReader;
use Kentreez\EasyFileReader\Reader\XlsxReader;

class EasyFileReader
{
public static function Read (string $filepath): AbstractReader
{
$extension = strtolower(pathinfo($filepath, PATHINFO_EXTENSION));

switch ($extension) {
case 'txt':
return new TxtReader($filepath);
case 'csv':
return new CsvReader($filepath);
case 'xls':
return new XlsReader($filepath);
case 'xlsx':
return new XlsxReader($filepath);
}
}
}
10 changes: 10 additions & 0 deletions src/Exceptions/ReaderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Kentreez\EasyFileReader\Exceptions;

use Exception;

class ReaderException extends Exception
{
//
}
10 changes: 10 additions & 0 deletions src/Exceptions/UnhandledFileExtensionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Kentreez\EasyFileReader\Exceptions;

use Exception;

class UnhandledFileExtensionException extends Exception
{
//
}
35 changes: 35 additions & 0 deletions src/Reader/AbstractReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Kentreez\EasyFileReader\Reader;

use Generator;
use Kentreez\EasyFileReader\Exceptions\ReaderException;

abstract class AbstractReader
{
protected string $filepath;
protected ?int $count_rows = null;

public function __construct (string $filepath)
{
$this->filepath = $filepath;
}

/**
* @throws ReaderException
* @return Generator
*/
abstract function rows (): Generator;

/**
* @throws ReaderException
*/
public function count (): int
{
if (is_null($this->count_rows)) {
$this->count_rows = iterator_count($this->rows());
}

return $this->count_rows;
}
}
17 changes: 17 additions & 0 deletions src/Reader/CsvReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Kentreez\EasyFileReader\Reader;

use Generator;

class CsvReader extends AbstractReader
{
function rows (): Generator
{
$fp = fopen($this->filepath, 'r');
while ($row = fgetcsv($fp)) {
yield $row;
}
fclose($fp);
}
}
61 changes: 61 additions & 0 deletions src/Reader/ExcelReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Kentreez\EasyFileReader\Reader;

use Generator;
use Kentreez\EasyFileReader\Exceptions\ReaderException;
use PhpOffice\PhpSpreadsheet\Exception as ExcelSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Reader\Exception as ExcelReaderException;

abstract class ExcelReader extends AbstractReader
{
abstract protected function getReaderClass(): string;

/**
* Default look up highest column on every rows
*
* @var int|null
*/
private static ?int $lookupHighestColumnAtRow = null;

/**
* Specify row number that will look up for highest column
* Set to null will look up highest column on every rows
*
* @param int|null $row
* @return void
*/
public static function SetLookupHighestColumnAtRow (?int $row)
{
self::$lookupHighestColumnAtRow = $row;
}

function rows (): Generator
{
try {
$reader = new ($this->getReaderClass());
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($this->filepath);
$sheet = $spreadsheet->getSheet(0);

if (!is_null(self::$lookupHighestColumnAtRow)) {
$maxColumn = $sheet->getHighestColumn(self::$lookupHighestColumnAtRow);
}

for ($row = 1, $num_rows = $sheet->getHighestDataRow(); $row <= $num_rows; $row++) {

if (is_null(self::$lookupHighestColumnAtRow)) {
$maxColumn = $sheet->getHighestColumn($row);
}

$record = [];
for ($col = 'A'; $col <= $maxColumn; $col++) {
$record[] = $sheet->getCell("{$col}{$row}")->getValue();
}
yield $record;
}
} catch (ExcelReaderException|ExcelSpreadsheetException $e) {
throw new ReaderException($e->getMessage(), $e->getCode(), $e);
}
}
}
17 changes: 17 additions & 0 deletions src/Reader/TxtReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Kentreez\EasyFileReader\Reader;

use Generator;

class TxtReader extends AbstractReader
{
function rows (): Generator
{
$fp = fopen($this->filepath, 'r');
while ($line = fgets($fp)) {
yield [$line];
}
fclose($fp);
}
}
17 changes: 17 additions & 0 deletions src/Reader/XlsReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Kentreez\EasyFileReader\Reader;

use Generator;
use Kentreez\EasyFileReader\Exceptions\ReaderException;
use PhpOffice\PhpSpreadsheet\Exception as ExcelSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Reader\Exception as ExcelReaderException;
use PhpOffice\PhpSpreadsheet\Reader\Xls;

class XlsReader extends ExcelReader
{
protected function getReaderClass (): string
{
return Xls::class;
}
}
13 changes: 13 additions & 0 deletions src/Reader/XlsxReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Kentreez\EasyFileReader\Reader;

use PhpOffice\PhpSpreadsheet\Reader\Xlsx;

class XlsxReader extends ExcelReader
{
protected function getReaderClass (): string
{
return Xlsx::class;
}
}

0 comments on commit dc8218a

Please sign in to comment.