1. Go to this page and download the library: Download ict/api_one_endpoint 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/ */
ict / api_one_endpoint example snippets
class Hero {
public function __construct(
public readonly string $name,
public readonly string $hero
){ }
}
$data = [
new Hero('Peter Parker', 'spiderman'),
new Hero('Clark Kent', 'superman')
];
return new ApiOutput($data, 200);
class PaymentDoneOutput {
public function __construct(
public readonly string paymentId,
public readonly \DateTimeInmutable $date
){ }
}
$paymentOutput = new PaymentDoneOutput('899875557', new \DateTimeInmutable('2023-03-05 12:25');
return new ApiOutput($paymentOutput, 202);
return new ApiOutput($paymentOutput, 202, 'admin');
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\Validator\Constraints\NotBlank;
class SendPaymentOperationInput
{
#[NotBlank(message: 'From cannot be empty')]
private string $from;
#[NotBlank(message: 'To cannot be empty')]
private string $to;
#[NotBlank(message: 'Amount cannot be empty')]
#[GreaterThan(0, message: 'Amount must be greater then 0')]
private string $amount;
// getters & setters
}
use Ict\ApiOneEndpoint\Contract\Operation\OperationInterface;
use Ict\ApiOneEndpoint\Model\Api\ApiOutput;
class SendPaymentOperation implements OperationInterface
{
public function perform(mixed $operationData): ApiOutput
{
// Perform operation ....
// Sending bizum, BTC ......
return new ApiOutput([], 200);
}
public function getName(): string
{
return 'SendPayment';
}
public function getInput(): ?string
{
return SendPaymentOperationInput::class;
}
public function getGroup(): ?string
{
return null;
}
public function getContext() : ?array
{
return null;
}
}
use Ict\ApiOneEndpoint\Model\Operation\OperationSubject;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class SendPaymentVoter extends Voter
{
protected function supports(string $attribute, mixed $subject): bool
{
if(!$subject instanceof OperationSubject){
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if(!in_array('ROLE_PAYMENT', $user->getRoles())){
return false;
}
return true;
}
}
use Ict\ApiOneEndpoint\Model\Operation\OperationSubject;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class AccountManagementVoter extends Voter
{
protected function supports(string $attribute, mixed $subject): bool
{
if(!$subject instanceof OperationSubject){
return false;
}
return $subject->group === 'ACCOUNT';
}
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if(!in_array('ROLE_ADMIN', $user->getRoles())){
return false;
}
return true;
}
}
use Ict\ApiOneEndpoint\Contract\Operation\OperationInterface;
use Ict\ApiOneEndpoint\Model\Api\ApiOutput;
use Ict\ApiOneEndpoint\Model\Attribute\IsBackground;
#[IsBackground]
class SendPaymentOperation implements OperationInterface
{
.......
}
use Ict\ApiOneEndpoint\Contract\Operation\OperationInterface;
use Ict\ApiOneEndpoint\Model\Api\ApiOutput;
use Ict\ApiOneEndpoint\Model\Attribute\IsBackground;
#[IsBackground(delay: 300)]
class SendPaymentOperation implements OperationInterface
{
.......
}
class OperationSubscriber implements EventSubscriberInterface {
public static function getSubscribedEvents(): array
{
return [
OperationPerformedEvent::class => ['onOperationPerformed']
]
}
public function onOperationPerformed(OperationPerformedEvent $event): void
{
// events gives you access to operation name and operation result:
$opName = $event->operation;
$opResult = $event->operationResult;
// some stuff here .....
}
}
public function getContext(): ?array
{
return ['client'];
}
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Ict\ApiOneEndpoint\Operation\OperationHandler;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
use Ict\ApiOneEndpoint\Model\Api\Context;
#[Route('/api/v1')]
class BackendController extends AbstractController
{
use \Ict\ApiOneEndpoint\Controller\OperationControllerTrait;
#[Route('', name: 'api_backend_v1_process_operation', methods: ['POST'])]
public function processOperation(Request $request, SerializerInterface $serializer, OperationHandler $operationHandler): JsonResponse
{
return $this->executeOperation($request, $serializer, $operationHandler, new Context());
}
}
return $this->executeOperation($request, $serializer, $operationHandler, new Context('client'));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.