Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

phone number format as option instead of hard set to NATIONAL #173

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
14 changes: 12 additions & 2 deletions Form/DataTransformer/PhoneNumberToArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ class PhoneNumberToArrayTransformer implements DataTransformerInterface
*/
private $countryChoices;

/**
* @var int
*/
private $format;

/**
* Constructor.
*
* @param array $countryChoices
* @param int $format
*/
public function __construct(array $countryChoices)
public function __construct(
array $countryChoices,
$format = PhoneNumberFormat::INTERNATIONAL
Copy link
Member

@robhogan robhogan Mar 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I wasn't clear before. The test cases were fine, they highlighted a breaking change. The problem is this line - the default option should be NATIONAL because that's what it was fixed to before.

Anything else is going to result in a change in behaviour for peoples' existing setups.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok i revert test changes and set default format to national, thank you @rh389

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you could add a few more tests to those cases to check the desired result with a couple of different formats, that'd be perfect. Thanks again!

)
{
$this->countryChoices = $countryChoices;
$this->format = $format;
}

/**
Expand All @@ -57,7 +67,7 @@ public function transform($phoneNumber)

return array(
'country' => $util->getRegionCodeForNumber($phoneNumber),
'number' => $util->format($phoneNumber, PhoneNumberFormat::NATIONAL),
'number' => $util->format($phoneNumber, $this->format),
);
}

Expand Down
2 changes: 1 addition & 1 deletion Form/Type/PhoneNumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$builder
->add('country', $choiceType, $countryOptions)
->add('number', $textType, $numberOptions)
->addViewTransformer(new PhoneNumberToArrayTransformer($transformerChoices));
->addViewTransformer(new PhoneNumberToArrayTransformer($transformerChoices, $options['format']));
} else {
$builder->addViewTransformer(
new PhoneNumberToStringTransformer($options['default_region'], $options['format'])
Expand Down
15 changes: 11 additions & 4 deletions Tests/Form/DataTransformer/PhoneNumberToArrayTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public function testConstructor()
/**
* @dataProvider transformProvider
*/
public function testTransform(array $countryChoices, $actual, $expected)
public function testTransform(array $countryChoices, $format, $actual, $expected)
{
$transformer = new PhoneNumberToArrayTransformer($countryChoices);
$transformer = new PhoneNumberToArrayTransformer($countryChoices, $format);

if (is_array($actual)) {
try {
Expand All @@ -71,39 +71,46 @@ public function testTransform(array $countryChoices, $actual, $expected)

/**
* 0 => Country choices
* 1 => Actual value
* 2 => Expected result
* 1 => Format
* 2 => Actual value
* 3 => Expected result
*/
public function transformProvider()
{
return array(
array(
array('GB'),
null,
null,
array('country' => '', 'number' => ''),
),
array(
array('GB'),
PhoneNumberFormat::NATIONAL,
array('country' => 'GB', 'number' => '01234567890'),
array('country' => 'GB', 'number' => '01234 567890'),
),
array(// Wrong country code, but matching country exists.
array('GB', 'JE'),
PhoneNumberFormat::NATIONAL,
array('country' => 'JE', 'number' => '01234567890'),
array('country' => 'GB', 'number' => '01234 567890'),
),
array(// Wrong country code, but matching country exists.
array('GB', 'JE'),
PhoneNumberFormat::NATIONAL,
array('country' => 'JE', 'number' => '+441234567890'),
array('country' => 'GB', 'number' => '01234 567890'),
),
array(// Country code not in list.
array('US'),
null,
array('country' => 'GB', 'number' => '01234567890'),
self::TRANSFORMATION_FAILED,
),
array(
array('US'),
null,
array('country' => 'GB', 'number' => 'foo'),
self::TRANSFORMATION_FAILED,
),
Expand Down
8 changes: 4 additions & 4 deletions Tests/Form/Type/PhoneNumberTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ public function testCountryChoiceValues($input, $options, $output)
public function countryChoiceValuesProvider()
{
return array(
array(array('country' => 'GB', 'number' => '01234 567890'), array(), array('country' => 'GB', 'number' => '01234 567890')),
array(array('country' => 'GB', 'number' => '+44 1234 567890'), array(), array('country' => 'GB', 'number' => '01234 567890')),
array(array('country' => 'GB', 'number' => '1234 567890'), array(), array('country' => 'GB', 'number' => '01234 567890')),
array(array('country' => 'GB', 'number' => '+1 650-253-0000'), array(), array('country' => 'US', 'number' => '(650) 253-0000')),
array(array('country' => 'GB', 'number' => '01234 567890'), array('format' => PhoneNumberFormat::NATIONAL), array('country' => 'GB', 'number' => '01234 567890')),
array(array('country' => 'GB', 'number' => '+44 1234 567890'), array('format' => PhoneNumberFormat::NATIONAL), array('country' => 'GB', 'number' => '01234 567890')),
array(array('country' => 'GB', 'number' => '1234 567890'), array('format' => PhoneNumberFormat::NATIONAL), array('country' => 'GB', 'number' => '01234 567890')),
array(array('country' => 'GB', 'number' => '+1 650-253-0000'), array('format' => PhoneNumberFormat::NATIONAL), array('country' => 'US', 'number' => '(650) 253-0000')),
array(array('country' => '', 'number' => ''), array(), array('country' => '', 'number' => '')),
);
}
Expand Down