Skip to content

Commit df42521

Browse files
authored
Merge pull request #9 from SolumDeSignum/dev
Dev
2 parents 11d6b12 + afcf9d9 commit df42521

File tree

12 files changed

+702
-147
lines changed

12 files changed

+702
-147
lines changed

README.md

Lines changed: 130 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![MIT Licensed](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
77

88
### Introduction
9-
Solum DeSignum Scenarios is agnostic backend validation Scenarios package.
9+
Scenarios are agnostic backend validation Scenarios package.
1010

1111

1212
### Installation
@@ -21,9 +21,10 @@ Next, publish Scenarios resources using the vendor:publish command:
2121
php artisan vendor:publish --provider="SolumDeSignum\Scenarios\ScenariosServiceProvider"
2222
```
2323

24-
This command will publish Scenarios config to your config directory, which will be
25-
created if it does not exist.
24+
This command will publish scenarios.php config to your config directory, which will be created if it does not exist.
2625

26+
### Upgrade from v1.xx to version v2.00
27+
[UPGRADE_V2.md](UPGRADE_V2.md) !!!
2728

2829
### Scenarios Features
2930
The Scenarios configuration file contains a configuration array.
@@ -34,16 +35,132 @@ declare(strict_types=1);
3435

3536
return [
3637
'features' => [
37-
'setMethodFromUrlSegment' => false,
38-
'setMethodFromController' => true,
38+
'set_method' => [
39+
'from' => [
40+
'controller' => true,
41+
'url_segment' => false,
42+
],
43+
'exceptions' => [
44+
'controller' => true
45+
],
46+
],
3947
],
4048
'methods' => [
41-
'pattern' => '/create|store|update|destroy/im'
42-
]
49+
'pattern' => '/create|store|update|destroy/im',
50+
],
4351
];
4452
````
4553

46-
### Scenario's With Form Request Validation
54+
### Scenario's with Controller
55+
Before using it must change config
56+
```php
57+
<?php
58+
59+
declare(strict_types=1);
60+
61+
return [
62+
'features' => [
63+
'set_method' => [
64+
'from' => [
65+
'controller' => true,
66+
'url_segment' => false,
67+
],
68+
'exceptions' => [
69+
'controller' => false
70+
],
71+
],
72+
],
73+
'methods' => [
74+
'pattern' => '/create|store|update|destroy/im',
75+
],
76+
];
77+
````
78+
Now we are prepared to use it in controller.
79+
```php
80+
<?php
81+
82+
declare(strict_types=1);
83+
84+
namespace App\Http\Controllers;
85+
86+
use Illuminate\Http\Request;
87+
use SolumDeSignum\Scenarios\Traits\Scenarios;
88+
89+
class ExampleControler extends Controller
90+
{
91+
use Scenarios;
92+
93+
/**
94+
* Display a listing of the resource.
95+
*/
96+
public function index(): void
97+
{
98+
dump($this);
99+
dd($this->scenario);
100+
}
101+
102+
/**
103+
* Show the form for creating a new resource.
104+
*/
105+
public function create(): void
106+
{
107+
if ($this->scenario === 'create') {
108+
// my logic
109+
}
110+
}
111+
112+
/**
113+
* Store a newly created resource in storage.
114+
*/
115+
public function store(Request $request): void
116+
{
117+
if ($this->scenario === 'store') {
118+
// my logic
119+
}
120+
}
121+
122+
/**
123+
* Display the specified resource.
124+
*/
125+
public function show(string $id): void
126+
{
127+
dump($this);
128+
dd($this->scenario);
129+
}
130+
131+
/**
132+
* Show the form for editing the specified resource.
133+
*/
134+
public function edit(string $id): void
135+
{
136+
dump($this);
137+
dd($this->scenario);
138+
}
139+
140+
/**
141+
* Update the specified resource in storage.
142+
*/
143+
public function update(Request $request, string $id): void
144+
{
145+
if ($this->scenario === 'update') {
146+
// my logic
147+
}
148+
}
149+
150+
/**
151+
* Remove the specified resource from storage.
152+
*/
153+
public function destroy(string $id): void
154+
{
155+
if ($this->scenario === 'destroy') {
156+
// my logic
157+
}
158+
}
159+
}
160+
````
161+
162+
### Scenario's with your Form Request Validation
163+
47164
```php
48165
<?php
49166

@@ -53,8 +170,7 @@ namespace App\Http\Requests;
53170

54171
use Illuminate\Foundation\Http\FormRequest;
55172
use Illuminate\Support\Facades\Auth;
56-
use SolumDeSignum\Scenarios\Scenarios;
57-
173+
use SolumDeSignum\Scenarios\Traits\Scenarios;
58174

59175
class OfficeBlogRequest extends FormRequest
60176
{
@@ -110,11 +226,10 @@ class OfficeBlogRequest extends FormRequest
110226

111227
### Validation Rules Usage
112228
#### However, can be used on both examples
229+
113230
```php
114231
namespace App\Validation;
115232

116-
use SolumDeSignum\Scenarios\Scenarios;
117-
118233
class SampleRules
119234
{
120235
public static function ScenarioRules(string $scenario): ?array
@@ -141,16 +256,17 @@ class SampleRules
141256

142257
### Scenario's With Controller
143258
#### Manually Creating Validators
259+
144260
```php
145261
<?php
146262

147263
declare(strict_types=1);
148264

149265
namespace App\Http\Controllers\Office\Blog;
150266

151-
use Illuminate\Support\Facades\Validator;
152-
use SolumDeSignum\Scenarios\Scenarios;
153267
use App\Validation\SampleRules;
268+
use Illuminate\Support\Facades\Validator;
269+
use SolumDeSignum\Scenarios\Traits\Scenarios;
154270

155271
class BlogController
156272
{

UPGRADE_V2.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
### Upgrade Guide
2+
3+
Scenarios Migration to Traits
4+
Overview
5+
6+
In this version, the Scenarios functionality has been refactored and moved from the src directory to a new trait inside
7+
the SolumDeSignum\Scenarios\Traits namespace.
8+
This change allows for better modularity, easier reuse, and more flexible integration within different parts of your application.
9+
10+
### Key Changes
11+
12+
File Location:
13+
The Scenarios functionality, which was previously located in src/Scenarios.php, has been moved to
14+
src/Scenarios/Traits/Scenarios.php.
15+
16+
### Namespace Change:
17+
The namespace has changed from:
18+
19+
```php
20+
<?php
21+
22+
declare(strict_types=1);
23+
24+
use SolumDeSignum\Scenarios\Scenarios;;
25+
````
26+
27+
to
28+
29+
```php
30+
<?php
31+
32+
declare(strict_types=1);
33+
34+
use SolumDeSignum\Scenarios\Traits\Scenarios;
35+
````
36+
37+
### Config Change:
38+
The config has changed from:
39+
```php
40+
<?php
41+
42+
declare(strict_types=1);
43+
44+
return [
45+
'features' => [
46+
'setMethodFromUrlSegment' => false,
47+
'setMethodFromController' => true,
48+
],
49+
'methods' => [
50+
'pattern' => '/create|store|update|destroy/im',
51+
],
52+
];
53+
````
54+
to
55+
```php
56+
<?php
57+
58+
declare(strict_types=1);
59+
60+
return [
61+
'features' => [
62+
'set_method' => [
63+
'from' => [
64+
'controller' => true,
65+
'url_segment' => false,
66+
],
67+
'exceptions' => [
68+
'controller' => false
69+
],
70+
],
71+
],
72+
'methods' => [
73+
'pattern' => '/create|store|update|destroy/im',
74+
],
75+
];
76+
````

composer.json

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@
2020
],
2121
"homepage": "https://github.com/solumdesignum/scenarios",
2222
"require": {
23-
"php": "^8.1|^8.2|^8.3",
24-
"illuminate/support": "~10|~11"
23+
"php": "^8.1|^8.2|^8.3|^8.4",
24+
"illuminate/support": "^5|^6|^7|^8|^9|^10|^11|^12"
2525
},
2626
"require-dev": {
27+
"phpunit/phpunit": "^8|^9|^10|^11",
28+
"mockery/mockery": "^1.4",
29+
"orchestra/testbench": "^5|^6|^7|^8|^9",
30+
"sempro/phpunit-pretty-print": "^1.4"
2731
},
2832
"autoload": {
2933
"psr-4": {
@@ -32,23 +36,25 @@
3236
},
3337
"autoload-dev": {
3438
"psr-4": {
35-
"SolumDeSignum\\Scenarios\\Tests\\": "tests"
39+
"SolumDeSignum\\Scenarios\\Tests\\": "tests/"
3640
}
3741
},
3842
"extra": {
3943
"laravel": {
4044
"providers": [
4145
"SolumDeSignum\\Scenarios\\ScenariosServiceProvider"
42-
],
43-
"branch-alias": {
44-
"dev-master": "1.x-master",
45-
"dev-dev": "1.x-dev"
46-
}
46+
]
4747
}
4848
},
4949
"config": {
50-
"sort-packages": true
50+
"preferred-install": "dist",
51+
"sort-packages": true,
52+
"optimize-autoloader": true
5153
},
5254
"minimum-stability": "dev",
53-
"prefer-stable": true
55+
"prefer-stable": true,
56+
"scripts": {
57+
"test": "vendor/bin/phpunit",
58+
"test-coverage": "phpunit --coverage-html coverage"
59+
}
5460
}

config/scenarios.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44

55
return [
66
'features' => [
7-
'setMethodFromUrlSegment' => false,
8-
'setMethodFromController' => true,
7+
'set_method' => [
8+
'from' => [
9+
'controller' => true,
10+
'url_segment' => false,
11+
],
12+
'exceptions' => [
13+
'controller' => true
14+
],
15+
],
916
],
1017
'methods' => [
1118
'pattern' => '/create|store|update|destroy/im',

0 commit comments

Comments
 (0)