PHP code example of riverwaysoft / api-tools

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

    

riverwaysoft / api-tools example snippets


class UserRegisteredMessage {
    public function __construct(public string $username) {}
}

use Riverwaysoft\ApiTools\DomainEvents\EventSourceCollector;
class User extends EventSourceCollector {
    
    public function signUp(string $username){
        $this->rememberMessage(new UserRegisteredMessage($username));
    }
    
}

# After that message can be consumed with:

$user = new User();
$messages = $user->popMessages();

    $services->set(Riverwaysoft\ApiTools\DomainEvents\Doctrine\DoctrineDomainEventsCollector::class)->public()
        ->tag('doctrine.event_listener', ['event' => "postPersist"])
        ->tag('doctrine.event_listener', ['event' => "postUpdate"])
        ->tag('doctrine.event_listener', ['event' => "postFlush"])
        ->tag('doctrine.event_listener', ['event' => "postLoad"]);

class UserFilter
{
    public function __construct(
        public int $ageGreaterThan,
        public string $name,
    ) {
    }
}

class CreateUserInput {
    public function __construct(
        public int $age,
        public string $name,
    ) {
    } 
}

use Riverwaysoft\ApiTools\InputValueResolver\Query;
use Riverwaysoft\ApiTools\InputValueResolver\Input;

class UserController
{
    #[Route('/api/users', methods: ['GET'])]
    public function getUsers(#[Query] UserFilter $userFilter)
    {
        // Use $userFilter for requests like
        // /api/users?ageGreaterThan=18&name=test
    }
    
    #[Route('/api/users', methods: ['POST'])]
    public function createUser(#[Input] CreateUserInput $input)
    {
        // variable $input will be automatically created
        // from the request body   
    }
}

use Riverwaysoft\ApiTools\Testing\UnicodeIgnoreOrderJsonDriver;

//

public function assertMatchesJsonUnicodeSnapshot(mixed $actual): void
{
    $this->assertMatchesSnapshot($actual, new UnicodeIgnoreOrderJsonDriver());
}