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/ */
$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'
);