-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1d5949
commit 7ecb1a5
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
spec/EricksonReyes/DomainDrivenDesign/Common/Factory/PhoneFactorySpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace spec\EricksonReyes\DomainDrivenDesign\Common\Factory; | ||
|
||
use EricksonReyes\DomainDrivenDesign\Common\Factory\PhoneFactory; | ||
use EricksonReyes\DomainDrivenDesign\Common\ValueObject\PhoneNumber; | ||
use PhpSpec\ObjectBehavior; | ||
use spec\EricksonReyes\DomainDrivenDesign\Common\UnitTestTrait; | ||
|
||
class PhoneFactorySpec extends ObjectBehavior | ||
{ | ||
use UnitTestTrait; | ||
|
||
public function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(PhoneFactory::class); | ||
} | ||
|
||
|
||
public function it_can_create_basic_phone_numbers() | ||
{ | ||
$expectedPhoneNumber = (int)$this->seeder->numberBetween(10000, 9999999); | ||
$this::create($expectedPhoneNumber)->shouldHaveType(PhoneNumber::class); | ||
} | ||
|
||
public function it_can_create_fully_formatted_phone_numbers() | ||
{ | ||
$expectedAreaCode = (int)$this->seeder->numberBetween(0, 1); | ||
$expectedPhoneNumber = (int)$this->seeder->numberBetween(10000, 9999999); | ||
$expectedCountryCode = (int)$this->seeder->numberBetween(0, 1); | ||
|
||
$this::create( | ||
$expectedPhoneNumber, | ||
$expectedCountryCode, | ||
$expectedAreaCode | ||
)->shouldHaveType(PhoneNumber::class); | ||
|
||
$this::create( | ||
$expectedPhoneNumber, | ||
$expectedCountryCode, | ||
null | ||
)->shouldHaveType(PhoneNumber::class); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/EricksonReyes/DomainDrivenDesign/Common/Factory/PhoneFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace EricksonReyes\DomainDrivenDesign\Common\Factory; | ||
|
||
use EricksonReyes\DomainDrivenDesign\Common\ValueObject\PhoneNumber; | ||
|
||
class PhoneFactory | ||
{ | ||
/** | ||
* @param int $phoneNumber | ||
* @param int|null $countryIso2Code | ||
* @param int|null $areaCode | ||
* @return PhoneNumber | ||
*/ | ||
public function create(int $phoneNumber, ?int $countryIso2Code = 0, ?int $areaCode = 0): PhoneNumber | ||
{ | ||
if ( | ||
$countryIso2Code !== null && $countryIso2Code > 0 && | ||
$areaCode !== null && $areaCode > 0 | ||
) { | ||
return PhoneNumber::createWithCountryAndAreaCode( | ||
$countryIso2Code, | ||
$areaCode ?? 0, | ||
$phoneNumber | ||
); | ||
} | ||
return new PhoneNumber($phoneNumber); | ||
} | ||
} |