Skip to content

Commit bb4ac7a

Browse files
committed
code comment and type hinting added
1 parent e463ebf commit bb4ac7a

File tree

6 files changed

+72
-21
lines changed

6 files changed

+72
-21
lines changed

src/Contracts/ExporterContract.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66

77

8+
use Generator;
9+
810
interface ExporterContract
911
{
10-
public function set();
11-
public function get();
12+
public function set(): Generator;
13+
public function get(): void;
1214
}

src/Export.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
namespace Sujan\Exporter;
55

66

7-
use Exception;
8-
use Sujan\Exporter\Contracts\ExporterContract;
97

108
class Export
119
{
@@ -33,10 +31,13 @@ class Export
3331
* Array of content types
3432
*/
3533
private $contentTypes = [
36-
'.csv' => 'application/csv',
37-
'.xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
34+
'csv' => 'application/csv',
35+
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
3836
];
3937

38+
/*
39+
* Content type
40+
*/
4041
private $contentType;
4142

4243
/**
@@ -49,14 +50,18 @@ class Export
4950
*/
5051
protected $delimiter = ";";
5152

53+
/**
54+
* @param array | object $model
55+
* @param array $columns
56+
* @param string $filename
57+
*/
5258
protected function setConfiguration($model, array $columns, string $filename)
5359
{
5460
$this->setModel($model);
5561
$this->setColumns($columns);
5662
$this->setHeading($columns);
5763
$this->setFilename($filename);
5864
$this->setContentType();
59-
$this->openOutputStream();
6065
}
6166

6267
/**
@@ -102,18 +107,27 @@ public function setContentType()
102107

103108
if (!$parts[0]) {
104109
$this->filename = $this->filename . '.csv';
105-
$this->contentType = $this->contentTypes['.csv'];
110+
$this->contentType = $this->contentTypes['csv'];
106111
} else {
107-
$this->contentType = $this->contentTypes[strtolower($parts[0])];
112+
$this->contentType = $this->contentTypes[trim(strtolower($parts[0]), '.')];
108113
}
109114

110115
header("Content-Type: {$this->contentType}");
111116
header("Content-Disposition: attachment; filename={$this->filename};");
112117
}
113118

114-
private function openOutputStream()
119+
/**
120+
* Open the "output" stream
121+
*/
122+
protected function openOutputStream()
115123
{
116-
// open the "output" stream
117124
$this->file = fopen('php://output', 'w');
118125
}
126+
127+
/**
128+
* Close output stream
129+
*/
130+
protected function closeOutputStream() {
131+
fclose($this->file);
132+
}
119133
}

src/ExportFromArray.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Sujan\Exporter;
55

66

7+
use Generator;
78
use Sujan\Exporter\Contracts\ExporterContract;
89
use Sujan\Exporter\Traits\ExportGenerator;
910

@@ -12,28 +13,38 @@ class ExportFromArray extends Export implements ExporterContract
1213
{
1314
use ExportGenerator;
1415

15-
public function __construct($model, $columns, $filename)
16+
public function __construct(array $model, array $columns, string $filename)
1617
{
1718
$this->setConfiguration($model, $columns, $filename);
1819
}
1920

20-
public function set()
21+
/**
22+
* @return Generator
23+
*/
24+
public function set(): Generator
2125
{
26+
$this->openOutputStream();
27+
2228
foreach ($this->model as $data) {
2329
$line = $this->getLine($data, $this->columns);
2430

2531
yield fputcsv($this->file, $line, $this->delimiter);
2632
}
2733
}
2834

29-
public function get()
35+
/**
36+
* Get data out of generator
37+
*/
38+
public function get(): void
3039
{
3140
$generator = $this->set();
3241

3342
while ($generator->valid()) {
3443
$generator->next();
3544
}
3645

46+
$this->closeOutputStream();
47+
3748
die();
3849
}
3950
}

src/ExportFromPDOStatement.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
namespace Sujan\Exporter;
55

66

7+
use Generator;
8+
use PDOStatement;
79
use Sujan\Exporter\Contracts\ExporterContract;
810
use Sujan\Exporter\Traits\ExportGenerator;
911

@@ -12,28 +14,38 @@ class ExportFromPDOStatement extends Export implements ExporterContract
1214
{
1315
use ExportGenerator;
1416

15-
public function __construct($model, $columns, $filename)
17+
public function __construct(PDOStatement $model, array $columns, string $filename)
1618
{
1719
$this->setConfiguration($model, $columns, $filename);
1820
}
1921

20-
public function set()
22+
/**
23+
* @return Generator
24+
*/
25+
public function set(): Generator
2126
{
27+
$this->openOutputStream();
28+
2229
foreach ($this->model->fetchAll() as $data) {
2330
$line = $this->getLine($data, $this->columns);
2431

2532
yield fputcsv($this->file, $line, $this->delimiter);
2633
}
2734
}
2835

29-
public function get()
36+
/**
37+
* Get data from PDO statement
38+
*/
39+
public function get(): void
3040
{
3141
$generator = $this->set();
3242

3343
while ($generator->valid()) {
3444
$generator->next();
3545
}
3646

47+
$this->closeOutputStream();
48+
3749
die();
3850
}
3951
}

src/ExportFromQueryBuilder.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
namespace Sujan\Exporter;
55

66

7+
use Generator;
8+
use Illuminate\Database\Eloquent\Builder;
79
use Sujan\Exporter\Contracts\ExporterContract;
810
use Sujan\Exporter\Traits\ExportGenerator;
911

@@ -12,28 +14,38 @@ class ExportFromQueryBuilder extends Export implements ExporterContract
1214
{
1315
use ExportGenerator;
1416

15-
public function __construct($model, $columns, $filename)
17+
public function __construct(Builder $model, array $columns, string $filename)
1618
{
1719
$this->setConfiguration($model, $columns, $filename);
1820
}
1921

20-
public function set()
22+
/**
23+
* @return Generator
24+
*/
25+
public function set(): Generator
2126
{
27+
$this->openOutputStream();
28+
2229
foreach ($this->model->get() as $data) {
2330
$line = $this->getLine($data, $this->columns);
2431

2532
yield fputcsv($this->file, $line, $this->delimiter);
2633
}
2734
}
2835

29-
public function get()
36+
/*
37+
* Get data from query builder
38+
*/
39+
public function get(): void
3040
{
3141
$generator = $this->set();
3242

3343
while ($generator->valid()) {
3444
$generator->next();
3545
}
3646

47+
$this->closeOutputStream();
48+
3749
die();
3850
}
3951
}

src/Exporter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static function export(): void
3333
}
3434

3535
/**
36-
* @param $model
36+
* @param array | object $model
3737
* @param $columns
3838
* @param $filename
3939
* @return ExporterContract

0 commit comments

Comments
 (0)