bus-factor / ddd
Domain Driven Design related Classes and Utilities
Installs: 8 213
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- ext-json: *
- ramsey/uuid: >=4.1
Requires (Dev)
- phpunit/phpunit: >=5.7
README
Domain-Driven Design related Classes and Utilities.
Model
Value Objects
The following value object classes are available:
EmailAddress
Enum
IpAddress
IpV4Address
IpV6Address
MacAddress
SingleValueObject
(Single-value object base class)Url
Sample usage
/** * @method static Currency EUR() * @method static Currency USD() */ class Currency extends \BusFactor\Ddd\ValueObject\Enum { public const EUR = 'EUR'; public const USD = 'USD'; } class Decimal implements \BusFactor\Ddd\ComparableInterface { use \BusFactor\Ddd\ComparableTrait; // ... public function compareTo(\BusFactor\Ddd\ComparableInterface $subject) : int { } // ... } class Money implements \BusFactor\Ddd\ComparableInterface { use \BusFactor\Ddd\ComparableTrait; /** @var Decimal $amount*/ private $amount; /** @var Currency $currency */ private $currency; /** * @param Decimal $amount * @param Currency $currency */ public function __construct(Decimal $amount, Currency $currency) { $this->amount = $amount; $this->currency = $currency; } /** * @param \BusFactor\Ddd\ComparableInterface $subject * @return int */ public function compareTo(\BusFactor\Ddd\ComparableInterface $subject) : int { $comparable = $subject instanceof static::class && $this->currency === $subject->currency; if (!$comparable) { throw new LogicException('Incompatible currencies'); } return $this->amount->compareTo($subject->amount); } } $money = new Money(new Decimal(/* ... */), Currency::EUR()); $otherMoney = new Money(new Decimal(/* ... */), Currency::EUR());