PHP code example of thombas / revised-service-pattern

1. Go to this page and download the library: Download thombas/revised-service-pattern 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/ */

    

thombas / revised-service-pattern example snippets


use Thombas\RevisedServicePattern\Services\RestApiService;

class MyApiService extends RestApiService
{
    protected function setup(): static
    {
        return $this->baseUrl('https://api.example.com')->asJson();
    }
}

class FetchUserEndpoint extends MyApiService
{
    protected function setup(): static
    {
        return parent::setup()
            ->setUrl('user')
            ->setMethod(ServiceMethodEnum::Get)
            ->setParamaters(['id' => 123]);
    }
}

public function __construct(protected string $userId)
{
    parent::__construct();
}

protected function setup(): static
{
    return parent::setup()
        ->setUrl('user')
        ->setMethod(ServiceMethodEnum::Get)
        ->setParamaters(['id' => 123]);
}

protected function before(): ?Response
{
    return Cache::get("user_{$this->userId}");
}

protected function validate(Response $response): void
{
    if ($response->json('status') !== 'success') {
        throw new \Exception("API error: " . $response->json('message'));
    }
}

protected function after(Response $response): void
{
    Cache::put("user_{$this->userId}", $response->json('data'), now()->addMinutes(10));
}

protected function format(Response $response): mixed
{
    return [
        'id' => $response->json('data.id'),
        'name' => $response->json('data.full_name'),
    ];
}

$response = MyApiService::fetchUser();

public function __construct(public string $code = 'test')
{
    parent::__construct();
}

public function __invoke(): array
{
    return ['Code' => $this->code];
}

protected function validator(): ?\Illuminate\Validation\Validator
{
    return \Illuminate\Support\Facades\Validator::make(
        ['code' => $this->code],
        ['code' => ['

$response = MyApiService::fetchUser(new FetchUserTemplate());
bash
php artisan template:create OpenWeather
bash
php artisan template:create OpenWeather --endpoint=Thunderstorms
bash
php artisan template:create OpenWeather --endpoint=Thunderstorms/Frequency