1. Go to this page and download the library: Download migears/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/ */
migears / domain example snippets
use MiGears\Domain\DataAccess;
class UserDomain
{
use DataAccess;
public function __construct(
public readonly int $id,
public readonly string $user_name,
public readonly string $email,
public readonly int $age,
public readonly string $created_at,
) {}
}
final class OrderManager
{
public function __construct(private OrderItemDao $itemDao)
{
OrderDomain::setItemLoader(
fn(OrderDomain $order) => $this->itemDao->getByOrderId($order->id)
);
}
}
use Closure;
use MiGears\Domain\DataAccess;
use RuntimeException;
class OrderDomain
{
use DataAccess;
/** @var null|Closure(self): list<OrderItemDomain> */
private static ?Closure $itemLoader = null;
public function __construct(
public readonly int $id,
public readonly string $title,
) {}
public static function setItemLoader(?callable $loader): void
{
static::$itemLoader = $loader === null ? null : Closure::fromCallable($loader);
}
/** @return list<OrderItemDomain> */
public function items(): array
{
if (static::$itemLoader === null) {
throw new RuntimeException('OrderDomain::setItemLoader() was not called');
}
return (static::$itemLoader)($this);
}
}
$order = OrderDomain::fromArray($row);
$order->items(); // loaded through the Manager's callable
$order->toArray(); // ['id' => ..., 'title' => ...] — loader not
use MiGears\Domain\DataAccess;
use MiGears\Domain\Validatable;
class UserDomain
{
use DataAccess;
use Validatable;
public function __construct(
public readonly string $username,
public readonly string $email,
public readonly int $age = 0,
) {}
protected static function validationRules(): array
{
return [
'username' => ['
use MiGears\Validator\RuleInterface;
final class StrengthRule implements RuleInterface
{
public function __construct(private int $min = 8) {}
public function validate(mixed $value): bool
{
return is_string($value) && strlen($value) >= $this->min;
}
public function getErrorCode(): string { return 'strength'; }
public function getErrorParams(): array { return ['min' => $this->min]; }
}
UserDomain::register(StrengthRule::class);
// in validationRules():
'password' => ['strength' => 12], // → new StrengthRule(12)
'pin' => ['strength' => 4], // → new StrengthRule(4)
use Closure;
use MiGears\Validator\RuleInterface;
final class UniqueEmailRule implements RuleInterface
{
/** @param Closure(string): bool $emailExists */
public function __construct(private Closure $emailExists) {}
public function validate(mixed $value): bool
{
return $value === null || $value === ''
|| !($this->emailExists)((string) $value);
}
public function getErrorCode(): string { return 'uniqueEmail'; }
public function getErrorParams(): array { return []; }
}
final class UserManager
{
public function __construct(private UserDao $dao)
{
UserDomain::register(
new UniqueEmailRule(fn (string $email) => $this->dao->existsByEmail($email))
);
}
}
use MiGears\Domain\DataAccess;
class UserDomain
{
use DataAccess;
public function __construct(
public readonly int $id,
public readonly string $user_name,
public readonly string $email,
public readonly int $age,
public readonly string $created_at,
) {}
}
final class OrderManager
{
public function __construct(private OrderItemDao $itemDao)
{
OrderDomain::setItemLoader(
fn(OrderDomain $order) => $this->itemDao->getByOrderId($order->id)
);
}
}
use Closure;
use MiGears\Domain\DataAccess;
use RuntimeException;
class OrderDomain
{
use DataAccess;
/** @var null|Closure(self): list<OrderItemDomain> */
private static ?Closure $itemLoader = null;
public function __construct(
public readonly int $id,
public readonly string $title,
) {}
public static function setItemLoader(?callable $loader): void
{
static::$itemLoader = $loader === null ? null : Closure::fromCallable($loader);
}
/** @return list<OrderItemDomain> */
public function items(): array
{
if (static::$itemLoader === null) {
throw new RuntimeException('OrderDomain::setItemLoader() was not called');
}
return (static::$itemLoader)($this);
}
}
use MiGears\Domain\DataAccess;
use MiGears\Domain\Validatable;
class UserDomain
{
use DataAccess;
use Validatable;
public function __construct(
public readonly string $username,
public readonly string $email,
public readonly int $age = 0,
) {}
protected static function validationRules(): array
{
return [
'username' => ['
use MiGears\Validator\RuleInterface;
final class StrengthRule implements RuleInterface
{
public function __construct(private int $min = 8) {}
public function validate(mixed $value): bool
{
return is_string($value) && strlen($value) >= $this->min;
}
public function getErrorCode(): string { return 'strength'; }
public function getErrorParams(): array { return ['min' => $this->min]; }
}
UserDomain::register(StrengthRule::class);
// validationRules() 里:
'password' => ['strength' => 12], // → new StrengthRule(12)
'pin' => ['strength' => 4], // → new StrengthRule(4)
use Closure;
use MiGears\Validator\RuleInterface;
final class UniqueEmailRule implements RuleInterface
{
/** @param Closure(string): bool $emailExists */
public function __construct(private Closure $emailExists) {}
public function validate(mixed $value): bool
{
return $value === null || $value === ''
|| !($this->emailExists)((string) $value);
}
public function getErrorCode(): string { return 'uniqueEmail'; }
public function getErrorParams(): array { return []; }
}
final class UserManager
{
public function __construct(private UserDao $dao)
{
UserDomain::register(
new UniqueEmailRule(fn (string $email) => $this->dao->existsByEmail($email))
);
}
}