From 0c7e27b8849c36ee29965cb62a70086cba4fefdc Mon Sep 17 00:00:00 2001 From: Meritoo Date: Fri, 22 Feb 2019 20:35:43 +0100 Subject: [PATCH] ValueObject > Address > represents address of company, institution, user etc. --- docs/Value-Objects.md | 55 ++++++++++- src/ValueObject/Address.php | 159 ++++++++++++++++++++++++++++++ tests/ValueObject/AddressTest.php | 105 ++++++++++++++++++++ 3 files changed, 317 insertions(+), 2 deletions(-) create mode 100644 src/ValueObject/Address.php create mode 100644 tests/ValueObject/AddressTest.php diff --git a/docs/Value-Objects.md b/docs/Value-Objects.md index 8497655..ce58bd0 100644 --- a/docs/Value-Objects.md +++ b/docs/Value-Objects.md @@ -4,7 +4,58 @@ Common and useful classes, methods, exceptions etc. # 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 @@ -14,7 +65,7 @@ Located in `Meritoo\Common\ValueObject` namespace. ##### Info -Represents version of software. Contains 3 properties: +Represents version of software. Contains properties: 1. `$majorPart` - the "major" part of version 2. `$minorPart` - the "minor" part of version 3. `$patchPart` - the "patch" part of version diff --git a/src/ValueObject/Address.php b/src/ValueObject/Address.php new file mode 100644 index 0000000..337f7ca --- /dev/null +++ b/src/ValueObject/Address.php @@ -0,0 +1,159 @@ + + * @copyright Meritoo + */ +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; + } +} diff --git a/tests/ValueObject/AddressTest.php b/tests/ValueObject/AddressTest.php new file mode 100644 index 0000000..e07db25 --- /dev/null +++ b/tests/ValueObject/AddressTest.php @@ -0,0 +1,105 @@ + + * @copyright Meritoo + */ +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'); + } +}