mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 01:31:45 +01:00
ValueObject > Address > represents address of company, institution, user etc.
This commit is contained in:
@@ -4,7 +4,58 @@ Common and useful classes, methods, exceptions etc.
|
|||||||
|
|
||||||
# Value Objects
|
# Value Objects
|
||||||
|
|
||||||
Located in `Meritoo\Common\ValueObject` namespace.
|
Located in `Meritoo\Common\ValueObject` namespace and in `src/ValueObject/` directory.
|
||||||
|
|
||||||
|
### Address
|
||||||
|
|
||||||
|
##### Namespace
|
||||||
|
|
||||||
|
`Meritoo\Common\ValueObject\Address`
|
||||||
|
|
||||||
|
##### Info
|
||||||
|
|
||||||
|
Represents address of company, institution, user etc. Contains properties:
|
||||||
|
1. `$street` - the street
|
||||||
|
2. `$buildingNumber` - the number of building
|
||||||
|
3. `$flatNumber` - the number of flat
|
||||||
|
4. `$zipCode` - the zip code
|
||||||
|
5. `$city` - the city, location
|
||||||
|
|
||||||
|
##### New instance
|
||||||
|
|
||||||
|
New instance can be created using constructor
|
||||||
|
|
||||||
|
```php
|
||||||
|
new Address('New York', '00123', '4th Avenue', '10', '200');
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Methods
|
||||||
|
|
||||||
|
Has getters for each property, e.g. `getFlatNumber()` or `getZipCode()`, and 1 extra method:
|
||||||
|
|
||||||
|
```php
|
||||||
|
getFullStreet()
|
||||||
|
```
|
||||||
|
|
||||||
|
that returns name of street with related numbers (building & flat number).
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```php
|
||||||
|
$address = new Address('New York', '00123', '4th Avenue', '10', '200');
|
||||||
|
$fullStreet = $address->getFullStreet(); // "4th Avenue 10/200"
|
||||||
|
```
|
||||||
|
|
||||||
|
##### Conversion to string (the `__toString()` method)
|
||||||
|
|
||||||
|
Instance of `Address` may be represented as string that contains all non-empty properties separated by `, `.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```php
|
||||||
|
$address = new Address('New York', '00123', '4th Avenue', '10', '200');
|
||||||
|
$asString = (string)$address; // "4th Avenue 10/200, 00123, New York"
|
||||||
|
```
|
||||||
|
|
||||||
### Version
|
### Version
|
||||||
|
|
||||||
@@ -14,7 +65,7 @@ Located in `Meritoo\Common\ValueObject` namespace.
|
|||||||
|
|
||||||
##### Info
|
##### Info
|
||||||
|
|
||||||
Represents version of software. Contains 3 properties:
|
Represents version of software. Contains properties:
|
||||||
1. `$majorPart` - the "major" part of version
|
1. `$majorPart` - the "major" part of version
|
||||||
2. `$minorPart` - the "minor" part of version
|
2. `$minorPart` - the "minor" part of version
|
||||||
3. `$patchPart` - the "patch" part of version
|
3. `$patchPart` - the "patch" part of version
|
||||||
|
|||||||
159
src/ValueObject/Address.php
Normal file
159
src/ValueObject/Address.php
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Address
|
||||||
|
*
|
||||||
|
* @author Meritoo <github@meritoo.pl>
|
||||||
|
* @copyright Meritoo <http://www.meritoo.pl>
|
||||||
|
*/
|
||||||
|
class Address
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The street
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $street;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number of building
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $buildingNumber;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number of flat
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $flatNumber;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The zip code
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $zipCode;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The city, location
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $city;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class constructor
|
||||||
|
*
|
||||||
|
* @param string $city City, location
|
||||||
|
* @param string $zipCode The zip code
|
||||||
|
* @param string $street The street
|
||||||
|
* @param string $buildingNumber The number of building
|
||||||
|
* @param string $flatNumber (optional) The number of flat. Default: "".
|
||||||
|
*/
|
||||||
|
public function __construct($city, $zipCode, $street, $buildingNumber, $flatNumber = '')
|
||||||
|
{
|
||||||
|
$this->city = $city;
|
||||||
|
$this->zipCode = $zipCode;
|
||||||
|
$this->street = $street;
|
||||||
|
$this->buildingNumber = $buildingNumber;
|
||||||
|
$this->flatNumber = $flatNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns representation of object as string
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
$values = [
|
||||||
|
$this->getFullStreet(),
|
||||||
|
$this->zipCode,
|
||||||
|
$this->city,
|
||||||
|
];
|
||||||
|
|
||||||
|
return Arrays::getNonEmptyValuesAsString($values);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns street
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getStreet()
|
||||||
|
{
|
||||||
|
return $this->street;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns full street (name + building & flat number)
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getFullStreet()
|
||||||
|
{
|
||||||
|
if (empty($this->street)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
$numbers = $this->buildingNumber;
|
||||||
|
|
||||||
|
if (!empty($numbers) && !empty($this->flatNumber)) {
|
||||||
|
$numbers = sprintf('%s/%s', $numbers, $this->flatNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sprintf('%s %s', $this->street, $numbers);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns number of building
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getBuildingNumber()
|
||||||
|
{
|
||||||
|
return $this->buildingNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns number of flat
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getFlatNumber()
|
||||||
|
{
|
||||||
|
return $this->flatNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns zip code
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getZipCode()
|
||||||
|
{
|
||||||
|
return $this->zipCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns city, location
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getCity()
|
||||||
|
{
|
||||||
|
return $this->city;
|
||||||
|
}
|
||||||
|
}
|
||||||
105
tests/ValueObject/AddressTest.php
Normal file
105
tests/ValueObject/AddressTest.php
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
<?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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case for the address
|
||||||
|
*
|
||||||
|
* @author Meritoo <github@meritoo.pl>
|
||||||
|
* @copyright Meritoo <http://www.meritoo.pl>
|
||||||
|
*/
|
||||||
|
class AddressTest extends BaseTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Address
|
||||||
|
*/
|
||||||
|
private $address;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Address
|
||||||
|
*/
|
||||||
|
private $addressWithoutFlat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Address
|
||||||
|
*/
|
||||||
|
private $addressWithoutStreet;
|
||||||
|
|
||||||
|
public function testConstructor()
|
||||||
|
{
|
||||||
|
static::assertConstructorVisibilityAndArguments(
|
||||||
|
Address::class,
|
||||||
|
OopVisibilityType::IS_PUBLIC,
|
||||||
|
5,
|
||||||
|
4
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetFlatNumber()
|
||||||
|
{
|
||||||
|
static::assertSame('200', $this->address->getFlatNumber());
|
||||||
|
static::assertSame('', $this->addressWithoutFlat->getFlatNumber());
|
||||||
|
static::assertSame('300', $this->addressWithoutStreet->getFlatNumber());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetBuildingNumber()
|
||||||
|
{
|
||||||
|
static::assertSame('10', $this->address->getBuildingNumber());
|
||||||
|
static::assertSame('22', $this->addressWithoutFlat->getBuildingNumber());
|
||||||
|
static::assertSame('1', $this->addressWithoutStreet->getBuildingNumber());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetStreet()
|
||||||
|
{
|
||||||
|
static::assertSame('4th Avenue', $this->address->getStreet());
|
||||||
|
static::assertSame('Green Street', $this->addressWithoutFlat->getStreet());
|
||||||
|
static::assertSame('', $this->addressWithoutStreet->getStreet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetFullStreet()
|
||||||
|
{
|
||||||
|
static::assertSame('4th Avenue 10/200', $this->address->getFullStreet());
|
||||||
|
static::assertSame('Green Street 22', $this->addressWithoutFlat->getFullStreet());
|
||||||
|
static::assertSame('', $this->addressWithoutStreet->getFullStreet());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetCity()
|
||||||
|
{
|
||||||
|
static::assertSame('New York', $this->address->getCity());
|
||||||
|
static::assertSame('San Francisco', $this->addressWithoutFlat->getCity());
|
||||||
|
static::assertSame('Saint Louis', $this->addressWithoutStreet->getCity());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetZipCode()
|
||||||
|
{
|
||||||
|
static::assertSame('00123', $this->address->getZipCode());
|
||||||
|
static::assertSame('00456', $this->addressWithoutFlat->getZipCode());
|
||||||
|
static::assertSame('00111', $this->addressWithoutStreet->getZipCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testToString()
|
||||||
|
{
|
||||||
|
static::assertSame('4th Avenue 10/200, 00123, New York', (string)$this->address);
|
||||||
|
static::assertSame('Green Street 22, 00456, San Francisco', (string)$this->addressWithoutFlat);
|
||||||
|
static::assertSame('00111, Saint Louis', (string)$this->addressWithoutStreet);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->address = new Address('New York', '00123', '4th Avenue', '10', '200');
|
||||||
|
$this->addressWithoutFlat = new Address('San Francisco', '00456', 'Green Street', '22');
|
||||||
|
$this->addressWithoutStreet = new Address('Saint Louis', '00111', '', '1', '300');
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user