Skip to content

Commit 54e6e7b

Browse files
Merge pull request #5 from michael-rubel/feat/name
Add `Name` object
2 parents 5e5ab0e + a852dce commit 54e6e7b

File tree

7 files changed

+181
-14
lines changed

7 files changed

+181
-14
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ composer require michael-rubel/laravel-value-objects
2929
- [`Text`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Primitive/Text.php)
3030
- [`ClassString`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/ClassString.php)
3131
- [`FullName`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/FullName.php)
32+
- [`Name`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Name.php)
3233
- [`TaxNumber`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/TaxNumber.php)
3334
- [`Uuid`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Uuid.php)
3435

@@ -127,6 +128,19 @@ $name->lastName(); // 'Otwell'
127128

128129
---
129130

131+
### Name
132+
```php
133+
$name = new Name(' Company name! ');
134+
$name = Name::make(' Company name! ');
135+
$name = Name::from(' Company name! ');
136+
137+
$name->value(); // 'Company name!'
138+
(string) $name; // 'Company name!'
139+
$name->toArray(); // ['Company name!']
140+
```
141+
142+
---
143+
130144
### TaxNumber
131145
```php
132146
$taxNumber = new TaxNumber('0123456789', 'PL');

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"require": {
1919
"php": "^8.0",
2020
"illuminate/support": "^9.7",
21-
"michael-rubel/laravel-formatters": "^6.1",
2221
"phpmath/bignumber": "^1.2",
22+
"michael-rubel/laravel-formatters": "^6.2",
2323
"spatie/laravel-package-tools": "^1.12"
2424
},
2525
"require-dev": {

src/Collection/Complex/FullName.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace MichaelRubel\ValueObjects\Collection\Complex;
1414

1515
use Illuminate\Support\Collection;
16+
use Illuminate\Support\Stringable;
1617
use InvalidArgumentException;
1718
use MichaelRubel\Formatters\Collection\FullNameFormatter;
1819
use MichaelRubel\ValueObjects\ValueObject;
@@ -25,9 +26,9 @@
2526
* @template TKey of array-key
2627
* @template TValue
2728
*
28-
* @method static static make(string $value)
29-
* @method static static from(string $value)
30-
* @method static static makeOrNull(string $value)
29+
* @method static static make(string|Stringable $value)
30+
* @method static static from(string|Stringable $value)
31+
* @method static static makeOrNull(string|Stringable $value)
3132
*
3233
* @extends ValueObject<TKey, TValue>
3334
*/
@@ -41,12 +42,12 @@ class FullName extends ValueObject
4142
/**
4243
* Create a new instance of the value object.
4344
*
44-
* @param string $value
45+
* @param string|Stringable $value
4546
*/
46-
public function __construct(protected string $value)
47+
public function __construct(protected string|Stringable $value)
4748
{
4849
$this->split();
49-
$this->format();
50+
$this->sanitize();
5051
$this->validate();
5152
}
5253

@@ -57,7 +58,7 @@ public function __construct(protected string $value)
5758
*/
5859
public function fullName(): string
5960
{
60-
return $this->value;
61+
return $this->value();
6162
}
6263

6364
/**
@@ -87,7 +88,7 @@ public function lastName(): string
8788
*/
8889
public function value(): string
8990
{
90-
return $this->fullName();
91+
return (string) $this->value;
9192
}
9293

9394
/**
@@ -121,11 +122,11 @@ protected function validate(): void
121122
}
122123

123124
/**
124-
* Format the value.
125+
* Sanitize the value.
125126
*
126127
* @return void
127128
*/
128-
protected function format(): void
129+
protected function sanitize(): void
129130
{
130131
$this->value = format(FullNameFormatter::class, $this->value());
131132
}

src/Collection/Complex/Name.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects)
7+
*
8+
* @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository
9+
* @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/)
10+
* @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT
11+
*/
12+
13+
namespace MichaelRubel\ValueObjects\Collection\Complex;
14+
15+
use Illuminate\Support\Stringable;
16+
use MichaelRubel\ValueObjects\Collection\Primitive\Text;
17+
18+
/**
19+
* "Name" object presenting a generic name.
20+
*
21+
* @author Michael Rubél <michael@laravel.software>
22+
*
23+
* @template TKey of array-key
24+
* @template TValue
25+
*
26+
* @method static static make(string|Stringable $value)
27+
* @method static static from(string|Stringable $value)
28+
* @method static static makeOrNull(string|Stringable $value)
29+
*
30+
* @extends Text<TKey, TValue>
31+
*/
32+
class Name extends Text
33+
{
34+
/**
35+
* Create a new instance of the value object.
36+
*
37+
* @param string|Stringable $value
38+
*/
39+
public function __construct(protected string|Stringable $value)
40+
{
41+
parent::__construct($this->value);
42+
43+
$this->sanitize();
44+
}
45+
46+
/**
47+
* Sanitize the value.
48+
*
49+
* @return void
50+
*/
51+
protected function sanitize(): void
52+
{
53+
$this->value = str($this->value())
54+
->replaceMatches('/\p{C}+/u', '')
55+
->replace(['\r', '\n', '\t'], '')
56+
->squish()
57+
->value();
58+
}
59+
}

src/Collection/Complex/TaxNumber.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct(
4343
protected ?string $prefix = null,
4444
) {
4545
$this->validate();
46-
$this->format();
46+
$this->sanitize();
4747

4848
if ($this->canSplit()) {
4949
$this->split();
@@ -131,11 +131,11 @@ protected function validate(): void
131131
}
132132

133133
/**
134-
* Format the value.
134+
* Sanitize the value.
135135
*
136136
* @return void
137137
*/
138-
protected function format(): void
138+
protected function sanitize(): void
139139
{
140140
$this->number = format(TaxNumberFormatter::class, $this->taxNumber(), $this->prefix());
141141
}

tests/Unit/Complex/FullNameTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@
105105
$this->assertSame($valueObject->value(), (string) $valueObject);
106106
});
107107

108+
test('full name accepts stringable', function () {
109+
$valueObject = new FullName(str('Name Name'));
110+
$this->assertSame('Name Name', $valueObject->value());
111+
});
112+
108113
test('full name fails when passed only first name', function () {
109114
$this->expectException(\InvalidArgumentException::class);
110115

tests/Unit/Complex/NameTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Olsza\ValueObjects\Tests\Feature\ValueObjects;
6+
7+
use MichaelRubel\ValueObjects\Collection\Complex\Name;
8+
9+
test('name replaces invisible characters', function () {
10+
$name = new Name(' Company Name ');
11+
$this->assertSame('Company Name', $name->value());
12+
13+
$name = new Name(' Name!');
14+
$this->assertSame('Name!', $name->value());
15+
16+
$name = new Name('Name@$ ');
17+
$this->assertSame('Name@$', $name->value());
18+
19+
$name = new Name('HOTEL GOŁĘBIEWSKI TADEUSZ GOŁĘBIEWSKI,\r\nTAGO PRZEDSIĘBIORSTWO PRZEMYSŁU CUKIERNICZEGO TADEUSZ GOŁĘBIEWSKI');
20+
$this->assertSame('HOTEL GOŁĘBIEWSKI TADEUSZ GOŁĘBIEWSKI,TAGO PRZEDSIĘBIORSTWO PRZEMYSŁU CUKIERNICZEGO TADEUSZ GOŁĘBIEWSKI', $name->value());
21+
22+
$name = new Name('HOTEL GOŁĘBIEWSKI TADEUSZ GOŁĘBIEWSKI,
23+
TAGO PRZEDSIĘBIORSTWO PRZEMYSŁU CUKIERNICZEGO TADEUSZ GOŁĘBIEWSKI');
24+
$this->assertSame('HOTEL GOŁĘBIEWSKI TADEUSZ GOŁĘBIEWSKI,TAGO PRZEDSIĘBIORSTWO PRZEMYSŁU CUKIERNICZEGO TADEUSZ GOŁĘBIEWSKI', $name->value());
25+
});
26+
27+
test('name cannot accept null', function () {
28+
$this->expectException(\TypeError::class);
29+
30+
new Name(null);
31+
});
32+
33+
test('name fails when no argument passed', function () {
34+
$this->expectException(\TypeError::class);
35+
36+
new Name;
37+
});
38+
39+
test('name fails when empty string passed', function () {
40+
$this->expectException(\InvalidArgumentException::class);
41+
42+
new Name('');
43+
});
44+
45+
test('name is makeable', function () {
46+
$valueObject = Name::make('1');
47+
$this->assertSame('1', $valueObject->value());
48+
49+
$valueObject = Name::from('1');
50+
$this->assertSame('1', $valueObject->value());
51+
});
52+
53+
test('name is macroable', function () {
54+
Name::macro('str', function () {
55+
return str($this->value());
56+
});
57+
58+
$valueObject = new Name('Lorem ipsum');
59+
60+
$this->assertTrue($valueObject->str()->is('Lorem ipsum'));
61+
});
62+
63+
test('text is conditionable', function () {
64+
$valueObject = new Name('1');
65+
$this->assertSame('1', $valueObject->when(true)->value());
66+
$this->assertSame($valueObject, $valueObject->when(false)->value());
67+
});
68+
69+
test('text is arrayable', function () {
70+
$array = (new Name('Lorem Ipsum is simply dummy text.'))->toArray();
71+
$this->assertSame(['Lorem Ipsum is simply dummy text.'], $array);
72+
});
73+
74+
test('text is stringable', function () {
75+
$valueObject = new Name('Lorem ipsum');
76+
$this->assertSame('Lorem ipsum', (string) $valueObject);
77+
});
78+
79+
test('text accepts stringable', function () {
80+
$valueObject = new Name(str('Lorem ipsum'));
81+
$this->assertSame('Lorem ipsum', $valueObject->value());
82+
});
83+
84+
test('text fails when empty stringable passed', function () {
85+
$this->expectException(\InvalidArgumentException::class);
86+
87+
new Name(str(''));
88+
});

0 commit comments

Comments
 (0)