Skip to content

Commit

Permalink
[Form] Changed value transformers to throw UnexpectedTypeException in…
Browse files Browse the repository at this point in the history
…stances
  • Loading branch information
Bernhard Schussek authored and fabpot committed Jan 3, 2011
1 parent 48af2fc commit acdd5c0
Show file tree
Hide file tree
Showing 22 changed files with 84 additions and 46 deletions.
6 changes: 3 additions & 3 deletions src/Symfony/Component/Form/ChoiceField.php
Expand Up @@ -11,7 +11,7 @@
* with this source code in the file LICENSE.
*/

use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Exception\InvalidOptionsException;

/**
* Lets the user select between different choices
Expand Down Expand Up @@ -39,11 +39,11 @@ protected function configure()
$this->addOption('empty_value', '');

if (!is_array($this->getOption('choices'))) {
throw new UnexpectedTypeException('The choices option must be an array');
throw new InvalidOptionsException('The choices option must be an array', array('choices'));
}

if (!is_array($this->getOption('preferred_choices'))) {
throw new UnexpectedTypeException('The preferred_choices option must be an array');
throw new InvalidOptionsException('The preferred_choices option must be an array', array('preferred_choices'));
}

if (count($this->getOption('preferred_choices')) > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/CollectionField.php
Expand Up @@ -60,7 +60,7 @@ protected function configure()
public function setData($collection)
{
if (!is_array($collection) && !$collection instanceof \Traversable) {
throw new UnexpectedTypeException('The data passed to the CollectionField must be an array or a Traversable');
throw new UnexpectedTypeException($collection, 'array or \Traversable');
}

foreach ($this as $name => $field) {
Expand Down
Expand Up @@ -13,4 +13,8 @@

class UnexpectedTypeException extends FormException
{
public function __construct($value, $expectedType)
{
parent::__construct(sprintf('Expected argument of type %s, %s given', $expectedType, gettype($value)));
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/FieldGroup.php
Expand Up @@ -281,7 +281,7 @@ public function bind($taintedData)
}

if (!is_array($taintedData)) {
throw new UnexpectedTypeException('You must pass an array parameter to the bind() method');
throw new UnexpectedTypeException($taintedData, 'array');
}

foreach ($this->fields as $key => $field) {
Expand Down
Expand Up @@ -11,6 +11,8 @@
* with this source code in the file LICENSE.
*/

use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* Transforms between a boolean and a string.
*
Expand All @@ -32,7 +34,7 @@ public function transform($value)
}

if (!is_bool($value)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type boolean but got %s.', gettype($value)));
throw new UnexpectedTypeException($value, 'boolean');
}

return true === $value ? '1' : '';
Expand All @@ -47,7 +49,7 @@ public function transform($value)
public function reverseTransform($value, $originalValue)
{
if (!is_string($value)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type string but got %s.', gettype($value)));
throw new UnexpectedTypeException($value, 'string');
}

return '' !== $value;
Expand Down
Expand Up @@ -11,7 +11,7 @@
* with this source code in the file LICENSE.
*/

use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* Transforms between a normalized time and a localized time string/array.
Expand Down Expand Up @@ -61,7 +61,7 @@ public function transform($dateTime)
}

if (!$dateTime instanceof \DateTime) {
throw new \InvalidArgumentException('Expected value of type \DateTime');
throw new UnexpectedTypeException($dateTime, '\DateTime');
}

$inputTimezone = $this->getOption('input_timezone');
Expand Down Expand Up @@ -106,7 +106,7 @@ public function reverseTransform($value, $originalValue)
$outputTimezone = $this->getOption('output_timezone');

if (!is_array($value)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type array, %s given', gettype($value)));
throw new UnexpectedTypeException($value, 'array');
}

if (implode('', $value) === '') {
Expand Down
Expand Up @@ -11,7 +11,7 @@
* with this source code in the file LICENSE.
*/

use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* Transforms between a normalized time and a localized time string
Expand Down Expand Up @@ -62,7 +62,7 @@ public function transform($dateTime)
}

if (!$dateTime instanceof \DateTime) {
throw new \InvalidArgumentException('Expected value of type \DateTime');
throw new UnexpectedTypeException($dateTime, '\DateTime');
}

$inputTimezone = $this->getOption('input_timezone');
Expand Down Expand Up @@ -92,7 +92,7 @@ public function reverseTransform($value, $originalValue)
$inputTimezone = $this->getOption('input_timezone');

if (!is_string($value)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type string, %s given', gettype($value)));
throw new UnexpectedTypeException($value, 'string');
}

if ('' === $value) {
Expand Down
Expand Up @@ -11,7 +11,7 @@
* with this source code in the file LICENSE.
*/

use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* Transforms between a date string and a DateTime object
Expand Down Expand Up @@ -47,7 +47,7 @@ public function transform($value)
}

if (!$value instanceof \DateTime) {
throw new \InvalidArgumentException('Expected value of type \DateTime');
throw new UnexpectedTypeException($value, '\DateTime');
}

$value->setTimezone(new \DateTimeZone($this->getOption('output_timezone')));
Expand All @@ -63,10 +63,14 @@ public function transform($value)
*/
public function reverseTransform($value, $originalValue)
{
if ('' === $value) {
if (empty($value)) {
return null;
}

if (!is_string($value)) {
throw new UnexpectedTypeException($value, 'string');
}

$outputTimezone = $this->getOption('output_timezone');
$inputTimezone = $this->getOption('input_timezone');

Expand Down
Expand Up @@ -11,7 +11,7 @@
* with this source code in the file LICENSE.
*/

use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* Transforms between a timestamp and a DateTime object
Expand Down Expand Up @@ -45,7 +45,7 @@ public function transform($value)
}

if (!$value instanceof \DateTime) {
throw new \InvalidArgumentException('Expected value of type \DateTime');
throw new UnexpectedTypeException($value, '\DateTime');
}

$value->setTimezone(new \DateTimeZone($this->getOption('output_timezone')));
Expand All @@ -65,6 +65,10 @@ public function reverseTransform($value, $originalValue)
return null;
}

if (!is_numeric($value)) {
throw new UnexpectedTypeException($value, 'numeric');
}

$outputTimezone = $this->getOption('output_timezone');
$inputTimezone = $this->getOption('input_timezone');

Expand Down
Expand Up @@ -11,7 +11,7 @@
* with this source code in the file LICENSE.
*/

use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* Transforms between a normalized format and a localized money string.
Expand Down Expand Up @@ -43,7 +43,7 @@ public function transform($value)
{
if (null !== $value) {
if (!is_numeric($value)) {
throw new \InvalidArgumentException(sprintf('Numeric argument expected, %s given', gettype($value)));
throw new UnexpectedTypeException($value, 'numeric');
}

$value /= $this->getOption('divisor');
Expand Down
Expand Up @@ -11,7 +11,7 @@
* with this source code in the file LICENSE.
*/

use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* Transforms between a number type and a localized number with grouping
Expand Down Expand Up @@ -55,7 +55,7 @@ public function transform($value)
}

if (!is_numeric($value)) {
throw new \InvalidArgumentException(sprintf('Numeric argument expected, %s given', gettype($value)));
throw new UnexpectedTypeException($value, 'numeric');
}

$formatter = $this->getNumberFormatter();
Expand All @@ -76,7 +76,7 @@ public function transform($value)
public function reverseTransform($value, $originalValue)
{
if (!is_string($value)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type string, %s given', gettype($value)));
throw new UnexpectedTypeException($value, 'string');
}

if ('' === $value) {
Expand Down
Expand Up @@ -11,7 +11,7 @@
* with this source code in the file LICENSE.
*/

use \Symfony\Component\Form\ValueTransformer\ValueTransformerException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;

/**
* Transforms between a normalized format (integer or float) and a percentage value.
Expand Down Expand Up @@ -57,7 +57,7 @@ public function transform($value)
}

if (!is_numeric($value)) {
throw new \InvalidArgumentException(sprintf('Numeric argument expected, %s given', gettype($value)));
throw new UnexpectedTypeException($value, 'numeric');
}

if (self::FRACTIONAL == $this->getOption('type')) {
Expand All @@ -84,7 +84,7 @@ public function transform($value)
public function reverseTransform($value, $originalValue)
{
if (!is_string($value)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type string, %s given', gettype($value)));
throw new UnexpectedTypeException($value, 'string');
}

if ('' === $value) {
Expand Down
Expand Up @@ -43,7 +43,7 @@ interface ValueTransformerInterface extends Localizable
*
* @param mixed $value The value in the original representation
* @return mixed The value in the transformed representation
* @throws InvalidArgumentException when the argument is no string
* @throws UnexpectedTypeException when the argument is no string
* @throws ValueTransformerException when the transformation fails
*/
function transform($value);
Expand All @@ -69,7 +69,7 @@ function transform($value);
* @param mixed $value The value in the transformed representation
* @param mixed $originalValue The original value from the datasource that is about to be overwritten by the new value.
* @return mixed The value in the original representation
* @throws InvalidArgumentException when the argument is not of the
* @throws UnexpectedTypeException when the argument is not of the
* expected type
* @throws ValueTransformerException when the transformation fails
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/Symfony/Tests/Component/Form/ChoiceFieldTest.php
Expand Up @@ -38,7 +38,7 @@ class ChoiceFieldTest extends \PHPUnit_Framework_TestCase
);

/**
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
* @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
*/
public function testConfigureChoicesWithNonArray()
{
Expand All @@ -48,7 +48,7 @@ public function testConfigureChoicesWithNonArray()
}

/**
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
* @expectedException Symfony\Component\Form\Exception\InvalidOptionsException
*/
public function testConfigurePreferredChoicesWithNonArray()
{
Expand Down
Expand Up @@ -22,14 +22,14 @@ public function testTransform()

public function testTransformExpectsBoolean()
{
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');

$this->transformer->transform('1');
}

public function testReverseTransformExpectsString()
{
$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');

$this->transformer->reverseTransform(1, null);
}
Expand Down
Expand Up @@ -115,7 +115,7 @@ public function testTransformRequiresDateTime()
{
$transformer = new DateTimeToArrayTransformer();

$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');

$transformer->reverseTransform('12345', null);
}
Expand Down Expand Up @@ -212,7 +212,7 @@ public function testReverseTransformRequiresArray()
{
$transformer = new DateTimeToArrayTransformer();

$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');

$transformer->reverseTransform('12345', null);
}
Expand Down
Expand Up @@ -155,7 +155,7 @@ public function testTransformRequiresValidDateTime()
{
$transformer = new DateTimeToLocalizedStringTransformer();

$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');

$transformer->transform('2010-01-01');
}
Expand Down Expand Up @@ -294,7 +294,7 @@ public function testReverseTransformRequiresString()
{
$transformer = new DateTimeToLocalizedStringTransformer();

$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');

$transformer->reverseTransform(12345, null);
}
Expand Down
Expand Up @@ -49,7 +49,8 @@ public function testTransformExpectsDateTime()
{
$transformer = new DateTimeToStringTransformer();

$this->setExpectedException('\InvalidArgumentException');
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');

$transformer->transform('1234');
}

Expand Down Expand Up @@ -87,11 +88,21 @@ public function testReverseTransform_differentTimezones()
$this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input, null));
}

public function testReverseTransformExpectsValidString()
public function testReverseTransformExpectsString()
{
$reverseTransformer = new DateTimeToStringTransformer();

$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');

$reverseTransformer->reverseTransform(1234, null);
}

public function testReverseTransformExpectsValidDateString()
{
$reverseTransformer = new DateTimeToStringTransformer();

$this->setExpectedException('\InvalidArgumentException');

$reverseTransformer->reverseTransform('2010-2010-2010', null);
}
}

0 comments on commit acdd5c0

Please sign in to comment.