Skip to content

Commit 2eca733

Browse files
committed
Rename from instruction to option
1 parent 7e6c933 commit 2eca733

File tree

6 files changed

+122
-122
lines changed

6 files changed

+122
-122
lines changed

config/data-extractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
return [
88
'is_enabled' => env('DATA_EXTRACTOR_ENABLED', false),
99

10-
'instructions' => [
10+
'options' => [
1111
[
1212
'name' => 'Default',
1313
'description' => 'Extra all user data',

src/Commands/DataExtractCommand.php

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ class DataExtractCommand extends Command
99
{
1010
public $signature = 'data:extract';
1111

12-
public $description = 'Extract data based on predefined instructions';
12+
public $description = 'Extract data based on predefined options';
1313

14-
public array $instructions = [];
14+
public array $options = [];
1515

1616
public function __construct()
1717
{
18-
$this->instructions = config('data-extractor.instructions', []);
18+
$this->options = config('data-extractor.options', []);
1919

2020
parent::__construct();
2121
}
@@ -28,21 +28,21 @@ public function handle(): int
2828
return self::FAILURE;
2929
}
3030

31-
if ($this->validateInstructions() === false) {
31+
if ($this->validateOptions() === false) {
3232
return self::FAILURE;
3333
}
3434

35-
$selectedInstructions = $this->promptInstructions();
35+
$selectedOptions = $this->promptOptions();
3636

37-
$id = $this->promptModelId($selectedInstructions);
37+
$id = $this->promptModelId($selectedOptions);
3838

3939
if ($id <= 0) {
4040
return self::FAILURE;
4141
}
4242

43-
$selectedSource = $selectedInstructions['source'] ?? null;
43+
$selectedSource = $selectedOptions['source'] ?? null;
4444
if (! $selectedSource) {
45-
$this->error('No source specified in the selected instruction.');
45+
$this->error('No source specified in the selected option.');
4646

4747
return self::FAILURE;
4848
}
@@ -88,39 +88,39 @@ public function handle(): int
8888
return self::SUCCESS;
8989
}
9090

91-
protected function validateInstructions(): bool
91+
protected function validateOptions(): bool
9292
{
93-
if (empty($this->instructions)) {
94-
$this->error('No instructions found in the configuration.');
93+
if (empty($this->options)) {
94+
$this->error('No options found in the configuration.');
9595

9696
return false;
9797
}
9898

9999
$sourceConnections = array_keys(config('data-extractor.source'));
100100

101-
foreach ($this->instructions as $instruction) {
102-
if (empty($instruction['name']) || empty($instruction['source']) || empty($instruction['export'])) {
103-
$this->error('Invalid instruction format. Each instruction must have a name, source, and export configuration.');
101+
foreach ($this->options as $option) {
102+
if (empty($option['name']) || empty($option['source']) || empty($option['export'])) {
103+
$this->error('Invalid option format. Each option must have a name, source, and export configuration.');
104104

105105
return false;
106106
}
107107

108-
if (! in_array($instruction['export']['format'], Export::FORMATS)) {
109-
$this->error('Invalid export format in instruction: '.$instruction['name']);
108+
if (! in_array($option['export']['format'], Export::FORMATS)) {
109+
$this->error('Invalid export format in option: '.$option['name']);
110110

111111
return false;
112112
}
113113

114-
if (! isset($instruction['source']) || ! is_string($instruction['source']) || ! in_array($instruction['source'], $sourceConnections)) {
115-
$this->error('Invalid source specified in instruction: '.$instruction['name']);
114+
if (! isset($option['source']) || ! is_string($option['source']) || ! in_array($option['source'], $sourceConnections)) {
115+
$this->error('Invalid source specified in option: '.$option['name']);
116116

117117
return false;
118118
}
119119

120-
$selectedSource = config("data-extractor.source.{$instruction['source']}", []);
120+
$selectedSource = config("data-extractor.source.{$option['source']}", []);
121121

122122
if (! isset($selectedSource['model'])) {
123-
$this->error('Invalid model configuration in source: '.$instruction['source']);
123+
$this->error('Invalid model configuration in source: '.$option['source']);
124124

125125
return false;
126126
}
@@ -129,46 +129,46 @@ protected function validateInstructions(): bool
129129
return true;
130130
}
131131

132-
protected function promptInstructions(): array
132+
protected function promptOptions(): array
133133
{
134134
$this->table(
135135
['Name', 'Description', 'Export Format'],
136-
array_map(function ($instruction) {
136+
array_map(function ($option) {
137137
return [
138-
$instruction['name'],
139-
$instruction['description'] ?? 'N/A',
140-
$instruction['export']['format'],
138+
$option['name'],
139+
$option['description'] ?? 'N/A',
140+
$option['export']['format'],
141141
];
142-
}, $this->instructions)
142+
}, $this->options)
143143
);
144144

145-
$instructionNames = array_column($this->instructions, 'name');
145+
$optionNames = array_column($this->options, 'name');
146146

147147
$selectedKey = array_keys($this->choice(
148-
'Select an instruction to execute',
149-
$instructionNames,
148+
'Select an option to execute',
149+
$optionNames,
150150
null,
151151
null,
152152
true
153153
));
154154

155-
return $this->instructions[$selectedKey[0]];
155+
return $this->options[$selectedKey[0]];
156156
}
157157

158-
protected function promptModelId($instruction): int
158+
protected function promptModelId($option): int
159159
{
160-
$source = $instruction['source'] ?? null;
160+
$source = $option['source'] ?? null;
161161

162162
if (! $source || ! is_string($source)) {
163-
$this->error('Invalid source specified in the instruction.');
163+
$this->error('Invalid source specified in the option.');
164164

165165
return 0;
166166
}
167167

168168
$modelClass = config("data-extractor.source.$source.model");
169169

170170
if (! $modelClass || ! class_exists($modelClass)) {
171-
$this->error('Invalid model class specified in the instruction source.');
171+
$this->error('Invalid model class specified in the option source.');
172172

173173
return 0;
174174
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use NaimSolong\DataExtractor\Builder\ExtractBuilder;
66

7-
readonly class Instruction
7+
readonly class Option
88
{
99
public function __construct(
1010
public string $name,

src/Extract.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Extract
99
{
1010
protected int $queryId;
1111

12-
protected ?InstructionsResolver $instruction = null;
12+
protected ?OptionsResolver $option = null;
1313

1414
protected ?SourcesResolver $source = null;
1515

@@ -24,9 +24,9 @@ public function __construct()
2424
$this->builder = new ExtractBuilder;
2525
}
2626

27-
public function instruction(int|string $value): self
27+
public function option(int|string $value): self
2828
{
29-
$this->instruction = (new InstructionsResolver)->set($value);
29+
$this->option = (new OptionsResolver)->set($value);
3030

3131
return $this;
3232
}
@@ -47,11 +47,11 @@ public function queryId(int $queryId): self
4747

4848
public function query(): mixed
4949
{
50-
if (is_null($this->instruction) && is_null($this->source)) {
51-
throw new Exception('Instruction or source are not set.');
50+
if (is_null($this->option) && is_null($this->source)) {
51+
throw new Exception('Option or source are not set.');
5252
}
5353

54-
$source = $this->source?->get()->toArray() ?? $this->instruction?->source()->toArray();
54+
$source = $this->source?->get()->toArray() ?? $this->option?->source()->toArray();
5555

5656
$query = app($source['model'])
5757
->setConnection($source['connection'])

src/InstructionsResolver.php

Lines changed: 0 additions & 80 deletions
This file was deleted.

src/OptionsResolver.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace NaimSolong\DataExtractor;
4+
5+
use InvalidArgumentException;
6+
use NaimSolong\DataExtractor\Dto\Export;
7+
use NaimSolong\DataExtractor\Dto\Option;
8+
use NaimSolong\DataExtractor\Dto\Source;
9+
10+
class OptionsResolver
11+
{
12+
protected array $options = [];
13+
14+
protected Option $option;
15+
16+
public function __construct()
17+
{
18+
$this->resolveInstance();
19+
}
20+
21+
protected function resolveInstance(): void
22+
{
23+
$configOptions = config('data-extractor.options', []);
24+
$configSource = config('data-extractor.source', []);
25+
$configExport = config('data-extractor.export', []);
26+
27+
foreach ($configOptions as $option) {
28+
$source = (array_key_exists($option['source'], $configSource)) ? $configSource[$option['source']] : [];
29+
$export = (array_key_exists($option['export'], $configExport)) ? $configExport[$option['export']] : [];
30+
31+
$this->options[] = Option::fromArray([
32+
'name' => $option['name'],
33+
'description' => $option['description'],
34+
'format' => $option['format'],
35+
'source' => Source::fromArray($source),
36+
'export' => Export::fromArray($export),
37+
]);
38+
}
39+
}
40+
41+
public function set(int|string $value): self
42+
{
43+
$length = count($this->options);
44+
45+
if (is_int($value) && ($value >= 0 && $value < $length)) {
46+
$this->option = $this->options[$value];
47+
48+
return $this;
49+
}
50+
51+
if (is_string($value)) {
52+
$filteredOptions = array_filter($this->options, function ($option) use ($value) {
53+
return $option->name === $value;
54+
});
55+
56+
if (count($filteredOptions) > 0) {
57+
$this->option = $filteredOptions[0];
58+
59+
return $this;
60+
}
61+
}
62+
63+
throw new InvalidArgumentException("Invalid option value: {$value}");
64+
}
65+
66+
public function get(): Option
67+
{
68+
return $this->option;
69+
}
70+
71+
public function source(): Source
72+
{
73+
return $this->option->source;
74+
}
75+
76+
public function export(): Export
77+
{
78+
return $this->option->export;
79+
}
80+
}

0 commit comments

Comments
 (0)