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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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::NATIONAL
)
{
$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
8 changes: 6 additions & 2 deletions 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 Expand Up @@ -144,7 +144,11 @@ public function configureOptions(OptionsResolver $resolver)
return PhoneNumberType::WIDGET_SINGLE_TEXT !== $options['widget'];
},
'default_region' => PhoneNumberUtil::UNKNOWN_REGION,
'format' => PhoneNumberFormat::INTERNATIONAL,
'format' => function (Options $options) {
return PhoneNumberType::WIDGET_SINGLE_TEXT === $options['widget']
? PhoneNumberFormat::INTERNATIONAL
: PhoneNumberFormat::NATIONAL;
},
'invalid_message' => 'This value is not a valid phone number.',
'by_reference' => false,
'error_bubbling' => false,
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
4 changes: 4 additions & 0 deletions Tests/Form/Type/PhoneNumberTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ public function countryChoiceValuesProvider()
{
return array(
array(array('country' => 'GB', 'number' => '01234 567890'), array(), array('country' => 'GB', 'number' => '01234 567890')),
array(array('country' => 'GB', 'number' => '01234 567890'), array('format' => PhoneNumberFormat::NATIONAL), array('country' => 'GB', 'number' => '01234 567890')),
array(array('country' => 'GB', 'number' => '01234 567890'), array('format' => PhoneNumberFormat::INTERNATIONAL), array('country' => 'GB', 'number' => '+44 1234 567890')),
array(array('country' => 'GB', 'number' => '+44 1234 567890'), array(), array('country' => 'GB', 'number' => '01234 567890')),
array(array('country' => 'GB', 'number' => '+44 1234 567890'), array('format' => PhoneNumberFormat::INTERNATIONAL), array('country' => 'GB', 'number' => '+44 1234 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(), 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' => '', 'number' => ''), array(), array('country' => '', 'number' => '')),
Expand Down