Domain Driven Design related Classes and Utilities

v6.0.0 2023-05-06 23:29 UTC

This package is auto-updated.

Last update: 2024-11-07 02:39:47 UTC


README

Domain-Driven Design related Classes and Utilities.

Codacy Badge Latest Stable Version Total Downloads Minimum PHP Version Build Status Coverage Status

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());