diff --git a/Form/DataTransformer/PhoneNumberToArrayTransformer.php b/Form/DataTransformer/PhoneNumberToArrayTransformer.php index 81b2cc7b..73c69cad 100644 --- a/Form/DataTransformer/PhoneNumberToArrayTransformer.php +++ b/Form/DataTransformer/PhoneNumberToArrayTransformer.php @@ -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; } /** @@ -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), ); } diff --git a/Form/Type/PhoneNumberType.php b/Form/Type/PhoneNumberType.php index 2f01548c..9ef2ac72 100644 --- a/Form/Type/PhoneNumberType.php +++ b/Form/Type/PhoneNumberType.php @@ -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']) @@ -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, diff --git a/Tests/Form/DataTransformer/PhoneNumberToArrayTransformerTest.php b/Tests/Form/DataTransformer/PhoneNumberToArrayTransformerTest.php index 90d1681a..a3823af1 100644 --- a/Tests/Form/DataTransformer/PhoneNumberToArrayTransformerTest.php +++ b/Tests/Form/DataTransformer/PhoneNumberToArrayTransformerTest.php @@ -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 { @@ -71,8 +71,9 @@ 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() { @@ -80,30 +81,36 @@ public function transformProvider() 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, ), diff --git a/Tests/Form/Type/PhoneNumberTypeTest.php b/Tests/Form/Type/PhoneNumberTypeTest.php index 7a4394d3..bd4681c8 100644 --- a/Tests/Form/Type/PhoneNumberTypeTest.php +++ b/Tests/Form/Type/PhoneNumberTypeTest.php @@ -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' => '')),