From 07a04d86f018807ff7ec749b09e6d75284b0e18e Mon Sep 17 00:00:00 2001 From: Meritoo Date: Fri, 22 Feb 2019 20:37:16 +0100 Subject: [PATCH] ValueObject > BankAccount > represents bank account --- docs/Value-Objects.md | 35 ++++++++++++ src/ValueObject/BankAccount.php | 81 +++++++++++++++++++++++++++ tests/ValueObject/BankAccountTest.php | 71 +++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 src/ValueObject/BankAccount.php create mode 100644 tests/ValueObject/BankAccountTest.php diff --git a/docs/Value-Objects.md b/docs/Value-Objects.md index ce58bd0..68b2261 100644 --- a/docs/Value-Objects.md +++ b/docs/Value-Objects.md @@ -57,6 +57,41 @@ $address = new Address('New York', '00123', '4th Avenue', '10', '200'); $asString = (string)$address; // "4th Avenue 10/200, 00123, New York" ``` +### BankAccount + +##### Namespace + +`Meritoo\Common\ValueObject\BankAccount` + +##### Info + +Represents bank account. Contains properties: +1. `$bankName` - name of bank +2. $accountNumber` - number of bank's account + +##### New instance + +New instance can be created using constructor + +```php +new BankAccount('Bank of America', '1234567890') +``` + +##### Methods + +Has getters for each property `getBankName()` and `getAccountNumber()`. + +##### Conversion to string (the `__toString()` method) + +Instance of `BankAccount` may be represented as string that contains all non-empty properties separated by `, `. + +Example: + +```php +$bank = new BankAccount('Bank of America', '1234567890'); +$asString = (string)$bank; // "Bank of America, 1234567890" +``` + ### Version ##### Namespace diff --git a/src/ValueObject/BankAccount.php b/src/ValueObject/BankAccount.php new file mode 100644 index 0000000..0b2c83f --- /dev/null +++ b/src/ValueObject/BankAccount.php @@ -0,0 +1,81 @@ + + * @copyright Meritoo + */ +class BankAccount +{ + /** + * Name of bank + * + * @var string + */ + protected $bankName; + + /** + * Number of bank's account + * + * @var string + */ + protected $accountNumber; + + /** + * Class constructor + * + * @param string $bankName Name of bank + * @param string $accountNumber Number of bank's account + */ + public function __construct($bankName, $accountNumber) + { + $this->bankName = $bankName; + $this->accountNumber = $accountNumber; + } + + /** + * Returns representation of object as string + * + * @return string + */ + public function __toString() + { + $values = [ + $this->bankName, + $this->accountNumber, + ]; + + return Arrays::getNonEmptyValuesAsString($values); + } + + /** + * Returns name of bank + * + * @return string + */ + public function getBankName() + { + return $this->bankName; + } + + /** + * Returns number of bank's account + * + * @return string + */ + public function getAccountNumber() + { + return $this->accountNumber; + } +} diff --git a/tests/ValueObject/BankAccountTest.php b/tests/ValueObject/BankAccountTest.php new file mode 100644 index 0000000..9dc6ab4 --- /dev/null +++ b/tests/ValueObject/BankAccountTest.php @@ -0,0 +1,71 @@ + + * @copyright Meritoo + */ +class BankAccountTest extends BaseTestCase +{ + /** + * @var BankAccount + */ + private $emptyBankAccount; + + /** + * @var BankAccount + */ + private $bankAccount; + + public function testConstructor() + { + static::assertConstructorVisibilityAndArguments( + BankAccount::class, + OopVisibilityType::IS_PUBLIC, + 2, + 2 + ); + } + + public function testGetAccountNumber() + { + self::assertSame('', $this->emptyBankAccount->getAccountNumber()); + self::assertSame('1234567890', $this->bankAccount->getAccountNumber()); + } + + public function testGetBankName() + { + self::assertSame('', $this->emptyBankAccount->getBankName()); + self::assertSame('Bank of America', $this->bankAccount->getBankName()); + } + + public function testToString() + { + static::assertSame('', (string)$this->emptyBankAccount); + static::assertSame('Bank of America, 1234567890', (string)$this->bankAccount); + } + + /** + * {@inheritdoc} + */ + protected function setUp() + { + parent::setUp(); + + $this->emptyBankAccount = new BankAccount('', ''); + $this->bankAccount = new BankAccount('Bank of America', '1234567890'); + } +}