Giter VIP home page Giter VIP logo

simple-action's Introduction

Simple Action

Lightweight action classes for Laravel

Usage

CreateUserData.php

namespace App\Actions\ActionData\User;

use PharkasBence\SimpleAction\ActionData;

class CreateUserData extends ActionData
{
    use HydratesProperties;

    protected string $email;
    protected string $username;
    protected string $password;
    protected ?string $phoneNumber;

    public function __construct(array $data)
    {
        parent::__construct($data);

        $this->hydrateProperties();
    }

    // optional
    protected function validationRules(): array
    {
        return [
            'email' => 'required|email',
            'password' => 'required|string|min:6',
            'username' => 'required|string',
            'phone_number' => 'string',
        ];
    }
    
    // optional
    protected function validationMessages(): array
    {
        'password' => 'Passwords must be at least 6 characters in length',
    }
}

CreateUser.php

namespace App\Actions\User;

use Illuminate\Support\Facades\Hash;
use App\Actions\Action;
use App\Actions\ActionData\User\CreateUserData;
use App\Models\User;

class CreateUser extends Action
{
    public function __construct(/*  */) {}

    public function handle(CreateUserData $data): User
    {
        $passwordHash = Hash::make($data->password);
        
        return User::create([
            'email' => $data->email,
            'password' => $passwordHash,
            'username' => $data->username,
            'phone_number' => $data->phoneNumber,
        ]);

        // data can also be extracted by calling $data->toArray()
    }
}

UserController.php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Actions\User\CreateUser;
use App\Actions\ActionData\User\CreateUserData;

class UserController extends Controller
{
    public function store(Request $request)
    {
        // request data fields need to be: email, username, password, phone_number
        CreateUser::run(new CreateUserData($request->all()));
        
        // ...
    }
}

License

MIT

simple-action's People

Contributors

pharkasbence avatar

Stargazers

Zsolt Horváth avatar

Watchers

 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.