-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/.idea | ||
/vendor |
This file contains 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
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); | ||
} | ||
|
||
?> |
This file contains 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
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" | ||
} | ||
} |
This file contains 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
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); | ||
} | ||
} | ||
} |
This file contains 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
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 | ||
{ | ||
// | ||
} |
This file contains 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
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 | ||
{ | ||
// | ||
} |
This file contains 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
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; | ||
} | ||
} |
This file contains 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
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); | ||
} | ||
} |
This file contains 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
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); | ||
} | ||
} | ||
} |
This file contains 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
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); | ||
} | ||
} |
This file contains 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
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; | ||
} | ||
} |
This file contains 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
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; | ||
} | ||
} |