PHP code example of solidframe / core

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

    

solidframe / core example snippets


use SolidFrame\Core\Identity\AbstractIdentity;
use SolidFrame\Core\Identity\UuidIdentity;

// Simple identity
final readonly class UserId extends AbstractIdentity {}

$id = new UserId('user-123');
$id->value();              // 'user-123'
$id->equals(new UserId('user-123')); // true

// UUID identity with auto-generation
final readonly class OrderId extends UuidIdentity {}

$id = OrderId::generate(); // random UUIDv4

use SolidFrame\Core\Bus\CommandBusInterface;
use SolidFrame\Core\Bus\QueryBusInterface;
use SolidFrame\Core\Bus\EventBusInterface;

// Command — side effect, no return value
$commandBus->dispatch($command); // void

// Query — returns data, no side effect
$result = $queryBus->ask($query); // mixed

// Event — notification, no return value
$eventBus->dispatch($event); // void

use SolidFrame\Core\Event\DomainEventInterface;

final readonly class OrderPlaced implements DomainEventInterface
{
    public function __construct(
        private string $orderId,
        private DateTimeImmutable $occurredAt = new DateTimeImmutable(),
    ) {}

    public function eventName(): string
    {
        return 'order.placed';
    }

    public function occurredAt(): DateTimeImmutable
    {
        return $this->occurredAt;
    }
}

use SolidFrame\Core\Pipeline\Pipeline;

$pipeline = new Pipeline();

$result = $pipeline
    ->pipe(fn (string $payload) => strtoupper($payload))
    ->pipe(fn (string $payload) => trim($payload))
    ->process('  hello world  ');

// 'HELLO WORLD'

use SolidFrame\Core\Pipeline\StageInterface;

final readonly class FormatPrice implements StageInterface
{
    public function __invoke(mixed $payload): mixed
    {
        return number_format($payload / 100, 2) . ' TL';
    }
}

$pipeline = (new Pipeline())->pipe(new FormatPrice());
$pipeline->process(1500); // '15.00 TL'

use SolidFrame\Core\Middleware\MiddlewareInterface;

final readonly class LoggingMiddleware implements MiddlewareInterface
{
    public function __construct(private LoggerInterface $logger) {}

    public function handle(object $message, callable $next): mixed
    {
        $this->logger->info('Handling: ' . $message::class);
        $result = $next($message);
        $this->logger->info('Handled: ' . $message::class);

        return $result;
    }
}

use SolidFrame\Core\Exception\EntityNotFoundException;

throw EntityNotFoundException::forId('order-123');
// "Entity with id "order-123" was not found."

throw EntityNotFoundException::forClassAndId(Order::class, 'order-123');
// "Entity App\Domain\Order with id "order-123" was not found."

use SolidFrame\Core\Service\ApplicationServiceInterface;

final readonly class PlaceOrderService implements ApplicationServiceInterface
{
    public function __invoke(PlaceOrderCommand $command): void
    {
        // ...
    }
}