PHP code example of marcoslopez95 / wrap-and-action-package

1. Go to this page and download the library: Download marcoslopez95/wrap-and-action-package library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

marcoslopez95 / wrap-and-action-package example snippets




namespace App\Actions;

class UserRegistrationAction 
{
    public function invoke(): mixed
    {
        // Lógica de registro de usuario
        return $result;
    }
}



namespace App\Wrapper;

use Manu\WrapAndActionPackage\Wrapper;

class UserDataWrapper extends Wrapper
{
    public function getName(): string
    {
        return $this->get('name', '');
    }
    
    public function getAge(): int
    {
        return $this->getInt('age', 0);
    }
}


// namespace
// uses

class UserController extends Controller{

    public function store(Request $request)
    {
        $request->validate([
           'name' => 'de' => 'er = User::create($request->only(['name','email']));
        
        collect($request->addresses)->each(function(array $address) use ($user){
            $data = $address;
            // parseo y manipulación de la data
            $user->addresses()->create($data);
        });
    }
}



namespace App\Wrapper;

use Illuminate\Support\Collection;
use Manu\WrapAndActionPackage\Wrapper;

class UserDataWrapper extends Wrapper
{
    public function getData(): array
    {
        return [
        'name' => $this->get('name'),
        'email' => $this->get('email'),
        ];
    }
    
    public function getAddresses(): Collection
    {
        return $this->castMany('addresses', AddressesWrapper::class);
    }
}

// AddressesWrapper.php

class AddressesWrapper extends Wrapper
{
    public function getData(): array
    {
        // parseo y manipulación de data.
        return [
            'name' => $this->get('name'),
            'zip_code' => $this->get('zip_code'),
            'city_id' => $this->get('city_id'),
            'name' => $this->get('name'),
        ];
    }
}


// namespace
// uses
use App\Wrapper\UserDataWrapper;

class UserController extends Controller{

    public function store(UserRequest $request, UserDataWrapper $wrapper, UserStoreAction $action)
    {
        $user = $action->invoke(new UserDataWrapper($request->all()));
        return response()->json($user);
    }
}

// App/Actions/UserStoreAction.php

 
namespace App\Actions;  
  
class UserStoreAction{

    public function invoke(UserDataWrapper $data): User
    {
        $user = User::create($data->getData());
        
        $data->getAddresses()->each(fn($address) => $user->addresses()->create($address));
        
        return $user;
    }
}
bash
php artisan make:action UserRegistrationAction
bash
php artisan make:wrapper UserDataWrapper