Skip to content

Commit 4c07305

Browse files
Merge pull request #9 from michael-rubel/v3
`v3` update
2 parents ae9d8a8 + 62bca24 commit 4c07305

21 files changed

+196
-366
lines changed

README.md

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ composer require michael-rubel/laravel-value-objects
2424
## Built-in value objects
2525

2626
- [`Boolean`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Primitive/Boolean.php)
27-
- [`Decimal`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Primitive/Decimal.php)
28-
- [`Integer`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Primitive/Integer.php)
27+
- [`Number`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Primitive/Number.php)
2928
- [`Text`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Primitive/Text.php)
3029
- [`ClassString`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/ClassString.php)
3130
- [`FullName`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/FullName.php)
@@ -54,28 +53,15 @@ $bool->toArray(); // ['true']
5453

5554
---
5655

57-
### Decimal
56+
### Number
5857
```php
59-
$decimal = new Decimal('10.20999', scale: 2);
60-
$decimal = Decimal::make('10.20999', scale: 2);
61-
$decimal = Decimal::from('10.20999', scale: 2);
58+
$number = new Number('10.20999', scale: 2);
59+
$number = Number::make('10.20999', scale: 2);
60+
$number = Number::from('10.20999', scale: 2);
6261

63-
$decimal->value(); // '10.20'
64-
(string) $decimal; // '10.20'
65-
$decimal->toArray(); // ['10.20']
66-
```
67-
68-
---
69-
70-
### Integer
71-
```php
72-
$integer = new Integer(10);
73-
$integer = Integer::make(10);
74-
$integer = Integer::from(10);
75-
76-
$integer->value(); // 10
77-
(string) $integer; // '10'
78-
$integer->toArray(); // [10]
62+
$number->value(); // '10.20'
63+
(string) $number; // '10.20'
64+
$number->toArray(); // ['10.20']
7965
```
8066

8167
---

src/Artisan/stubs/value-object.stub

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ declare(strict_types=1);
44

55
namespace {{ namespace }};
66

7+
use Illuminate\Validation\ValidationException;
78
use MichaelRubel\ValueObjects\ValueObject;
89

910
/**
@@ -65,5 +66,9 @@ class {{ class }} extends ValueObject
6566
protected function validate(): void
6667
{
6768
// TODO: Implement validate() method.
69+
70+
if (empty($this->value())) {
71+
throw ValidationException::withMessages([__('Value of {{ class }} cannot be empty.')]);
72+
}
6873
}
6974
}

src/Collection/Complex/ClassString.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace MichaelRubel\ValueObjects\Collection\Complex;
1414

15-
use InvalidArgumentException;
15+
use Illuminate\Validation\ValidationException;
1616
use MichaelRubel\ValueObjects\ValueObject;
1717

1818
/**
@@ -103,7 +103,7 @@ public function value(): string
103103
protected function validate(): void
104104
{
105105
if (empty($this->value())) {
106-
throw new InvalidArgumentException('Class string cannot be empty.');
106+
throw ValidationException::withMessages([__('Class string cannot be empty.')]);
107107
}
108108
}
109109
}

src/Collection/Complex/FullName.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use Illuminate\Support\Collection;
1616
use Illuminate\Support\Stringable;
17-
use InvalidArgumentException;
17+
use Illuminate\Validation\ValidationException;
1818
use MichaelRubel\Formatters\Collection\FullNameFormatter;
1919
use MichaelRubel\ValueObjects\ValueObject;
2020

@@ -113,11 +113,11 @@ public function toArray(): array
113113
protected function validate(): void
114114
{
115115
if (empty($this->value())) {
116-
throw new InvalidArgumentException('Full name cannot be empty.');
116+
throw ValidationException::withMessages([__('Full name cannot be empty.')]);
117117
}
118118

119119
if (count($this->split) < 2) {
120-
throw new InvalidArgumentException('Full name should have a first name and last name.');
120+
throw ValidationException::withMessages([__('Full name should have a first name and last name.')]);
121121
}
122122
}
123123

src/Collection/Complex/TaxNumber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace MichaelRubel\ValueObjects\Collection\Complex;
1414

15-
use InvalidArgumentException;
15+
use Illuminate\Validation\ValidationException;
1616
use MichaelRubel\Formatters\Collection\TaxNumberFormatter;
1717
use MichaelRubel\ValueObjects\ValueObject;
1818

@@ -126,7 +126,7 @@ public function toArray(): array
126126
protected function validate(): void
127127
{
128128
if (empty($this->value())) {
129-
throw new InvalidArgumentException('Tax number cannot be empty.');
129+
throw ValidationException::withMessages([__('Tax number cannot be empty.')]);
130130
}
131131
}
132132

src/Collection/Complex/Uuid.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace MichaelRubel\ValueObjects\Collection\Complex;
1414

15-
use InvalidArgumentException;
15+
use Illuminate\Validation\ValidationException;
1616
use MichaelRubel\ValueObjects\ValueObject;
1717

1818
/**
@@ -95,7 +95,7 @@ public function toArray(): array
9595
protected function validate(): void
9696
{
9797
if (! str($this->value())->isUuid()) {
98-
throw new InvalidArgumentException('UUID is invalid.');
98+
throw ValidationException::withMessages([__('UUID is invalid.')]);
9999
}
100100
}
101101
}

src/Collection/Primitive/Integer.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/Collection/Primitive/Decimal.php renamed to src/Collection/Primitive/Number.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use PHP\Math\BigNumber\BigNumber;
1818

1919
/**
20-
* "Decimal" object presenting decimal values.
20+
* "Number" object that represents numeric values.
2121
*
2222
* @author Michael Rubél <michael@laravel.software>
2323
*
@@ -30,14 +30,14 @@
3030
*
3131
* @extends ValueObject<TKey, TValue>
3232
*/
33-
class Decimal extends ValueObject
33+
class Number extends ValueObject
3434
{
3535
use SanitizesNumbers;
3636

3737
/**
3838
* @var BigNumber
3939
*/
40-
protected BigNumber $number;
40+
protected BigNumber $bigNumber;
4141

4242
/**
4343
* Create a new instance of the value object.
@@ -47,7 +47,7 @@ class Decimal extends ValueObject
4747
*/
4848
public function __construct(int|string $number, protected int $scale = 2)
4949
{
50-
$this->number = new BigNumber($this->sanitize($number), $this->scale);
50+
$this->bigNumber = new BigNumber($this->sanitize($number), $this->scale);
5151
}
5252

5353
/**
@@ -57,6 +57,16 @@ public function __construct(int|string $number, protected int $scale = 2)
5757
*/
5858
public function value(): string
5959
{
60-
return (string) $this->number;
60+
return $this->bigNumber->getValue();
61+
}
62+
63+
/**
64+
* Get the number as an integer.
65+
*
66+
* @return int
67+
*/
68+
public function asInteger(): int
69+
{
70+
return (int) $this->bigNumber->getValue();
6171
}
6272
}

tests/Unit/Complex/ClassStringTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
declare(strict_types=1);
44

55
use Illuminate\Contracts\Container\BindingResolutionException;
6+
use Illuminate\Validation\ValidationException;
67
use MichaelRubel\ValueObjects\Collection\Complex\ClassString;
78
use MichaelRubel\ValueObjects\Tests\TestCase;
89

910
test('class string cannot be empty string', function () {
10-
$this->expectException(\InvalidArgumentException::class);
11+
$this->expectException(ValidationException::class);
1112

1213
new ClassString('');
1314
});
@@ -98,3 +99,10 @@
9899
$valueObject = new ClassString('Throwable');
99100
$this->assertSame($valueObject->value(), (string) $valueObject);
100101
});
102+
103+
test('class string is immutable', function () {
104+
$this->expectException(\InvalidArgumentException::class);
105+
$valueObject = new ClassString('\Exception');
106+
$this->assertSame('\Exception', $valueObject->string);
107+
$valueObject->classString = 'immutable';
108+
});

tests/Unit/Complex/EmailTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,10 @@
8585

8686
new Email(str(''));
8787
});
88+
89+
test('email is immutable', function () {
90+
$this->expectException(\InvalidArgumentException::class);
91+
$valueObject = new Email('contact@observer.name');
92+
$this->assertSame('contact@observer.name', $valueObject->value);
93+
$valueObject->value = 'immutable';
94+
});

0 commit comments

Comments
 (0)