Skip to content

Commit

Permalink
Invoke settings by custom method (#93)
Browse files Browse the repository at this point in the history
* invokeSettingsBy

* include php 8.1

* update phpunit

* update workflow php version
  • Loading branch information
glorand authored Oct 11, 2021
1 parent 3ce84e2 commit eccffb5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

## 4.3.0 - 2021-10-11
### Added
- Using another method name other than settings()

## 4.2.2 - 2021-04-07
### Fix param type

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Bug reports, feature requests, and pull requests can be submitted by following o
- [Check if the model has a specific setting](#check)
- [Remove a setting from a model](#remove)
- [Persistence](#persistence)
- [Using another method name other than settings()](#invokeSettingsBy)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [License](#license)
Expand Down Expand Up @@ -259,6 +260,11 @@ MODEL_SETTINGS_PERSISTENT=true
```
If the persistence is `false` you have to save the model after the operation.

### Using another method name other than `settings()` <a name="invokeSettingsBy"></a>
If you prefer to use another name other than `settings` ,
you can do so by defining a `$invokeSettingsBy` property.
This forward calls (such as `configurations()`) to the `settings()` method.

## Changelog <a name="changelog"></a>
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Expand Down
9 changes: 9 additions & 0 deletions src/Traits/HasSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public function getDefaultSettings(): array
return [];
}

public function __call($name, $args)
{
if (isset($this->invokeSettingsBy) && $name === $this->invokeSettingsBy) {
return $this->settings();
}

return is_callable(['parent', '__call']) ? parent::__call($name, $args) : null;
}

abstract public function getSettingsValue(): array;

abstract public function settings(): SettingsManagerContract;
Expand Down
4 changes: 4 additions & 0 deletions tests/Models/UsersWithTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

namespace Glorand\Model\Settings\Tests\Models;

use Glorand\Model\Settings\Contracts\SettingsManagerContract;
use Glorand\Model\Settings\Traits\HasSettingsTable;
use Illuminate\Database\Eloquent\Model;

/**
* Class UsersWithTable
* @package Glorand\Model\Settings\Tests\Models
* @method static first()
* @method SettingsManagerContract config()
*/
class UsersWithTable extends Model
{
use HasSettingsTable;

public $invokeSettingsBy = 'config';

protected $table = 'users_with_table';

protected $guarded = [];
Expand Down
5 changes: 5 additions & 0 deletions tests/TableSettingsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public function testSpecificDefaultValue()
$this->model->settings()->all()
);

$this->assertEquals(
$this->model->config()->all(),
$this->model->settings()->all()
);

$this->model->settings()->apply($this->testArray);
$this->assertEquals(
array_merge($this->defaultSettingsTestArray, $this->testArray),
Expand Down

0 comments on commit eccffb5

Please sign in to comment.