Skip to content

Instantly share code, notes, and snippets.

@ianrodrigues
Last active May 7, 2020 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianrodrigues/ddc534f365cae53843c04687857d4f74 to your computer and use it in GitHub Desktop.
Save ianrodrigues/ddc534f365cae53843c04687857d4f74 to your computer and use it in GitHub Desktop.
public function addPhoneNumber(string $phone): void {}
<?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;
}
}
<?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);
}
}
<?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;
}
}
<?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;
}
}
<?php declare(strict_types=1);
final class Price
{
// ...
public function isEqualsTo(Price $price): bool
{
return $this->amount === $price->amount && $this->currency === $price->currency;
}
}
public function addPhoneNumber(PhoneNumber $phone): void {}
<?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