PHP code example of net-code / laravel-domain

1. Go to this page and download the library: Download net-code/laravel-domain 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/ */

    

net-code / laravel-domain example snippets


use NetCode\Domain\Identifier\Uuid;

final class OrderId extends Uuid {}

$id = OrderId::random();          // UUIDv7
$id->equals(OrderId::fromString((string) $id)); // true

use NetCode\Domain\AggregateRoot;

final class Order extends AggregateRoot
{
    public function place(): void
    {
        // ... mutate state ...
        $this->recordThat(new OrderPlaced(/* ... */));
    }
}

$events = $order->releaseEvents(); // hand to your DomainEventPublisher

use NetCode\Domain\Rule\Specification;

Specification::check(
    new OrderMustHaveLines($order),
    new OrderMustBeUnpaid($order),
); // throws BusinessRuleException on the first broken rule

use Illuminate\Database\Eloquent\Model;
use NetCode\Domain\Laravel\IdentifierCast;

final class OrderModel extends Model
{
    /** @return array<string, string> */
    protected function casts(): array
    {
        return ['id' => IdentifierCast::class.':'.OrderId::class];
    }
}