Skip to content

Commit

Permalink
Add PhoneFactory class
Browse files Browse the repository at this point in the history
  • Loading branch information
ericksonreyes committed Jun 24, 2019
1 parent b1d5949 commit 7ecb1a5
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
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);
}
}
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);
}
}

0 comments on commit 7ecb1a5

Please sign in to comment.