Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ composer require michael-rubel/laravel-value-objects
- [`ClassString`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/ClassString.php)
- [`FullName`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/FullName.php)
- [`Name`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Name.php)
- [`Phone`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Phone.php)
- [`TaxNumber`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/TaxNumber.php)
- [`Uuid`](https://github.com/michael-rubel/laravel-value-objects/blob/main/src/Collection/Complex/Uuid.php)

Expand Down Expand Up @@ -140,6 +141,21 @@ $name->toArray(); // ['Company name!']

---

### Phone
```php
$phone = new Phone(' +38 000 000 00 00 ');
$phone = Phone::make(' +38 000 000 00 00 ');
$phone = Phone::from(' +38 000 000 00 00 ');

$phone->value(); // '+38 000 000 00 00'
(string) $phone; // '+38 000 000 00 00'
$phone->toArray(); // ['+38 000 000 00 00']

$phone->sanitized(); // '+380000000000'
```

---

### TaxNumber
```php
$taxNumber = new TaxNumber('0123456789', 'PL');
Expand Down
13 changes: 12 additions & 1 deletion src/Collection/Complex/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function __construct(protected string|Stringable $value)
parent::__construct($this->value);

$this->validate();
$this->trim();
}

/**
Expand All @@ -50,8 +51,8 @@ public function __construct(protected string|Stringable $value)
public function sanitized(): string
{
return str($this->value())
->replace(' ', '')
->replaceMatches('/\p{C}+/u', '')
->replace(' ', '')
->value();
}

Expand All @@ -66,4 +67,14 @@ protected function validate(): void
throw ValidationException::withMessages([__('Your phone number is invalid.')]);
}
}

/**
* Trim the value.
*
* @return void
*/
protected function trim(): void
{
$this->value = trim($this->value());
}
}
5 changes: 5 additions & 0 deletions tests/Unit/Complex/PhoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
$this->assertSame('+48000000000', $phone->value());
});

test('phone is squished', function () {
$phone = new Phone(' +38 000 000 00 00 ');
$this->assertSame('+38 000 000 00 00', $phone->value());
});

test('phone allows short version', function () {
$phone = new Phone('00000');
$this->assertSame('00000', $phone->value());
Expand Down