diff --git a/src/Model/CreatePaymentCustomer.php b/src/Model/CreatePaymentCustomer.php index 205a97f..8446a4d 100644 --- a/src/Model/CreatePaymentCustomer.php +++ b/src/Model/CreatePaymentCustomer.php @@ -3,16 +3,17 @@ namespace ThePay\ApiClient\Model; use InvalidArgumentException; +use ThePay\ApiClient\ValueObject\EmailAddress; +use ThePay\ApiClient\ValueObject\NonEmptyString; use ThePay\ApiClient\ValueObject\PhoneNumber; -use ThePay\ApiClient\ValueObject\StringValue; final class CreatePaymentCustomer { private string $name; private string $surname; - /** @var StringValue|null */ + /** @var string|null */ private $email; - /** @var PhoneNumber|null */ + /** @var string|null */ private $phone; /** @var Address|null */ private $billingAddress; @@ -31,10 +32,10 @@ public function __construct(string $name, string $surname, $email, $phone, Addre throw new InvalidArgumentException('At least one of $email and $phone is required.'); } - $this->name = $name; - $this->surname = $surname; - $this->email = $email === null ? null : new StringValue($email); - $this->phone = $phone === null ? null : new PhoneNumber($phone); + $this->name = (new NonEmptyString($name))->getValue(); + $this->surname = (new NonEmptyString($surname))->getValue(); + $this->email = $email === null ? null : (new EmailAddress($email))->getValue(); + $this->phone = $phone === null ? null : (new PhoneNumber($phone))->getValue(); $this->billingAddress = $billingAddress; $this->shippingAddress = $shippingAddress; } @@ -54,7 +55,7 @@ public function getSurname(): string */ public function getEmail() { - return $this->email === null ? null : $this->email->getValue(); + return $this->email; } /** @@ -62,7 +63,7 @@ public function getEmail() */ public function getPhone() { - return $this->phone === null ? null : $this->phone->getValue(); + return $this->phone; } /** diff --git a/src/ValueObject/Amount.php b/src/ValueObject/Amount.php index 1056318..5d7d71a 100644 --- a/src/ValueObject/Amount.php +++ b/src/ValueObject/Amount.php @@ -4,11 +4,11 @@ use InvalidArgumentException; +/** + * @extends BaseValueObject + */ final class Amount extends BaseValueObject { - /** @var int */ - private $value; - /** * Amount constructor. * diff --git a/src/ValueObject/BaseValueObject.php b/src/ValueObject/BaseValueObject.php index e4440eb..f660840 100644 --- a/src/ValueObject/BaseValueObject.php +++ b/src/ValueObject/BaseValueObject.php @@ -2,13 +2,27 @@ namespace ThePay\ApiClient\ValueObject; +use InvalidArgumentException; + +/** + * @template TValue of mixed + */ abstract class BaseValueObject implements ValueObject { /** - * BaseValueObject constructor. - * @param mixed $value + * @var TValue */ - abstract public function __construct($value); + protected $value; + + /** + * @param TValue|mixed $value + * + * @throws InvalidArgumentException + */ + final public function __construct($value) + { + $this->value = static::filter($value); + } /** * @param mixed $value @@ -30,4 +44,34 @@ public function equals(ValueObject $object) return $this->getValue() === $object->getValue(); } + + + /** + * @return TValue + */ + public function getValue() + { + return $this->value; + } + + /** + * @internal should be abstract + * + * @param TValue|mixed $value + * + * @return TValue + * + * @throws InvalidArgumentException + */ + protected static function filter($value) + { + throw self::invalidValue('expected'); + } + + protected static function invalidValue(string $expected, ?string $actual = null): InvalidArgumentException + { + return new InvalidArgumentException( + 'Value ' . ($actual === null ? '' : '"' . $actual . '" ') . 'is not ' . $expected, + ); + } } diff --git a/src/ValueObject/CountryCode.php b/src/ValueObject/CountryCode.php index dc9275e..9fad216 100644 --- a/src/ValueObject/CountryCode.php +++ b/src/ValueObject/CountryCode.php @@ -2,11 +2,11 @@ namespace ThePay\ApiClient\ValueObject; +/** + * @extends BaseValueObject + */ final class CountryCode extends BaseValueObject { - /** @var string */ - private $value; - /** * @param string $value */ diff --git a/src/ValueObject/CurrencyCode.php b/src/ValueObject/CurrencyCode.php index 653de62..5276f78 100644 --- a/src/ValueObject/CurrencyCode.php +++ b/src/ValueObject/CurrencyCode.php @@ -4,11 +4,11 @@ use InvalidArgumentException; +/** + * @extends BaseValueObject + */ final class CurrencyCode extends BaseValueObject { - /** @var string */ - private $value; - /** * CurrencyCode constructor. * diff --git a/src/ValueObject/EmailAddress.php b/src/ValueObject/EmailAddress.php new file mode 100644 index 0000000..7fa515b --- /dev/null +++ b/src/ValueObject/EmailAddress.php @@ -0,0 +1,17 @@ + + */ abstract class EnumValueObject extends BaseValueObject { - /** @var string */ - protected $value; - /** * @param string $value */ diff --git a/src/ValueObject/Identifier.php b/src/ValueObject/Identifier.php index ed86a22..ee77934 100644 --- a/src/ValueObject/Identifier.php +++ b/src/ValueObject/Identifier.php @@ -4,11 +4,11 @@ use InvalidArgumentException; +/** + * @extends BaseValueObject + */ final class Identifier extends BaseValueObject { - /** @var string */ - private $value; - /** * Uid constructor. * diff --git a/src/ValueObject/LanguageCode.php b/src/ValueObject/LanguageCode.php index f384464..6a5348a 100644 --- a/src/ValueObject/LanguageCode.php +++ b/src/ValueObject/LanguageCode.php @@ -4,11 +4,11 @@ use InvalidArgumentException; +/** + * @extends BaseValueObject + */ final class LanguageCode extends BaseValueObject { - /** @var string */ - private $value; - /** * CurrencyCode constructor. * diff --git a/src/ValueObject/NonEmptyString.php b/src/ValueObject/NonEmptyString.php new file mode 100644 index 0000000..6e54759 --- /dev/null +++ b/src/ValueObject/NonEmptyString.php @@ -0,0 +1,17 @@ + + */ final class PhoneNumber extends BaseValueObject { /** @var string */ diff --git a/src/ValueObject/SecureUrl.php b/src/ValueObject/SecureUrl.php index ed9ff78..9193d0a 100644 --- a/src/ValueObject/SecureUrl.php +++ b/src/ValueObject/SecureUrl.php @@ -2,6 +2,9 @@ namespace ThePay\ApiClient\ValueObject; +/** + * @extends BaseValueObject + */ final class SecureUrl extends BaseValueObject { /** @var string */ diff --git a/src/ValueObject/StringValue.php b/src/ValueObject/StringValue.php index da0401a..50443bb 100644 --- a/src/ValueObject/StringValue.php +++ b/src/ValueObject/StringValue.php @@ -2,33 +2,22 @@ namespace ThePay\ApiClient\ValueObject; -final class StringValue extends BaseValueObject +/** + * @extends BaseValueObject + */ +class StringValue extends BaseValueObject { - /** @var string */ - private $value; - - public function __construct($value) - { - if ( ! is_string($value)) { - throw new \InvalidArgumentException('type of value: ' . (string) $value . ' is not string'); - } - - $this->value = $value; - } - - /** - * @return string - */ public function __toString() { return $this->value; } - /** - * @return string - */ - public function getValue() + protected static function filter($value) { - return $this->value; + if ( ! is_string($value)) { + throw self::invalidValue('string'); + } + + return $value; } } diff --git a/src/ValueObject/Url.php b/src/ValueObject/Url.php index dc6d970..cb5831c 100644 --- a/src/ValueObject/Url.php +++ b/src/ValueObject/Url.php @@ -4,6 +4,9 @@ use InvalidArgumentException; +/** + * @extends BaseValueObject + */ final class Url extends BaseValueObject { /** @var string */ diff --git a/src/ValueObject/ValueObject.php b/src/ValueObject/ValueObject.php index 00ba024..c93e893 100644 --- a/src/ValueObject/ValueObject.php +++ b/src/ValueObject/ValueObject.php @@ -4,11 +4,6 @@ interface ValueObject { - /** - * @param mixed $value - */ - public function __construct($value); - /** * @return string */