Skip to content

Commit

Permalink
Merge pull request #12 from michael-rubel/feat/squish-for-phone
Browse files Browse the repository at this point in the history
Trim value in `Phone`
  • Loading branch information
michael-rubel authored Nov 2, 2022
2 parents 701096f + 1c85a3c commit f61da3a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
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

0 comments on commit f61da3a

Please sign in to comment.