Skip to content

Commit 20dfb02

Browse files
authored
Develop (#94)
* Validation system for settings data
1 parent eccffb5 commit 20dfb02

18 files changed

+163
-37
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,19 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
php: [8.0, 7.4, 7.3, 7.2]
17-
laravel: [8.*, 7.*, 6.*]
16+
php: [8.0, 7.4, 7.3]
17+
laravel: [8.*, 7.*]
1818
dependency-version: [prefer-lowest, prefer-stable]
1919
include:
2020
- laravel: 8.*
2121
testbench: 6.*
2222
- laravel: 7.*
2323
testbench: 5.*
24-
- laravel: 6.*
25-
testbench: 4.*
2624
exclude:
2725
- laravel: 8.*
2826
php: 7.2
2927
- laravel: 7.*
3028
php: 8.0
31-
- laravel: 6.*
32-
php: 8.0
33-
- laravel: 6.*
34-
php: 7.4
3529

3630
name: P ${{ matrix.php }} - L ${{ matrix.laravel }} - ${{ matrix.dependency-version }}
3731

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 7.2
54
- 7.3
65
- 7.4
76

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.4.0 - 2021-11-01
6+
### Added
7+
- Validation system for settings data
8+
59
## 4.3.0 - 2021-10-11
610
### Added
711
- Using another method name other than settings()

README.md

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<a title="PHP Version" href="#"><img src="https://img.shields.io/packagist/php-v/glorand/laravel-model-settings" alt="PHP Version" /></a>
4242
</p>
4343

44-
The package requires PHP 7.2+ and follows the FIG standards PSR-1, PSR-2 and PSR-4
44+
The package requires PHP 7.3+ and follows the FIG standards PSR-1, PSR-2, PSR-4 and PSR-12
4545
to ensure a high level of interoperability between shared PHP.
4646

4747
Bug reports, feature requests, and pull requests can be submitted by following our [Contribution Guide](CONTRIBUTING.md).
@@ -62,7 +62,8 @@ 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)
65+
- [Using another method name other than `settings()`](#invokeSettingsBy)
66+
- [Validation system for settings data](#validation)
6667
- [Changelog](#changelog)
6768
- [Contributing](#contributing)
6869
- [License](#license)
@@ -265,6 +266,40 @@ If you prefer to use another name other than `settings` ,
265266
you can do so by defining a `$invokeSettingsBy` property.
266267
This forward calls (such as `configurations()`) to the `settings()` method.
267268

269+
### Validation system for settings data <a name="validation></a>
270+
When you're using the set() or apply()|update() methods thrown an exception when you break a rule.
271+
You can define rules on model using `$settingsRules` public property, and the rules array definition is identical with
272+
the Laravel default validation rules. ([see Laravel rules](https://laravel.com/docs/8.x/validation#available-validation-rules))
273+
```php
274+
class User extends Model
275+
{
276+
use HasSettingsTable;
277+
278+
public array $defaultSettings = [
279+
'user' => [
280+
'name' => 'Test User',
281+
'email' => 'user@test.com'
282+
'age' => 27,
283+
],
284+
'language' => 'en',
285+
'max_size' => 12,
286+
];
287+
288+
// settings rules
289+
public array $settingsRules = [
290+
'user' => 'array',
291+
'user.email' => [
292+
'string',
293+
'email',
294+
],
295+
'user.age' => 'integer',
296+
'language' => 'string|in:en,es,it|max:2',
297+
'max_size' => 'int|min:5|max:15',
298+
];
299+
}
300+
301+
```
302+
268303
## Changelog <a name="changelog"></a>
269304
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
270305

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@
1717
"settings"
1818
],
1919
"require": {
20-
"php": ">=7.2",
21-
"illuminate/config": "^6.0|^7.0|^8.0",
22-
"illuminate/database": "^6.0|^7.0|^8.0",
23-
"illuminate/support": "^6.0|^7.0|^8.0",
24-
"illuminate/console": "^6.0|^7.0|^8.0",
25-
"illuminate/filesystem": "^6.0|^7.0|^8.0",
26-
"illuminate/cache": "^6.0|^7.0|^8.0",
20+
"php": ">=7.3",
21+
"illuminate/config": "^7.0|^8.0",
22+
"illuminate/database": "^7.0|^8.0",
23+
"illuminate/support": "^7.0|^8.0",
24+
"illuminate/console": "^7.0|^8.0",
25+
"illuminate/filesystem": "^7.0|^8.0",
26+
"illuminate/cache": "^7.0|^8.0",
2727
"ext-json": "*"
2828
},
2929
"require-dev": {
3030
"phpunit/phpunit": "^8.0|^9.0",
31-
"orchestra/testbench": "^4.0|^5.0|^6.0",
31+
"orchestra/testbench": "^5.0|^6.0",
3232
"friendsofphp/php-cs-fixer": "^2.16@dev",
3333
"josiasmontag/laravel-redis-mock": "^1.2"
3434
},

src/Console/CreateSettingsFieldForModel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class CreateSettingsFieldForModel extends Command
1616
protected $description = 'Create migration for the (update) selected table, adding settings field';
1717

1818
/**
19-
* @param \Illuminate\Filesystem\Filesystem $file
20-
* @return bool
19+
* @param \Illuminate\Filesystem\Filesystem $file
20+
* @return int
2121
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
2222
*/
23-
public function handle(Filesystem $file)
23+
public function handle(Filesystem $file): int
2424
{
2525
$table = $this->ask('What is the name of the table?');
2626
$table = strtolower(Str::snake(trim($table)));

src/Managers/AbstractSettingsManager.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
use Glorand\Model\Settings\Contracts\SettingsManagerContract;
66
use Glorand\Model\Settings\Exceptions\ModelSettingsException;
77
use Glorand\Model\Settings\Traits\HasSettings;
8+
use Illuminate\Support\Facades\Validator;
89
use Illuminate\Database\Eloquent\Model;
910
use Illuminate\Support\Arr;
1011

1112
/**
1213
* Class AbstractSettingsManager
1314
* @package Glorand\Model\Settings\Managers
15+
* @SuppressWarnings(PHPMD.StaticAccess)
1416
*/
1517
abstract class AbstractSettingsManager implements SettingsManagerContract
1618
{
@@ -49,11 +51,12 @@ private static function isAssoc(array $arr): bool
4951

5052
/**
5153
* Flatten array with dots for settings package
52-
* @param $array
54+
* @param array $array
5355
* @param string $prepend
5456
* @return array
57+
* @SuppressWarnings(PHPMD.ElseExpression)
5558
*/
56-
public static function dotFlatten($array, $prepend = ''): array
59+
public static function dotFlatten(array $array, string $prepend = ''): array
5760
{
5861
$results = [];
5962
foreach ($array as $key => $value) {
@@ -226,4 +229,13 @@ public function deleteMultiple(iterable $paths): SettingsManagerContract
226229

227230
return $this;
228231
}
232+
233+
/**
234+
* @param array $settings
235+
* @throws \Illuminate\Validation\ValidationException
236+
*/
237+
protected function validate(array $settings)
238+
{
239+
Validator::make(Arr::wrap($settings), Arr::wrap($this->model->getRules()))->validate();
240+
}
229241
}

src/Managers/FieldSettingsManager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
class FieldSettingsManager extends AbstractSettingsManager
1313
{
1414
/**
15-
* @param array $settings
15+
* @param array $settings
1616
* @return \Glorand\Model\Settings\Contracts\SettingsManagerContract
1717
*/
1818
public function apply(array $settings = []): SettingsManagerContract
1919
{
20+
$this->validate($settings);
21+
2022
$this->model->{$this->model->getSettingsFieldName()} = json_encode($settings);
2123
if ($this->model->isPersistSettings()) {
2224
$this->model->save();

src/Managers/RedisSettingsManager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class RedisSettingsManager extends AbstractSettingsManager
1414
{
1515
public function apply(array $settings = []): SettingsManagerContract
1616
{
17+
$this->validate($settings);
18+
1719
Redis::set($this->model->cacheKey(), json_encode($settings));
1820

1921
return $this;

src/Managers/TableSettingsManager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ class TableSettingsManager extends AbstractSettingsManager
1919
*/
2020
public function apply(array $settings = []): SettingsManagerContract
2121
{
22+
$this->validate($settings);
23+
2224
$modelSettings = $this->model->modelSettings()->first();
2325
if (!count($settings)) {
2426
if ($modelSettings) {

0 commit comments

Comments
 (0)