Last active
May 7, 2020 18:38
-
-
Save ianrodrigues/ddc534f365cae53843c04687857d4f74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function addPhoneNumber(string $phone): void {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
/** @Embeddable */ | |
final class Price | |
{ | |
/** @Column(type="float") */ | |
private $amount; | |
/** @Column(type="string") */ | |
private $currency; | |
public function __construct(float $amount, string $currency = 'USD') | |
{ | |
// ... | |
$this->amount = $amount; | |
$this->currency = $currency; | |
} | |
// ... | |
} | |
/** @Entity */ | |
class Product | |
{ | |
/** @Embedded(class="Price") */ | |
private $price; | |
public function __construct(Price $price) | |
{ | |
$this->price = $price; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
final class Price | |
{ | |
// ... | |
private function hasSameCurrency(Price $price): bool | |
{ | |
return $this->currency === $price->currency; | |
} | |
public function sum(Price $price): self | |
{ | |
if (!$this->hasSameCurrency($price)) { | |
throw \InvalidArgumentException( | |
"You can only sum values with the same currency: {$this->currency} !== {$price->currency}." | |
); | |
} | |
return new self($this->amount + $price->amount, $this->currency); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
final class Price | |
{ | |
const USD = 'USD'; | |
const CAD = 'CAD'; | |
/** @var float */ | |
private $amount; | |
/** @var string */ | |
private $currency; | |
public function __construct(float $amount, string $currency = 'USD') | |
{ | |
if ($amount < 0) { | |
throw new \InvalidArgumentException("Amount should be a positive value: {$amount}."); | |
} | |
if (!in_array($currency, $this->getAvailableCurrencies())) { | |
throw new \InvalidArgumentException("Currency should be a valid one: {$currency}."); | |
} | |
$this->amount = $amount; | |
$this->currency = $currency; | |
} | |
private function getAvailableCurrencies(): array | |
{ | |
return [self::USD, self::CAD]; | |
} | |
public function getAmount(): float | |
{ | |
return $this->amount; | |
} | |
public function getCurrency(): string | |
{ | |
return $this->currency; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
final class Price | |
{ | |
// ... | |
public function __construct(float $amount, string $currency = 'USD') | |
{ | |
if ($amount < 0) { | |
throw new \InvalidArgumentException("Amount should be a positive value: {$amount}."); | |
} | |
if (!in_array($currency, $this->getAvailableCurrencies())) { | |
throw new \InvalidArgumentException("Currency should be a valid one: {$currency}."); | |
} | |
$this->amount = $amount; | |
$this->currency = $currency; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
final class Price | |
{ | |
// ... | |
public function isEqualsTo(Price $price): bool | |
{ | |
return $this->amount === $price->amount && $this->currency === $price->currency; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function addPhoneNumber(PhoneNumber $phone): void {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
final class Price | |
{ | |
// ... | |
private function hash(): string | |
{ | |
return md5("{$this->amount}{$this->currency}"); | |
} | |
public function isEqualsTo(Price $price): bool | |
{ | |
return $this->hash() === $price->hash(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment