Skip to content

Commit 2ff7626

Browse files
committed
Add source
1 parent 637c38e commit 2ff7626

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

src/Extract.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,34 @@
22

33
namespace NaimSolong\DataExtractor;
44

5+
use Exception;
56
use NaimSolong\DataExtractor\Builder\ExtractBuilder;
67

78
class Extract
89
{
910
protected int $queryId;
1011

11-
protected InstructionsResolver $instructions;
12+
protected ?InstructionsResolver $instruction = null;
13+
14+
protected ?SourcesResolver $source = null;
1215

1316
protected ExtractBuilder $builder;
1417

1518
public function __construct()
1619
{
17-
$this->builder = new ExtractBuilder;
18-
$this->instructions = new InstructionsResolver;
20+
$this->builder = new ExtractBuilder;;
1921
}
2022

2123
public function instruction(int|string $value): self
2224
{
23-
$this->instructions->set($value);
25+
$this->instruction = (new InstructionsResolver)->set($value);
26+
27+
return $this;
28+
}
29+
30+
public function source(string $source): self
31+
{
32+
$this->source = (new SourcesResolver)->set($source);
2433

2534
return $this;
2635
}
@@ -34,7 +43,11 @@ public function queryId(int $queryId): self
3443

3544
public function query(): mixed
3645
{
37-
$source = $this->instructions->source()->toArray();
46+
if (is_null($this->instruction) && is_null($this->source)) {
47+
throw new Exception('Instruction or source are not set.');
48+
}
49+
50+
$source = $this->source?->get()->toArray() ?? $this->instruction?->source()->toArray();
3851

3952
$query = app($source['model'])
4053
->setConnection($source['connection'])

src/SourcesResolver.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace NaimSolong\DataExtractor;
4+
5+
use InvalidArgumentException;
6+
use NaimSolong\DataExtractor\Dto\Source;
7+
8+
class SourcesResolver
9+
{
10+
protected array $sources = [];
11+
12+
protected Source $source;
13+
14+
public function __construct()
15+
{
16+
$this->sources = config('data-extractor.source', []);
17+
}
18+
19+
public function set(string $value): self
20+
{
21+
if (array_key_exists($value, $this->sources)) {
22+
$this->source = Source::fromArray($this->sources[$value]);
23+
24+
return $this;
25+
}
26+
27+
throw new InvalidArgumentException("Invalid source value: {$value}");
28+
}
29+
30+
public function get(): Source
31+
{
32+
return $this->source;
33+
}
34+
}

0 commit comments

Comments
 (0)