GithubHelp home page GithubHelp logo

solumdesignum / scenarios Goto Github PK

View Code? Open in Web Editor NEW
8.0 2.0 0.0 43 KB

Solum DeSignum Scenarios is agnostic backend validation Scenarios package.

Home Page: https://solum-designum.eu/

License: MIT License

PHP 100.00%
scenarios laravel-scenarios php php7 opensource develoment laravel packages package solumdesignum

scenarios's Introduction

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:

composer require solumdesignum/scenarios

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

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.

<?php

declare(strict_types=1);

return [
    'features' => [
        'set_method' => [
            'from' => [
                'controller' => true,
                'url_segment' => false,
            ],
            'exceptions' => [
                'controller' => true
            ],
        ],
    ],
    'methods' => [
        'pattern' => '/create|store|update|destroy/im',
    ],
];

Scenario's with Controller

Before using it must change config

<?php 

declare(strict_types=1);

return [
    'features' => [
        'set_method' => [
            'from' => [
                'controller' => true,
                'url_segment' => false,
            ],
            'exceptions' => [
                'controller' => false
            ],
        ],
    ],
    'methods' => [
        'pattern' => '/create|store|update|destroy/im',
    ],
];

Now we are prepared to use it in controller.

<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use SolumDeSignum\Scenarios\Traits\Scenarios;

class ExampleControler extends Controller
{
    use Scenarios;

    /**
     * Display a listing of the resource.
     */
    public function index(): void
    {
        dump($this);
        dd($this->scenario);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create(): void
    {
        if ($this->scenario === 'create') {
            // my logic
        }
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request): void
    {
        if ($this->scenario === 'store') {
            // my logic
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id): void
    {
        dump($this);
        dd($this->scenario);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id): void
    {
        dump($this);
        dd($this->scenario);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id): void
    {
        if ($this->scenario === 'update') {
            // my logic
        }
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id): void
    {
        if ($this->scenario === 'destroy') {
            // my logic
        }
    }
}

Scenario's with your Form Request Validation

<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use SolumDeSignum\Scenarios\Traits\Scenarios;

class OfficeBlogRequest extends FormRequest
{
    use Scenarios;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize(): bool
    {
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(): array
    {
        $rules = [];

        if ($this->scenario === 'store') {
            $rules = [
                'title' => 'required|string',
                'publish_at' => 'required',
                'blog_category_id' => 'required|numeric',
                'description' => 'required',
            ];
        }

        if ($this->scenario === 'update') {
            $rules = [
                'title' => 'required|string',
                'publish_at' => 'required',
                'blog_category_id' => 'required|numeric',
                'description' => 'required',
                'img' => 'image',
            ];
        }

        if ($this->scenario === 'destroy') {
            $rules = [];
        }

        return $rules;
    }
}

Validation Rules Usage

However, can be used on both examples

namespace App\Validation;
	
class SampleRules
{
  public static function ScenarioRules(string $scenario): ?array
  {
        switch ($scenario) {
            case $scenario === 'store';
                return
                    [
                        'text' => 'required|string',
                    ];
                break;

            case $scenario === 'update';
                return
                    [
                        'text' => 'required|string',
                        'description' => 'required|string',
                    ];
                break;
        }
  }
}

Scenario's With Controller

Manually Creating Validators

<?php

declare(strict_types=1);

namespace App\Http\Controllers\Office\Blog;

use App\Validation\SampleRules;
use Illuminate\Support\Facades\Validator;
use SolumDeSignum\Scenarios\Traits\Scenarios;

class BlogController
{
    use Scenarios;

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), SampleRules::ScenarioRules($this->scenario));
        if ($validator->passes()) {
            #Your Logic Code
        }
    }
}

Controller Functions Names Examples

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

<?php

declare(strict_types=1);

return [
    'methods' => [
        'pattern' => '/create|store|update|destroy/im'
    ]
];

#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.

scenarios's People

Contributors

faks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.