Skip to content

Commit bac7ba8

Browse files
committed
Add easy way to override model class names on runtime
1 parent e26b51b commit bac7ba8

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ $vouchers = Vouchers::create(10);
7676
```
7777

7878
### Redeem Vouchers
79-
Redeeming vouchers requires that one provides a redeemer entity.
79+
Redeeming vouchers requires that you provide a redeemer entity.
8080
Additional metadata for the redeemer can be provided.
8181
```php
8282
Vouchers::redeem(string $code, Illuminate\Database\Eloquent\Model $entity, array $metadata = []): bool;
@@ -145,7 +145,14 @@ try {
145145
```
146146

147147
### Options
148-
Besides defaults specified in `config/vouchers.php`, one can override options when generating codes or creating vouchers.
148+
Besides defaults specified in `config/vouchers.php`, you can override options when generating codes or creating vouchers.
149+
Overriding model class names on runtime can be done using these methods.
150+
```php
151+
// Override model class names.
152+
Config::withModels(string|null $voucher = null, string|null $redeemer = null, string|null $entity = null);
153+
// Reset model class names.
154+
Config::resetModels();
155+
```
149156
Following methods apply to `Vouchers::generate()`, `Vouchers::batch()` and `Vouchers::create()` calls.
150157
```php
151158
// Override characters list.

src/Config.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ class Config
1717
*/
1818
protected array $options = [];
1919

20+
/**
21+
* Overridden model class names.
22+
*
23+
* @var array<string, string>
24+
*/
25+
protected static array $models = [];
26+
2027
/**
2128
* Get dynamically set options.
2229
*/
@@ -320,18 +327,34 @@ public function withOwner(?Model $owner): self
320327
}
321328

322329
/**
323-
* Get model class name from config.
330+
* Set model class name overrides.
331+
*/
332+
public static function withModels(?string $voucher = null, ?string $redeemer = null, ?string $entity = null): void
333+
{
334+
static::$models = compact('entity', 'redeemer', 'voucher');
335+
}
336+
337+
/**
338+
* Reset model class name overrides.
339+
*/
340+
public static function resetModels(): void
341+
{
342+
static::$models = [];
343+
}
344+
345+
/**
346+
* Get model class name from override or config.
324347
*/
325348
public static function model(string $name): ?string
326349
{
327-
return config('vouchers.models.' . $name);
350+
return static::$models[$name] ?? config("vouchers.models.{$name}");
328351
}
329352

330353
/**
331354
* Get database table name for a model from config.
332355
*/
333356
public static function table(string $name): ?string
334357
{
335-
return config('vouchers.tables.' . $name);
358+
return config("vouchers.tables.{$name}");
336359
}
337360
}

tests/Feature/Vouchers/ConfigTest.php

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@
2626
'redeemer' => 'FakeRedeemer',
2727
'voucher' => 'FakeVoucher',
2828
];
29-
app()['config']->set('vouchers.models', $models);
29+
30+
// Temporarily overrides.
31+
Config::withModels(...$models);
32+
foreach ($models as $name => $model) {
33+
expect(Config::model($name))->toBe($model);
34+
}
35+
Config::resetModels();
36+
37+
// Config overrides.
38+
config(['vouchers.models' => $models]);
3039
foreach ($models as $name => $model) {
3140
expect(Config::model($name))->toBe($model);
3241
}
@@ -51,7 +60,7 @@
5160
'redeemers' => 'this_redeemers',
5261
'vouchers' => 'this_vouchers',
5362
];
54-
app()['config']->set('vouchers.tables', $tables);
63+
config(['vouchers.tables' => $tables]);
5564
foreach ($tables as $name => $table) {
5665
expect(Config::table($name))->toBe($table);
5766
}
@@ -66,22 +75,20 @@
6675
*/
6776
test('default options', function () {
6877
$config = new Config();
69-
$app_config = app()['config'];
7078

7179
expect($config->getOptions())->toBeEmpty();
72-
expect($config->getCharacters())->toBe($app_config->get('vouchers.characters'));
73-
expect($config->getMask())->toBe($app_config->get('vouchers.mask'));
74-
expect($config->getPrefix())->toBe($app_config->get('vouchers.prefix'));
75-
expect($config->getSuffix())->toBe($app_config->get('vouchers.suffix'));
76-
expect($config->getSeparator())->toBe($app_config->get('vouchers.separator'));
80+
expect($config->getCharacters())->toBe(config('vouchers.characters'));
81+
expect($config->getMask())->toBe(config('vouchers.mask'));
82+
expect($config->getPrefix())->toBe(config('vouchers.prefix'));
83+
expect($config->getSuffix())->toBe(config('vouchers.suffix'));
84+
expect($config->getSeparator())->toBe(config('vouchers.separator'));
7785
});
7886

7987
/**
8088
* Test options overridden in config.
8189
*/
8290
test('config overridden options', function () {
8391
$config = new Config();
84-
$app_config = app()['config'];
8592

8693
// Override config.
8794
$options = [
@@ -94,7 +101,7 @@
94101
foreach ($options as $key => $value) {
95102
$getter = 'get' . ucfirst($key);
96103
expect($value)->not->toBe($config->{$getter}());
97-
$app_config->set('vouchers.' . $key, $value);
104+
config(["vouchers.{$key}" => $value]);
98105
}
99106

100107
expect($config->getOptions())->toBeEmpty();
@@ -147,11 +154,11 @@
147154
->withSuffix(null)
148155
->withSeparator(null)
149156
;
150-
expect($config->getCharacters())->toBe(app()['config']->get('vouchers.characters'));
151-
expect($config->getMask())->toBe(app()['config']->get('vouchers.mask'));
152-
expect($config->getPrefix())->toBe(app()['config']->get('vouchers.prefix'));
153-
expect($config->getSuffix())->toBe(app()['config']->get('vouchers.suffix'));
154-
expect($config->getSeparator())->toBe(app()['config']->get('vouchers.separator'));
157+
expect($config->getCharacters())->toBe(config('vouchers.characters'));
158+
expect($config->getMask())->toBe(config('vouchers.mask'));
159+
expect($config->getPrefix())->toBe(config('vouchers.prefix'));
160+
expect($config->getSuffix())->toBe(config('vouchers.suffix'));
161+
expect($config->getSeparator())->toBe(config('vouchers.separator'));
155162
});
156163

157164
/**

0 commit comments

Comments
 (0)