Skip to content

Commit 6e7feaa

Browse files
committed
#2 - Added name value object and unittesting
1 parent fbade7c commit 6e7feaa

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

src/PhpValueObject/Person/Name.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <[email protected]>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace PhpValueObject\Person;
9+
10+
use PhpValueObject\ValueObject;
11+
12+
final class Name implements ValueObject
13+
{
14+
/**
15+
* @var string
16+
*/
17+
private $name;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $surnames;
23+
24+
/**
25+
* Name constructor.
26+
* @param string $name
27+
* @param string $surnames
28+
*/
29+
private function __construct(string $name, ?string $surnames)
30+
{
31+
$this->name = $this->cleaStringFromSpecialChars($name);
32+
if (null !== $surnames) {
33+
$this->surnames = $this->cleaStringFromSpecialChars($surnames);
34+
}
35+
}
36+
37+
/**
38+
* Named constructor
39+
*
40+
* @param string $name
41+
* @param null|string $surnames
42+
* @return static
43+
*/
44+
public static function build(string $name, ?string $surnames = null)
45+
{
46+
return new static($name, $surnames);
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
public function name(): string
53+
{
54+
return $this->name;
55+
}
56+
57+
/**
58+
* @return string|null
59+
*/
60+
public function surnames(): ?string
61+
{
62+
return $this->surnames;
63+
}
64+
65+
/**
66+
* Compare a value object with another one.
67+
*
68+
* @param Name|ValueObject $valueObjectToCompare
69+
* @return bool
70+
*/
71+
public function equals(ValueObject $valueObjectToCompare): bool
72+
{
73+
return ($this->name() === $valueObjectToCompare->name() &&
74+
$this->surnames() === $valueObjectToCompare->surnames());
75+
}
76+
77+
/**
78+
* Clean string from special chars
79+
*
80+
* @param string $stringToClean
81+
* @return string
82+
*/
83+
private function cleaStringFromSpecialChars(string $stringToClean): string
84+
{
85+
return preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/', '', $stringToClean);
86+
}
87+
}

tests/Person/NameTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <[email protected]>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace Test\Person;
9+
10+
use PHPUnit\Framework\TestCase;
11+
use PhpValueObject\Person\Name;
12+
13+
class NameTest extends TestCase
14+
{
15+
16+
public function testSpecialCharsAreRemoved()
17+
{
18+
$name = 'H00ola´ç`´´Q,.<Ase';
19+
$lastname = 'To´`me¿¿¿¡¡¡';
20+
21+
$nameInstance = Name::build($name, $lastname);
22+
$this->assertEquals('H00olaQ.Ase', $nameInstance->name());
23+
$this->assertEquals('Tome', $nameInstance->surnames());
24+
}
25+
26+
/**
27+
* @dataProvider names()
28+
* @param Name $name
29+
* @param Name $nameToCompare
30+
* @param bool $isEquals
31+
*/
32+
public function testEqualsMethod(Name $name, Name $nameToCompare, bool $isEquals)
33+
{
34+
$this->assertEquals($name->equals($nameToCompare), $isEquals);
35+
}
36+
37+
/**
38+
* @return array
39+
*/
40+
public function names(): array
41+
{
42+
return [
43+
[Name::build('Daniel'), Name::build('Daniel'), true],
44+
[Name::build('Daniel'), Name::build('Daniel', 'Tome'), false]
45+
];
46+
}
47+
}

0 commit comments

Comments
 (0)