PHP code example of antonioprimera / bapi

1. Go to this page and download the library: Download antonioprimera/bapi 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/ */

    

antonioprimera / bapi example snippets


AddCarPartToInventoryBapi::run(
    partCategory: $category,
    partType: $type,
    partManufacturer: $manufacturer,
    carMake: $make,
    carModel: $model,
    carYear: $year,
    storage: $storageToBeAddedTo,
);

    UpdatePostBapi::run(post: $post, title: $title, contents: $contents)

    protected function handle(Post $post, $title, $contents)

    return
        $this->post->title === $this->title
        && $this->post->contents === $this->contents;

    //static method call
    UpdatePostBapi::run($post, 'New title', 'Some contents');

    //instance method call
    $updatePostBapi = new UpdatePostBapi();
    $updatePostBapi->run($post, 'New title', 'Some contents');

    //invoke
    $updatePostBapi = new UpdatePostBapi();
    $updatePostBapi($post, 'New title', 'Some contents');

    //static method call
    UpdatePostBapi::withoutAuthorizationCheck()
        ->run($post, 'New title', 'Some contents');

    //instance method call
    $updatePostBapi = new UpdatePostBapi();
    $updatePostBapi->withoutAuthorizationCheck();
    $updatePostBapi->run($post, 'New title', 'Some contents');

    //static method call
    UpdatePostBapi::withoutDbTransaction()
        ->run($post, 'New title', 'Some contents');

    //instance method call
    $updatePostBapi = new UpdatePostBapi();
    $updatePostBapi->withoutDbTransaction();
    $updatePostBapi->run($post, 'New title', 'Some contents');

    $bapiValidationIssue = new \AntonioPrimera\Bapi\Components\BapiValidationIssue(
        attributeName: 'companyName',      //the name of the attribute at fault
        attributeValue: 'Amazon UK',       //the value of the attribute
        errorMessage: 'not-unique',        //the issue that occurred
        errorCode: 'C:N:NU'                //optionally, an issue code
    );

    protected function validate()
    {
        $issues = [];
        
        //business validation - whether the company name is unique in the EU
        if ($this->comapnyNameIsNotUnique($this->company->name))
            $issues[] = new \AntonioPrimera\Bapi\Components\BapiValidationIssue(
                'companyName',
                $this->company->name,
                'not-unique',
                'C:N:NU'
            );
            
        //business validation - whether the country is registered in the EU
        if ($this->companyCountryNotValid($this->company->country))
            $issues[] = new \AntonioPrimera\Bapi\Components\BapiValidationIssue(
                'companyCountry',
                $this->company->country,
                'non-EU',
                'C:C:NEU'
            );
        
        //if any issues were found, throw a new BapiValidationException with these issues
        if ($issues)
            throw new \AntonioPrimera\Bapi\Exceptions\BapiValidationException($issues);
    }

    use \AntonioPrimera\Bapi\Traits\ValidatesAttributes;
    
    protected function validate()
    {
        //business validation - whether the company name is unique
        if ($this->comapanyNameIsNotUnique($this->company))
            return new \AntonioPrimera\Bapi\Components\BapiValidationIssue(
                'companyName',
                $this->company->name,
                'Company name is not unique',
            );
            
        return true;
    }

protected function authorize()
{
    return $this->actor() 
        && $this->can('some-action', $someModel);
}

    // Inside the CreatePostBapi
    protected function handle(Post $post, $title, $contents)
    {
        $post = Post::create([
            'title' => $title,
            'contents' => $contents
        ]);

        //the "call" method is used, instead of the "run" method
        CreatePostNotificationBapi::call(post: $post);
        
        return $post;
    }

    // Inside a removeTopicBapi
    protected function handle(Topic $topic): void
    {
        //remove all posts from the topic, checking remove authorization for each post
        foreach ($topic->posts as $post)
            DeletePostBapi::callWithAuthorizationCheck(post: $post);
            
        $topic->delete();
    }

use AntonioPrimera\Bapi\TestInternalBapi;

//in your test file
$bapiResult = TestInternalBapi::run(
    bapi: CreatePostBapi::class,
    title: 'New post',
    contents: 'Some contents'
);
bash
php artisan make:bapi Posts/CreatePostBapi
bash
php artisan make:bapi Posts/CreatePostBapi --test Posts/CreatePostBapiBasicTest
bash
php artisan make:bapi Posts/CreatePostBapi --internal
# or
php artisan make:bapi Posts/CreatePostBapi -I