dddshelf / ddd-in-php-book-issues

Leave your comments, improvements or book mistakes as an issue! Thanks ❤️
https://leanpub.com/ddd-in-php
28 stars 2 forks source link

Chapter: Value Object, Money code example #71

Closed taiiiraaa closed 7 years ago

taiiiraaa commented 7 years ago

Version: PDF Chapter: Value Object Section: Side-Effect-Free Behavior Page: 54 - 55

In the two partial Money Class examples given, is it correct in example 1 to have the condition $money->currency() !== $this->currency()? Should it not be the same as example 2$money->currency()->equals($this->currency())?

Example 1 from page 54

class Money
{
    // ...

    public function add(Money $money)
    {
        if ($money->currency() !== $this->currency()) {
            throw new \InvalidArgumentException();
         }

         $this->amount += $money->amount();
     }
}

Example 2 from page 55

class Money
{
    // ...

    public function add(Money $money)
    {
        if(!$money->currency()->equals($this->currency())) {
            throw new \InvalidArgumentException();
        }

        // start-of-new-code-to-take-a-look
        return new self(
            $this->amount() + $money->amount(),
            $this->currency()
        );
        // end-of-new-code-to-take-a-look
    }
}
carlosbuenosvinos commented 7 years ago

Thanks. Fixed. Could you tell me your name and lastname?