mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 01:31:45 +01:00
ValueObject > Company > represents a company
This commit is contained in:
@@ -92,6 +92,51 @@ $bank = new BankAccount('Bank of America', '1234567890');
|
||||
$asString = (string)$bank; // "Bank of America, 1234567890"
|
||||
```
|
||||
|
||||
### Company
|
||||
|
||||
##### Namespace
|
||||
|
||||
`Meritoo\Common\ValueObject\Company`
|
||||
|
||||
##### Info
|
||||
|
||||
Represents a company. Contains properties:
|
||||
1. `$name` - name of company
|
||||
2. `$address` - address of company
|
||||
3. `$bankAccount` - bank account of company
|
||||
|
||||
##### New instance
|
||||
|
||||
New instance can be created using constructor:
|
||||
|
||||
```php
|
||||
new Company(
|
||||
'Test 1',
|
||||
new Address('New York', '00123', '4th Avenue', '10', '200'),
|
||||
new BankAccount('Bank 1', '12345')
|
||||
);
|
||||
```
|
||||
|
||||
##### Methods
|
||||
|
||||
Has getters for each property `getName()`, `getAddress()` and `getBankAccount()`.
|
||||
|
||||
##### Conversion to string (the `__toString()` method)
|
||||
|
||||
Instance of `Company` may be represented as string that contains all non-empty properties separated by `, `.
|
||||
|
||||
Example:
|
||||
|
||||
```php
|
||||
$company = new Company(
|
||||
'Test 1',
|
||||
new Address('New York', '00123', '4th Avenue', '10', '200'),
|
||||
new BankAccount('Bank 1', '12345')
|
||||
);
|
||||
|
||||
$asString = (string)$company; // "Test 1, 4th Avenue 10/200, 00123, New York, Bank 1, 12345"
|
||||
```
|
||||
|
||||
### Version
|
||||
|
||||
##### Namespace
|
||||
|
||||
101
src/ValueObject/Company.php
Normal file
101
src/ValueObject/Company.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\ValueObject;
|
||||
|
||||
use Meritoo\Common\Utilities\Arrays;
|
||||
|
||||
/**
|
||||
* Company
|
||||
*
|
||||
* @author Meritoo <github@meritoo.pl>
|
||||
* @copyright Meritoo <http://www.meritoo.pl>
|
||||
*/
|
||||
class Company
|
||||
{
|
||||
/**
|
||||
* Name of company
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* Address of company
|
||||
*
|
||||
* @var Address
|
||||
*/
|
||||
protected $address;
|
||||
|
||||
/**
|
||||
* Bank account of company
|
||||
*
|
||||
* @var BankAccount
|
||||
*/
|
||||
protected $bankAccount;
|
||||
|
||||
/**
|
||||
* Class constructor
|
||||
*
|
||||
* @param string $name Name of company
|
||||
* @param Address $address Address of company
|
||||
* @param BankAccount|null $bankAccount (optional) Bank account of company
|
||||
*/
|
||||
public function __construct($name, Address $address, BankAccount $bankAccount = null)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->address = $address;
|
||||
$this->bankAccount = $bankAccount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns representation of object as string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$values = [
|
||||
$this->name,
|
||||
$this->address,
|
||||
$this->bankAccount,
|
||||
];
|
||||
|
||||
return Arrays::getNonEmptyValuesAsString($values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns name of company
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns address of company
|
||||
*
|
||||
* @return Address
|
||||
*/
|
||||
public function getAddress()
|
||||
{
|
||||
return $this->address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns bank account of company
|
||||
*
|
||||
* @return BankAccount|null
|
||||
*/
|
||||
public function getBankAccount()
|
||||
{
|
||||
return $this->bankAccount;
|
||||
}
|
||||
}
|
||||
98
tests/ValueObject/CompanyTest.php
Normal file
98
tests/ValueObject/CompanyTest.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Test\ValueObject;
|
||||
|
||||
use Meritoo\Common\Test\Base\BaseTestCase;
|
||||
use Meritoo\Common\Type\OopVisibilityType;
|
||||
use Meritoo\Common\ValueObject\Address;
|
||||
use Meritoo\Common\ValueObject\BankAccount;
|
||||
use Meritoo\Common\ValueObject\Company;
|
||||
|
||||
/**
|
||||
* Test case for the company
|
||||
*
|
||||
* @author Meritoo <github@meritoo.pl>
|
||||
* @copyright Meritoo <http://www.meritoo.pl>
|
||||
*/
|
||||
class CompanyTest extends BaseTestCase
|
||||
{
|
||||
/**
|
||||
* @var Company
|
||||
*/
|
||||
private $company;
|
||||
|
||||
/**
|
||||
* @var Company
|
||||
*/
|
||||
private $companyWithoutBankAccount;
|
||||
|
||||
public function testConstructor()
|
||||
{
|
||||
static::assertConstructorVisibilityAndArguments(
|
||||
Company::class,
|
||||
OopVisibilityType::IS_PUBLIC,
|
||||
3,
|
||||
2
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetName()
|
||||
{
|
||||
static::assertSame('Test 1', $this->company->getName());
|
||||
static::assertSame('Test 2', $this->companyWithoutBankAccount->getName());
|
||||
}
|
||||
|
||||
public function testGetAddress()
|
||||
{
|
||||
static::assertEquals(
|
||||
new Address('New York', '00123', '4th Avenue', '10', '200'),
|
||||
$this->company->getAddress()
|
||||
);
|
||||
|
||||
static::assertEquals(
|
||||
new Address('San Francisco', '00456', 'Green Street', '22'),
|
||||
$this->companyWithoutBankAccount->getAddress()
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetBankAccount()
|
||||
{
|
||||
static::assertEquals(
|
||||
new BankAccount('Bank 1', '12345'),
|
||||
$this->company->getBankAccount()
|
||||
);
|
||||
|
||||
static::assertNull($this->companyWithoutBankAccount->getBankAccount());
|
||||
}
|
||||
|
||||
public function testToString()
|
||||
{
|
||||
static::assertSame('Test 1, 4th Avenue 10/200, 00123, New York, Bank 1, 12345', (string)$this->company);
|
||||
static::assertSame('Test 2, Green Street 22, 00456, San Francisco', (string)$this->companyWithoutBankAccount);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->company = new Company(
|
||||
'Test 1',
|
||||
new Address('New York', '00123', '4th Avenue', '10', '200'),
|
||||
new BankAccount('Bank 1', '12345')
|
||||
);
|
||||
|
||||
$this->companyWithoutBankAccount = new Company(
|
||||
'Test 2',
|
||||
new Address('San Francisco', '00456', 'Green Street', '22')
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user