Skip to content

Commit 1e7fd68

Browse files
committed
Update classes
1 parent 9f909ea commit 1e7fd68

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

src/Dto/Instruction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace NaimSolong\DataExtractor\Dto;
44

5-
use ExtractBuilder;
5+
use NaimSolong\DataExtractor\Builder\ExtractBuilder;
66

77
readonly class Instruction
88
{

src/Dto/Source.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace NaimSolong\DataExtractor\Dto;
44

5+
use Exception;
6+
use Illuminate\Database\Eloquent\Model;
7+
58
readonly class Source
69
{
710
public function __construct(
@@ -12,6 +15,10 @@ public function __construct(
1215

1316
public static function fromArray(array $data): self
1417
{
18+
if (!is_subclass_of($data['model'], Model::class)) {
19+
throw new Exception('The provided model, parent must be an instance of Illuminate\Database\Eloquent\Model');
20+
}
21+
1522
return new self(
1623
model: $data['model'],
1724
connection: $data['connection'] ?? config('database.default'),

src/Extract.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,22 @@ public function queryId(int $queryId): self
3333
return $this;
3434
}
3535

36-
public function query(string $format): string
36+
public function query(): mixed
3737
{
38-
$this->builder->createBuilder($format);
39-
4038
$source = $this->instructions->source()->toArray();
4139

4240
$query = app($source['model'])
4341
->setConnection($source['connection'])
44-
->with($source['relationships'] ?? [])
45-
->where('id', $this->queryId);
42+
->with($source['relationships'] ?? []);
4643

47-
if (! $query->exists()) {
48-
throw new ModelNotFoundException("No record found with ID {$this->queryId} in the {$source['model']} model.");
49-
}
44+
return $query->findOrFail($this->queryId);
45+
}
5046

51-
$data = $query->first();
47+
public function extract(string $format): string
48+
{
49+
$this->builder->createBuilder($format);
5250

53-
ray($data);
51+
$data = $this->query();
5452

5553
return $this->builder
5654
->setModel($data)
@@ -59,11 +57,11 @@ public function query(string $format): string
5957

6058
public function toCsv(): string
6159
{
62-
return $this->query(ExtractBuilder::FORMAT_CSV);
60+
return $this->extract(ExtractBuilder::FORMAT_CSV);
6361
}
6462

6563
public function toSql(): string
6664
{
67-
return $this->query(ExtractBuilder::FORMAT_SQL);
65+
return $this->extract(ExtractBuilder::FORMAT_SQL);
6866
}
6967
}

src/InstructionsResolver.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,15 @@ public function set(int|string $value): self
4949
}
5050

5151
if (is_string($value)) {
52-
$this->instruction = array_filter($this->instructions, function ($instruction) use ($value) {
52+
$filteredInstructions = array_filter($this->instructions, function ($instruction) use ($value) {
5353
return $instruction->name === $value;
54-
})[0];
54+
});
5555

56-
return $this;
56+
if(count($filteredInstructions) > 0) {
57+
$this->instruction = $filteredInstructions[0];
58+
59+
return $this;
60+
}
5761
}
5862

5963
throw new InvalidArgumentException("Invalid instruction value: {$value}");

0 commit comments

Comments
 (0)