项目作者: SolumDeSignum

项目描述 :
Solum DeSignum Scenarios is agnostic backend validation Scenarios package.
高级语言: PHP
项目地址: git://github.com/SolumDeSignum/scenarios.git
创建时间: 2018-08-24T00:27:04Z
项目社区:https://github.com/SolumDeSignum/scenarios

开源协议:MIT License

下载


StyleCI
Scrutinizer Code Quality
Total Downloads
Latest Stable Version
Latest Unstable Version
MIT Licensed

Introduction

Scenarios are agnostic backend validation Scenarios package.

Installation

To get started, install Scenarios using the Composer package manager:

  1. composer require solumdesignum/scenarios

Next, publish Scenarios resources using the vendor:publish command:

  1. php artisan vendor:publish --provider="SolumDeSignum\Scenarios\ScenariosServiceProvider"

This command will publish scenarios.php config to your config directory, which will be created if it does not exist.

Upgrade from v1.xx to version v2.00

UPGRADE_V2.md !!!

Scenarios Features

The Scenarios configuration file contains a configuration array.

  1. <?php
  2. declare(strict_types=1);
  3. return [
  4. 'features' => [
  5. 'set_method' => [
  6. 'from' => [
  7. 'controller' => true,
  8. 'url_segment' => false,
  9. ],
  10. 'exceptions' => [
  11. 'controller' => true
  12. ],
  13. ],
  14. ],
  15. 'methods' => [
  16. 'pattern' => '/create|store|update|destroy/im',
  17. ],
  18. ];
  19. `

Scenario’s with Controller

Before using it must change config

  1. <?php
  2. declare(strict_types=1);
  3. return [
  4. 'features' => [
  5. 'set_method' => [
  6. 'from' => [
  7. 'controller' => true,
  8. 'url_segment' => false,
  9. ],
  10. 'exceptions' => [
  11. 'controller' => false
  12. ],
  13. ],
  14. ],
  15. 'methods' => [
  16. 'pattern' => '/create|store|update|destroy/im',
  17. ],
  18. ];
  19. `

Now we are prepared to use it in controller.

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Http\Controllers;
  4. use Illuminate\Http\Request;
  5. use SolumDeSignum\Scenarios\Traits\Scenarios;
  6. class ExampleControler extends Controller
  7. {
  8. use Scenarios;
  9. /**
  10. * Display a listing of the resource.
  11. */
  12. public function index(): void
  13. {
  14. dump($this);
  15. dd($this->scenario);
  16. }
  17. /**
  18. * Show the form for creating a new resource.
  19. */
  20. public function create(): void
  21. {
  22. if ($this->scenario === 'create') {
  23. // my logic
  24. }
  25. }
  26. /**
  27. * Store a newly created resource in storage.
  28. */
  29. public function store(Request $request): void
  30. {
  31. if ($this->scenario === 'store') {
  32. // my logic
  33. }
  34. }
  35. /**
  36. * Display the specified resource.
  37. */
  38. public function show(string $id): void
  39. {
  40. dump($this);
  41. dd($this->scenario);
  42. }
  43. /**
  44. * Show the form for editing the specified resource.
  45. */
  46. public function edit(string $id): void
  47. {
  48. dump($this);
  49. dd($this->scenario);
  50. }
  51. /**
  52. * Update the specified resource in storage.
  53. */
  54. public function update(Request $request, string $id): void
  55. {
  56. if ($this->scenario === 'update') {
  57. // my logic
  58. }
  59. }
  60. /**
  61. * Remove the specified resource from storage.
  62. */
  63. public function destroy(string $id): void
  64. {
  65. if ($this->scenario === 'destroy') {
  66. // my logic
  67. }
  68. }
  69. }
  70. `

Scenario’s with your Form Request Validation

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Http\Requests;
  4. use Illuminate\Foundation\Http\FormRequest;
  5. use Illuminate\Support\Facades\Auth;
  6. use SolumDeSignum\Scenarios\Traits\Scenarios;
  7. class OfficeBlogRequest extends FormRequest
  8. {
  9. use Scenarios;
  10. /**
  11. * Determine if the user is authorized to make this request.
  12. *
  13. * @return bool
  14. */
  15. public function authorize(): bool
  16. {
  17. return Auth::check();
  18. }
  19. /**
  20. * Get the validation rules that apply to the request.
  21. *
  22. * @return array
  23. */
  24. public function rules(): array
  25. {
  26. $rules = [];
  27. if ($this->scenario === 'store') {
  28. $rules = [
  29. 'title' => 'required|string',
  30. 'publish_at' => 'required',
  31. 'blog_category_id' => 'required|numeric',
  32. 'description' => 'required',
  33. ];
  34. }
  35. if ($this->scenario === 'update') {
  36. $rules = [
  37. 'title' => 'required|string',
  38. 'publish_at' => 'required',
  39. 'blog_category_id' => 'required|numeric',
  40. 'description' => 'required',
  41. 'img' => 'image',
  42. ];
  43. }
  44. if ($this->scenario === 'destroy') {
  45. $rules = [];
  46. }
  47. return $rules;
  48. }
  49. }
  50. `

Validation Rules Usage

However, can be used on both examples

  1. namespace App\Validation;
  2. class SampleRules
  3. {
  4. public static function ScenarioRules(string $scenario): ?array
  5. {
  6. switch ($scenario) {
  7. case $scenario === 'store';
  8. return
  9. [
  10. 'text' => 'required|string',
  11. ];
  12. break;
  13. case $scenario === 'update';
  14. return
  15. [
  16. 'text' => 'required|string',
  17. 'description' => 'required|string',
  18. ];
  19. break;
  20. }
  21. }
  22. }

Scenario’s With Controller

Manually Creating Validators

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Http\Controllers\Office\Blog;
  4. use App\Validation\SampleRules;
  5. use Illuminate\Support\Facades\Validator;
  6. use SolumDeSignum\Scenarios\Traits\Scenarios;
  7. class BlogController
  8. {
  9. use Scenarios;
  10. public function store(Request $request)
  11. {
  12. $validator = Validator::make($request->all(), SampleRules::ScenarioRules($this->scenario));
  13. if ($validator->passes()) {
  14. #Your Logic Code
  15. }
  16. }
  17. }

Controller Functions Names Examples

However, you can override regex with your naming conventions inside configuration

  1. <?php
  2. declare(strict_types=1);
  3. return [
  4. 'methods' => [
  5. 'pattern' => '/create|store|update|destroy/im'
  6. ]
  7. ];
  8. #Controller Function Naming Samples: create(), store() , update() , destroy()

Author

Support

If you need support you can ask on Twitter.

License

Solum DeSignum Scenarios is open-sourced software licensed under the MIT license.