From fe40d9caee49abe769de03ab8ceaf7d94c39b64e Mon Sep 17 00:00:00 2001 From: Meritoo Date: Mon, 4 Mar 2019 19:25:25 +0100 Subject: [PATCH] ValueObject > Human > represents a human --- CHANGELOG.md | 1 + docs/Value-Objects.md | 40 ++++++++ src/Traits/ValueObject/HumanTrait.php | 137 ++++++++++++++++++++++++++ src/ValueObject/Human.php | 22 +++++ tests/ValueObject/HumanTest.php | 116 ++++++++++++++++++++++ 5 files changed, 316 insertions(+) create mode 100644 src/Traits/ValueObject/HumanTrait.php create mode 100644 src/ValueObject/Human.php create mode 100644 tests/ValueObject/HumanTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index e2355e9..ba62097 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Common and useful classes, methods, exceptions etc. # 0.1.6 1. Arrays > refactoring & more tests +2. ValueObject > Human > represents a human # 0.1.5 diff --git a/docs/Value-Objects.md b/docs/Value-Objects.md index 171947f..e6a8c79 100644 --- a/docs/Value-Objects.md +++ b/docs/Value-Objects.md @@ -137,6 +137,46 @@ $company = new Company( $asString = (string)$company; // "Test 1, 4th Avenue 10/200, 00123, New York, Bank 1, 12345" ``` +### Human + +##### Namespace + +`Meritoo\Common\ValueObject\Human` + +##### Info + +Represents human. Based on `\Meritoo\Common\Traits\ValueObject\HumanTrait` trait. Contains properties same as `HumanTrait` trait: +1. `$firstName` - first name +2. `$lastName` - last name +3. `$email` - email address +4. `$birthDate` - birth date + +##### New instance + +New instance can be created using constructor: + +```php +new Human('John', 'Scott', 'john@scott.com', new \DateTime('2001-01-01')); +``` + +##### Methods + +Has getters for each property, e.g. `getFirstName()`, `getEmail()` etc. + +##### Conversion to string (the `__toString()` method) + +Instance of `Human` may be represented as string that contains first name, last name and email address (if provided). + +Example: + +```php +$human1 = new Human('John', 'Scott'); +$asString1 = (string)$human1; // "John Scott" + +$human2 = new Human('John', 'Scott', 'john@scott.com', new \DateTime('2001-01-01')); +$asString2 = (string)$human2; // "John Scott " +``` + ### Version ##### Namespace diff --git a/src/Traits/ValueObject/HumanTrait.php b/src/Traits/ValueObject/HumanTrait.php new file mode 100644 index 0000000..b8b7cd8 --- /dev/null +++ b/src/Traits/ValueObject/HumanTrait.php @@ -0,0 +1,137 @@ + + * @copyright Meritoo + */ +trait HumanTrait +{ + /** + * First name + * + * @var string + */ + protected $firstName; + + /** + * Last name + * + * @var string + */ + protected $lastName; + + /** + * Email address + * + * @var string + */ + protected $email; + + /** + * Birth date + * + * @var \DateTime + */ + protected $birthDate; + + /** + * Class constructor + * + * @param string $firstName First name + * @param string $lastName Last name + * @param string $email (optional) Email address + * @param \DateTime $birthDate (optional) Birth date + */ + public function __construct($firstName, $lastName, $email = null, \DateTime $birthDate = null) + { + $this->firstName = $firstName; + $this->lastName = $lastName; + $this->email = $email; + $this->birthDate = $birthDate; + } + + /** + * Returns representation of object as string + * + * @return string + */ + public function __toString() + { + $template = '%s'; + + if ('' !== $this->email && null !== $this->email) { + $template .= ' <%s>'; + } + + return sprintf($template, $this->getFullName(), $this->email); + } + + /** + * Returns first name + * + * @return string + */ + public function getFirstName() + { + return $this->firstName; + } + + /** + * Returns last name + * + * @return string + */ + public function getLastName() + { + return $this->lastName; + } + + /** + * Returns email address + * + * @return string|null + */ + public function getEmail() + { + return $this->email; + } + + /** + * Returns birth date + * + * @return \DateTime|null + */ + public function getBirthDate() + { + return $this->birthDate; + } + + /** + * Returns the full name + * + * @param bool $firstNameFirst (optional) If is set to true, first name is the first part. Otherwise - last name. + * @return string + */ + public function getFullName($firstNameFirst = true) + { + $beginning = $this->lastName; + $finish = $this->firstName; + + if ($firstNameFirst) { + $beginning = $this->firstName; + $finish = $this->lastName; + } + + return trim(sprintf('%s %s', $beginning, $finish)); + } +} diff --git a/src/ValueObject/Human.php b/src/ValueObject/Human.php new file mode 100644 index 0000000..ad21c06 --- /dev/null +++ b/src/ValueObject/Human.php @@ -0,0 +1,22 @@ + + * @copyright Meritoo + */ +class Human +{ + use HumanTrait; +} diff --git a/tests/ValueObject/HumanTest.php b/tests/ValueObject/HumanTest.php new file mode 100644 index 0000000..0480b80 --- /dev/null +++ b/tests/ValueObject/HumanTest.php @@ -0,0 +1,116 @@ + + * @copyright Meritoo + */ +class HumanTest extends BaseTestCase +{ + public function testConstructor() + { + static::assertConstructorVisibilityAndArguments( + Human::class, + OopVisibilityType::IS_PUBLIC, + 4, + 2 + ); + } + + /** + * @param string $description Description of test + * @param Human $human Human to verify + * @param string $expected Expected string + * + * @dataProvider provideHuman + */ + public function testToString($description, Human $human, $expected) + { + static::assertSame($expected, (string)$human, $description); + } + + public function testGetFirstName() + { + $empty = new Human('', ''); + static::assertSame('', $empty->getFirstName()); + + $human = new Human('John', 'Scott'); + static::assertSame('John', $human->getFirstName()); + } + + public function testGetLastName() + { + $empty = new Human('', ''); + static::assertSame('', $empty->getLastName()); + + $human = new Human('John', 'Scott'); + static::assertSame('Scott', $human->getLastName()); + } + + public function testGetBirthDate() + { + $empty = new Human('', ''); + static::assertNull($empty->getBirthDate()); + + $human = new Human('John', 'Scott', '', new \DateTime('2001-01-01')); + static::assertEquals(new \DateTime('2001-01-01'), $human->getBirthDate()); + } + + public function testGetFullName() + { + $empty = new Human('', ''); + static::assertSame('', $empty->getFullName()); + + $human = new Human('John', 'Scott', '', new \DateTime('2001-01-01')); + static::assertSame('John Scott', $human->getFullName()); + } + + public function testGetEmail() + { + $empty = new Human('', ''); + static::assertNull($empty->getEmail()); + + $human = new Human('John', 'Scott', 'john@scott.com'); + static::assertSame('john@scott.com', $human->getEmail()); + } + + public function provideHuman() + { + yield[ + 'Without any data (an empty human)', + new Human('', ''), + '', + ]; + + yield[ + 'With first and last name only', + new Human('John', 'Scott'), + 'John Scott', + ]; + + yield[ + 'With first name, last name and email', + new Human('John', 'Scott', 'john@scott.com'), + 'John Scott ', + ]; + + yield[ + 'With whole/complete data', + new Human('John', 'Scott', 'john@scott.com', new \DateTime('2001-01-01')), + 'John Scott ', + ]; + } +}