Skip to content

Commit 1303d9c

Browse files
committed
Refactor
1 parent dd31625 commit 1303d9c

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

src/Extract.php

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace NaimSolong\DataExtractor;
44

55
use Exception;
6+
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Support\Collection;
78
use NaimSolong\DataExtractor\Builder\ExtractBuilder;
89

@@ -50,7 +51,41 @@ public function queryId(int|array $queryId): self
5051
return $this;
5152
}
5253

53-
public function query(): mixed
54+
public function toCsv(?Collection $models = null): array
55+
{
56+
return $this->extract(ExtractBuilder::FORMAT_CSV, $models);
57+
}
58+
59+
public function toSql(?Collection $models = null): array
60+
{
61+
return $this->extract(ExtractBuilder::FORMAT_SQL, $models);
62+
}
63+
64+
public function extract(string $format, ?Collection $models = null): array
65+
{
66+
$this->builder->createBuilder($format);
67+
68+
$models = $this->validateCustomModel($models) ?? $this->query();
69+
70+
// Flatten all models and their relationships
71+
$this->flattenRelation($models);
72+
73+
// Build result
74+
$this->buildResult();
75+
76+
return $this->results;
77+
}
78+
79+
public function validateCustomModel(Collection $models): Collection
80+
{
81+
if(!is_subclass_of($models->first(), Model::class)) {
82+
throw new Exception('The provided model, parent must be an instance of Illuminate\Database\Eloquent\Model');
83+
}
84+
85+
return $models;
86+
}
87+
88+
protected function query(): mixed
5489
{
5590
if (is_null($this->option) && is_null($this->source)) {
5691
throw new Exception('Option or source are not set.');
@@ -69,9 +104,11 @@ public function query(): mixed
69104
return $query->get();
70105
}
71106

72-
public function flattenRelation(Collection $models): void
107+
protected function flattenRelation(Collection $models): void
73108
{
74109
$models->each(function ($model) {
110+
$this->datas[] = $model;
111+
75112
// Get all loaded relations
76113
$relations = $model->getRelations();
77114

@@ -91,7 +128,7 @@ public function flattenRelation(Collection $models): void
91128
});
92129
}
93130

94-
public function buildResult(): void
131+
protected function buildResult(): void
95132
{
96133
collect($this->datas)->unique(function ($data) {
97134
return $data::class.$data['id'];
@@ -101,29 +138,4 @@ public function buildResult(): void
101138
->build();
102139
});
103140
}
104-
105-
public function toCsv(): array
106-
{
107-
return $this->extract(ExtractBuilder::FORMAT_CSV);
108-
}
109-
110-
public function toSql(): array
111-
{
112-
return $this->extract(ExtractBuilder::FORMAT_SQL);
113-
}
114-
115-
public function extract(string $format): array
116-
{
117-
$this->builder->createBuilder($format);
118-
119-
$models = $this->query();
120-
121-
// Flatten all models and their relationships
122-
$this->flattenRelation($models);
123-
124-
// Build result
125-
$this->buildResult();
126-
127-
return $this->results;
128-
}
129141
}

0 commit comments

Comments
 (0)