Skip to content

Commit 8ed94e5

Browse files
committed
static removed and doc updated
1 parent 4b8b6a1 commit 8ed94e5

File tree

2 files changed

+109
-65
lines changed

2 files changed

+109
-65
lines changed

README.md

Lines changed: 83 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,83 @@ A fast and tiny PHP library to export data to CSV and ExcelSheet. The library is
1010
composer require sujan/php-csv-exporter
1111
```
1212

13-
## Usage
14-
All you have to do is to pass the **Query Builder**
13+
## Basic Usage
14+
```$xslt
15+
$columns = [ 'id', 'name', 'email' ];
1516
17+
$queryBuilder = User::limit(10); // Query Builder
1618
17-
`use Sujan\Exporter\Exporter;
18-
`
19-
```$xslt
20-
$columns = [
21-
'id',
22-
'name',
23-
'email'
24-
];
25-
```
26-
###### From Query Builder (RECOMMENDED)
19+
$exporter = new Exporter();
20+
$exporter->build($queryBuilder, $columns, 'users.csv')
21+
->export();
2722
```
28-
$queryBuilder = User::query(); // Query builder
29-
// $queryBuilder = User::where('email', '[email protected]'); // Query builder
3023

31-
Exporter::init($queryBuilder, $columns, 'users.csv');
32-
// Exporter::init($queryBuilder, $columns, 'users.xlsx'); // For ExcelSheet export
33-
Exporter::export();
34-
```
24+
Build and export, that much simple.
25+
26+
## Documentation
3527

36-
###### From Collection
28+
- [Build CSV](#build-csv)
29+
- [Export CSV](#export-csv)
30+
- [Usage Examples](#usage-examples)
31+
- [Laravel](#laravel)
32+
- [From Eloquent Query Builder (RECOMMENDED)](#from-eloquent-query-builder-recommended)
33+
- [From Collection](#from-collection)
34+
- [From Array](#from-array)
35+
- [Eloquent Relation](#eloquent-relation)
36+
- [RAW PHP](#raw-php)
37+
- [From Array](#from-plain-array)
38+
- [From PDOStatement (RECOMMENDED)](#from-pdostatement-recommended)
39+
40+
### Build CSV
41+
CSV build takes three parameters. First one is the model which could be `Array`, `PDOStatement`, `Eloquent Query Builder` and
42+
`Collection`, seconds one takes the field names you want to export, third one is CSV filename.
43+
44+
```$xslt
45+
$exporter->build($queryBuilder, $columns, 'users.csv');
3746
```
38-
$usersCollection = User::all(); // Collection
3947

40-
Exporter::init($usersCollection, $columns, 'users.csv');
41-
Exporter::export();
48+
### Export CSV
49+
```$xslt
50+
$exporter->export();
4251
```
4352

44-
###### From Array
53+
## Usage Examples
54+
### Laravel
55+
You can export data from `Eloquent Query Builder`, `Collection` and `Array` whereas `Eloquent Query Builder` is highly recommended.
56+
#### From Eloquent Query Builder (RECOMMENDED)
57+
```$xslt
58+
$columns = [ 'id', 'name', 'email' ];
59+
60+
$queryBuilder = User::latest()->whereNotNull('email_verified_at'); // Query Builder
61+
62+
$exporter = new Exporter();
63+
$exporter->build($queryBuilder, $columns, 'users.csv')
64+
->export();
4565
```
46-
$usersArray = User::latest()->get()->toArray(); // Array
4766

48-
Exporter::init($usersArray, $columns, 'users.csv');
49-
Exporter::export();
67+
#### From Collection
68+
```$xslt
69+
$columns = [ 'id', 'name', 'email' ];
70+
71+
$collection = User::latest()->get(); // Collection
72+
73+
$exporter = new Exporter();
74+
$exporter->build($collection, $columns, 'users.csv')
75+
->export();
5076
```
5177

52-
Or you can pass `Collection` or `Array`. But it is **highly recommended** to pass the **`Query Builder`** as it will use generator to save memory usage.
78+
#### From Array
79+
```$xslt
80+
$columns = [ 'id', 'name', 'email' ];
81+
82+
$usersArray = User::latest()->get()->toArray(); // Array of Users
83+
84+
$exporter = new Exporter();
85+
$exporter->build($usersArray, $columns, 'users.csv')
86+
->export();
87+
```
5388

54-
**For eloquent relation**
89+
#### Eloquent Relation
5590
```$xslt
5691
$columns = [
5792
'id',
@@ -63,20 +98,29 @@ $columns = [
6398
6499
$queryBuilder = Post::with('user'); // Query builder
65100
66-
Exporter::init($queryBuilder, $columns, 'users.csv');
67-
Exporter::export();
101+
$exporter = new Exporter();
102+
$exporter->build($queryBuilder, $columns, 'users.csv')
103+
->export();
68104
```
69105

70-
Where `user` is the relation name, which is same as is in the `$columns` variable.
106+
### Raw PHP
107+
The library supports Laravel as well as raw PHP. You can easily export data from `PDOStatement` and `Array`.
71108

72-
## Usage with raw PHP (PDOStatement)
73-
74-
Usage with PDO is straight forward. You **MUST** have to add the following code
109+
#### From Plain Array
75110
```$xslt
76-
$stmt->setFetchMode(PDO::FETCH_ASSOC); // N.B. must be included
111+
$array = [
112+
['id' => 1, 'name' => 'John Doe', 'email' => '[email protected]'],
113+
['id' => 2, 'name' => 'Jane Doe', 'email' => '[email protected]']
114+
];
115+
116+
$columns = ['id', 'name', 'email'];
117+
118+
$exporter = new Exporter();
119+
$exporter->build($array, $columns, 'users.csv')
120+
->export();
77121
```
78122

79-
And then pass the `$stmt` to `Exporter`
123+
#### From PDOStatement (RECOMMENDED)
80124
```$xslt
81125
$servername = "localhost";
82126
$username = "root";
@@ -98,19 +142,14 @@ And then pass the `$stmt` to `Exporter`
98142
// set the resulting array to associative
99143
$stmt->setFetchMode(PDO::FETCH_ASSOC);
100144
101-
Exporter::init($stmt, $userData, 'users.csv');
102-
Exporter::export();
145+
$exporter = new Exporter();
146+
$exporter->build($stmt, $columns, 'users.csv)
147+
->export();
103148
}
104149
catch(PDOException $e) {
105150
echo "Error: " . $e->getMessage();
106151
}
107152
$conn = null;
108153
```
109154

110-
## XLSX export
111-
Just use the file extension as `.xlsx`
112-
```
113-
Exporter::init($queryBuilder, $columns, 'users.xlsx');
114-
```
115-
116155
## You are always welcome to contribute

src/Exporter.php

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,73 @@
55

66

77
use Exception;
8+
use Illuminate\Database\Eloquent\Builder;
9+
use PDOStatement;
810
use Sujan\Exporter\Contracts\ExporterContract;
911

1012
class Exporter
1113
{
12-
protected static $exporter;
14+
protected $exporter;
1315

1416
/**
1517
* @param $model
1618
* @param array $columns
1719
* @param string $filename
20+
* @return Exporter
1821
* @throws Exception
1922
*/
20-
public static function init($model, array $columns, string $filename): void
23+
public function build($model, array $columns, string $filename)
2124
{
2225
if (gettype($model) == 'string') throw new Exception('Type string for model is not allowed.');
2326

24-
self::setExporter($model, $columns, $filename);
25-
}
27+
$this->setExporter($model, $columns, $filename);
2628

27-
/**
28-
* Export the data and die
29-
*/
30-
public static function export(): void
31-
{
32-
self::$exporter->get();
29+
return $this;
3330
}
3431

3532
/**
36-
* @param array | object $model
33+
* @param array | Builder | PDOStatement $model
3734
* @param $columns
3835
* @param $filename
39-
* @return ExporterContract
36+
* @return Exporter
4037
* @throws Exception
4138
*/
42-
private static function setExporter($model, $columns, $filename): ExporterContract
39+
private function setExporter($model, $columns, $filename)
4340
{
4441
if (is_array($model)) {
45-
self::$exporter = new ExportFromArray($model, $columns, $filename);
46-
self::$exporter->set();
42+
$this->exporter = new ExportFromArray($model, $columns, $filename);
43+
$this->exporter->set();
4744

48-
return self::$exporter;
45+
return $this;
4946
}
5047

5148
/** @var object $model */
5249
$className = class_basename($model);
5350

5451
switch ($className) {
5552
case 'Collection':
56-
self::$exporter = new ExportFromArray($model, $columns, $filename);
53+
$this->exporter = new ExportFromArray($model, $columns, $filename);
5754
break;
5855
case 'Builder':
59-
self::$exporter = new ExportFromQueryBuilder($model, $columns, $filename);
56+
$this->exporter = new ExportFromQueryBuilder($model, $columns, $filename);
6057
break;
6158
case 'PDOStatement':
62-
self::$exporter = new ExportFromPDOStatement($model, $columns, $filename);
59+
$this->exporter = new ExportFromPDOStatement($model, $columns, $filename);
6360
break;
6461
default:
6562
throw new Exception('Type unknown');
6663
}
6764

68-
self::$exporter->set();
65+
$this->exporter->set();
6966

70-
return self::$exporter;
67+
return $this;
68+
}
69+
70+
/**
71+
* Export the data and die
72+
*/
73+
public function export(): void
74+
{
75+
$this->exporter->get();
7176
}
7277
}

0 commit comments

Comments
 (0)