PHP code example of pamellayamada / pamytools

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

    

pamellayamada / pamytools example snippets


namespace App\DTOs;

use Pamytools\Attributes\ExtractPath;

final readonly class CompanyDTO 
{
    public function __construct(
        #[ExtractPath(query: "//h1[@class='title']")]
        public ?string $companyName,

        #[ExtractPath(query: '/token=([a-z0-9]+)/', type: 'regex')]
        public ?string $csrfToken
    ) {}
}

use App\DTOs\CompanyDTO;
use Pamytools\Http\AsyncHttpEngine;
use Pamytools\Resilience\CircuitBreaker;
use Pamytools\Parser\DataHydrator;
use Pamytools\I18n\Translator;

// 1. Set global translation locale (Default: pt-BR. Available: en-US, es-ES, pt-BR)
Translator::setLocale('en-US');

// 2. Initialize HTTP client and Circuit Breaker guard
$http = new AsyncHttpEngine();
$breaker = new CircuitBreaker(serviceName: 'ExternalAPI', failureThreshold: 3);

// 3. Execute request safely
$response = $breaker->execute(
    fn() => $http->send('GET', 'https://example.com')
);

// 4. Hydrate HTML directly into DTO
$hydrator = new DataHydrator();

/** @var CompanyDTO $dto */
$dto = $hydrator->hydrate($response->body, CompanyDTO::class);

echo $dto->companyName; // Outputs extracted text from //h1[@class='title']

pamytools/
├── .github/
│   └── workflows/
│       └── tests.yml                 <-- CI/CD Pipeline
├── lang/                             <-- I18n Translation Dictionaries
│   ├── en-US.json
│   ├── es-ES.json
│   └── pt-BR.json
├── src/
│   ├── Attributes/
│   │   └── ExtractPath.php           <-- PHP 8.2+ Attribute definition
│   ├── Contracts/
│   │   ├── HttpClientInterface.php   <-- SOLID Interfaces
│   │   └── HydratorInterface.php
│   ├── Exception/
│   │   ├── PamytoolsException.php    <-- Translated Core Exceptions
│   │   ├── CircuitBreakerOpenException.php
│   │   └── HydrationException.php
│   ├── Http/
│   │   ├── AsyncHttpEngine.php       <-- Native Multi-cURL Driver
│   │   └── HttpResponse.php
│   ├── I18n/
│   │   └── Translator.php            <-- Global Translation Engine
│   ├── Parser/
│   │   └── DataHydrator.php          <-- Hydration Engine (DOMXPath/Regex)
│   └── Resilience/
│       └── CircuitBreaker.php        <-- Circuit Breaker Guard
└── tests/                            <-- PHPUnit Test Suite