Skip to content

Commit eccffb5

Browse files
authored
Invoke settings by custom method (#93)
* invokeSettingsBy * include php 8.1 * update phpunit * update workflow php version
1 parent 3ce84e2 commit eccffb5

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

CHANGELOG.md

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

33
All notable changes to `glorand/laravel-model-settings` will be documented in this file
44

5+
## 4.3.0 - 2021-10-11
6+
### Added
7+
- Using another method name other than settings()
8+
59
## 4.2.2 - 2021-04-07
610
### Fix param type
711

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Bug reports, feature requests, and pull requests can be submitted by following o
6262
- [Check if the model has a specific setting](#check)
6363
- [Remove a setting from a model](#remove)
6464
- [Persistence](#persistence)
65+
- [Using another method name other than settings()](#invokeSettingsBy)
6566
- [Changelog](#changelog)
6667
- [Contributing](#contributing)
6768
- [License](#license)
@@ -259,6 +260,11 @@ MODEL_SETTINGS_PERSISTENT=true
259260
```
260261
If the persistence is `false` you have to save the model after the operation.
261262

263+
### Using another method name other than `settings()` <a name="invokeSettingsBy"></a>
264+
If you prefer to use another name other than `settings` ,
265+
you can do so by defining a `$invokeSettingsBy` property.
266+
This forward calls (such as `configurations()`) to the `settings()` method.
267+
262268
## Changelog <a name="changelog"></a>
263269
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
264270

src/Traits/HasSettings.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ public function getDefaultSettings(): array
2020
return [];
2121
}
2222

23+
public function __call($name, $args)
24+
{
25+
if (isset($this->invokeSettingsBy) && $name === $this->invokeSettingsBy) {
26+
return $this->settings();
27+
}
28+
29+
return is_callable(['parent', '__call']) ? parent::__call($name, $args) : null;
30+
}
31+
2332
abstract public function getSettingsValue(): array;
2433

2534
abstract public function settings(): SettingsManagerContract;

tests/Models/UsersWithTable.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@
22

33
namespace Glorand\Model\Settings\Tests\Models;
44

5+
use Glorand\Model\Settings\Contracts\SettingsManagerContract;
56
use Glorand\Model\Settings\Traits\HasSettingsTable;
67
use Illuminate\Database\Eloquent\Model;
78

89
/**
910
* Class UsersWithTable
1011
* @package Glorand\Model\Settings\Tests\Models
1112
* @method static first()
13+
* @method SettingsManagerContract config()
1214
*/
1315
class UsersWithTable extends Model
1416
{
1517
use HasSettingsTable;
1618

19+
public $invokeSettingsBy = 'config';
20+
1721
protected $table = 'users_with_table';
1822

1923
protected $guarded = [];

tests/TableSettingsManagerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public function testSpecificDefaultValue()
4141
$this->model->settings()->all()
4242
);
4343

44+
$this->assertEquals(
45+
$this->model->config()->all(),
46+
$this->model->settings()->all()
47+
);
48+
4449
$this->model->settings()->apply($this->testArray);
4550
$this->assertEquals(
4651
array_merge($this->defaultSettingsTestArray, $this->testArray),

0 commit comments

Comments
 (0)