Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customers name and surname can not be empty strings and e-mail must be valid e-mail address #71

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"require": {
"php": "~7.4|~8.0",
"ext-json": "*",
"egulias/email-validator": "^3.2",
"psr/http-client": "^1.0",
"psr/http-factory": "^1.0",
"psr/http-message": "^1.0|^2.0"
Expand Down
2 changes: 1 addition & 1 deletion doc/create-payment-recommended.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Example with optional detail information about customer.
$customer = new \ThePay\ApiClient\Model\CreatePaymentCustomer(
'Mike',
'Smith',
'mike.smith@example.com',
'mike.smith@universal-acceptance-test.icu',
// Phone number in international format max 15 numeric chars https://en.wikipedia.org/wiki/MSISDN
'420589687963',
// Create billing address
Expand Down
19 changes: 10 additions & 9 deletions src/Model/CreatePaymentCustomer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -54,15 +55,15 @@ public function getSurname(): string
*/
public function getEmail()
{
return $this->email === null ? null : $this->email->getValue();
return $this->email;
}

/**
* @return string|null
*/
public function getPhone()
{
return $this->phone === null ? null : $this->phone->getValue();
return $this->phone;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/ValueObject/Amount.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use InvalidArgumentException;

/**
* @extends BaseValueObject<int>
*/
final class Amount extends BaseValueObject
{
/** @var int */
private $value;

/**
* Amount constructor.
*
Expand Down
54 changes: 50 additions & 4 deletions src/ValueObject/BaseValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@

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
*/
public function __construct($value)
{
$this->value = static::filter($value);
}

/**
* @param mixed $value
* @param TValue|mixed $value
*
* @return static
*
* @throws InvalidArgumentException
*/
public static function create($value)
{
Expand All @@ -30,4 +47,33 @@ public function equals(ValueObject $object)

return $this->getValue() === $object->getValue();
}

/**
* @return TValue
*/
public function getValue()
{
return $this->value;
}

/**
* @note 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,
);
}
}
6 changes: 3 additions & 3 deletions src/ValueObject/CountryCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace ThePay\ApiClient\ValueObject;

/**
* @extends BaseValueObject<string>
*/
final class CountryCode extends BaseValueObject
{
/** @var string */
private $value;

/**
* @param string $value
*/
Expand Down
6 changes: 3 additions & 3 deletions src/ValueObject/CurrencyCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use InvalidArgumentException;

/**
* @extends BaseValueObject<string>
*/
final class CurrencyCode extends BaseValueObject
{
/** @var string */
private $value;

/**
* CurrencyCode constructor.
*
Expand Down
27 changes: 27 additions & 0 deletions src/ValueObject/EmailAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

petrknap marked this conversation as resolved.
Show resolved Hide resolved
declare(strict_types=1);

namespace ThePay\ApiClient\ValueObject;

use Egulias\EmailValidator\EmailValidator;
use Egulias\EmailValidator\Validation\DNSCheckValidation;
use Egulias\EmailValidator\Validation\MultipleValidationWithAnd;
use Egulias\EmailValidator\Validation\RFCValidation;

class EmailAddress extends NonEmptyString
{
public static function filter($value)
{
$nonEmptyString = parent::filter($value);

if ((new EmailValidator())->isValid($nonEmptyString, new MultipleValidationWithAnd([
new RFCValidation(),
new DNSCheckValidation(),
])) === false) {
throw self::invalidValue('public e-mail address', $nonEmptyString);
}

return $nonEmptyString;
}
}
6 changes: 3 additions & 3 deletions src/ValueObject/EnumValueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace ThePay\ApiClient\ValueObject;

/**
* @extends BaseValueObject<string>
*/
abstract class EnumValueObject extends BaseValueObject
{
/** @var string */
protected $value;

/**
* @param string $value
*/
Expand Down
6 changes: 3 additions & 3 deletions src/ValueObject/Identifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use InvalidArgumentException;

/**
* @extends BaseValueObject<string>
*/
final class Identifier extends BaseValueObject
{
/** @var string */
private $value;

/**
* Uid constructor.
*
Expand Down
6 changes: 3 additions & 3 deletions src/ValueObject/LanguageCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

use InvalidArgumentException;

/**
* @extends BaseValueObject<string>
*/
final class LanguageCode extends BaseValueObject
{
/** @var string */
private $value;

/**
* CurrencyCode constructor.
*
Expand Down
19 changes: 19 additions & 0 deletions src/ValueObject/NonEmptyString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace ThePay\ApiClient\ValueObject;

class NonEmptyString extends StringValue
{
protected static function filter($value)
{
$string = parent::filter($value);

if (trim($string) === '') {
petrknap marked this conversation as resolved.
Show resolved Hide resolved
throw self::invalidValue('non-empty string', $string);
}

return $string;
}
}
3 changes: 3 additions & 0 deletions src/ValueObject/PhoneNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use InvalidArgumentException;

/**
* @extends BaseValueObject<string>
*/
final class PhoneNumber extends BaseValueObject
{
/** @var string */
Expand Down
3 changes: 3 additions & 0 deletions src/ValueObject/SecureUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace ThePay\ApiClient\ValueObject;

/**
* @extends BaseValueObject<string>
*/
final class SecureUrl extends BaseValueObject
{
/** @var string */
Expand Down
31 changes: 10 additions & 21 deletions src/ValueObject/StringValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,22 @@

namespace ThePay\ApiClient\ValueObject;

final class StringValue extends BaseValueObject
/**
* @extends BaseValueObject<string>
*/
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;
}
}
3 changes: 3 additions & 0 deletions src/ValueObject/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use InvalidArgumentException;

/**
* @extends BaseValueObject<string>
*/
final class Url extends BaseValueObject
{
/** @var string */
Expand Down
10 changes: 5 additions & 5 deletions tests/CreatePaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public static function createButtonProvider(): array
return [
[
new CreatePaymentParams(100, 'CZK', '202001010001', self::getCreatePaymentCustomer()),
'eyJhbW91bnQiOjEwMCwiY3VycmVuY3lfY29kZSI6IkNaSyIsInVpZCI6IjIwMjAwMTAxMDAwMSIsImxhbmd1YWdlX2NvZGUiOiJjcyIsImlzX2RlcG9zaXQiOnRydWUsInNhdmVfYXV0aG9yaXphdGlvbiI6ZmFsc2UsImNhbl9jdXN0b21lcl9jaGFuZ2VfbWV0aG9kIjp0cnVlLCJjdXN0b21lcl9uYW1lIjoiTWlrZSIsImN1c3RvbWVyX3N1cm5hbWUiOiJTbWl0aCIsImN1c3RvbWVyX2VtYWlsIjoibWlrZS5zbWl0aEBleGFtcGxlLmNvbSIsImN1c3RvbWVyX3Bob25lIjoiNDIwNTg5Njg3OTYzIiwiY3VzdG9tZXJfYmlsbGluZ19jb3VudHJ5X2NvZGUiOiJDWiIsImN1c3RvbWVyX2JpbGxpbmdfY2l0eSI6IlByYWd1ZSIsImN1c3RvbWVyX2JpbGxpbmdfemlwIjoiMTIzIDAwIiwiY3VzdG9tZXJfYmlsbGluZ19zdHJlZXQiOiJEb3duc3RyZWV0IDUiLCJtZXJjaGFudF9pZCI6Ijg2YTNlZWQwLTk1YTQtMTFlYS1hYzlmLTM3MWYzNDg4ZTBmYSIsInByb2plY3RfaWQiOjF9',
'bbad8c591d54fa52353f680363129a9949c6de23659140a36fc799d762a92a80',
'eyJhbW91bnQiOjEwMCwiY3VycmVuY3lfY29kZSI6IkNaSyIsInVpZCI6IjIwMjAwMTAxMDAwMSIsImxhbmd1YWdlX2NvZGUiOiJjcyIsImlzX2RlcG9zaXQiOnRydWUsInNhdmVfYXV0aG9yaXphdGlvbiI6ZmFsc2UsImNhbl9jdXN0b21lcl9jaGFuZ2VfbWV0aG9kIjp0cnVlLCJjdXN0b21lcl9uYW1lIjoiTWlrZSIsImN1c3RvbWVyX3N1cm5hbWUiOiJTbWl0aCIsImN1c3RvbWVyX2VtYWlsIjoibWlrZS5zbWl0aEB1bml2ZXJzYWwtYWNjZXB0YW5jZS10ZXN0LmljdSIsImN1c3RvbWVyX3Bob25lIjoiNDIwNTg5Njg3OTYzIiwiY3VzdG9tZXJfYmlsbGluZ19jb3VudHJ5X2NvZGUiOiJDWiIsImN1c3RvbWVyX2JpbGxpbmdfY2l0eSI6IlByYWd1ZSIsImN1c3RvbWVyX2JpbGxpbmdfemlwIjoiMTIzIDAwIiwiY3VzdG9tZXJfYmlsbGluZ19zdHJlZXQiOiJEb3duc3RyZWV0IDUiLCJtZXJjaGFudF9pZCI6Ijg2YTNlZWQwLTk1YTQtMTFlYS1hYzlmLTM3MWYzNDg4ZTBmYSIsInByb2plY3RfaWQiOjF9',
'583da369ef16a57119975981a86407693f22875b0b27804769cf08ecbf0ee16c',
],
[
new CreatePaymentParams(100, 'EUR', '202001010002', self::getCreatePaymentCustomer()),
'eyJhbW91bnQiOjEwMCwiY3VycmVuY3lfY29kZSI6IkVVUiIsInVpZCI6IjIwMjAwMTAxMDAwMiIsImxhbmd1YWdlX2NvZGUiOiJjcyIsImlzX2RlcG9zaXQiOnRydWUsInNhdmVfYXV0aG9yaXphdGlvbiI6ZmFsc2UsImNhbl9jdXN0b21lcl9jaGFuZ2VfbWV0aG9kIjp0cnVlLCJjdXN0b21lcl9uYW1lIjoiTWlrZSIsImN1c3RvbWVyX3N1cm5hbWUiOiJTbWl0aCIsImN1c3RvbWVyX2VtYWlsIjoibWlrZS5zbWl0aEBleGFtcGxlL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',
'7f7ee7a177bdd4cee0287283a2c497f0511a76a89464e52ae72b0b372c7be6ce',
],
];
}
Expand Down Expand Up @@ -157,7 +157,7 @@ private static function getCreatePaymentCustomer(): CreatePaymentCustomer
return new CreatePaymentCustomer(
'Mike',
'Smith',
'mike.smith@example.com',
'mike.smith@universal-acceptance-test.icu',
// Phone number in international format max 15 numeric chars https://en.wikipedia.org/wiki/MSISDN
'420589687963',
// Create billing address
Expand Down
Loading
Loading