Sfoglia il codice sorgente

[Form] Moved namespace FieldGuesser to Type\Guesser

Bernhard Schussek 14 anni fa
parent
commit
7f9284105d

+ 1 - 1
src/Symfony/Bundle/DoctrineBundle/Resources/config/orm.xml

@@ -41,7 +41,7 @@
         <parameter key="doctrine.orm.proxy_cache_warmer.class">Symfony\Bundle\DoctrineBundle\CacheWarmer\ProxyCacheWarmer</parameter>
         
         <!-- form field factory guesser -->
-        <parameter key="form.guesser.doctrine.class">Symfony\Component\Form\FieldGuesser\EntityFieldGuesser</parameter>
+        <parameter key="form.guesser.doctrine.class">Symfony\Component\Form\Type\Guesser\EntityTypeGuesser</parameter>
     </parameters>
 
     <services>

+ 2 - 2
src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml

@@ -7,7 +7,7 @@
     <parameters>
         <parameter key="form.factory.class">Symfony\Component\Form\FormFactory</parameter>
         <parameter key="form.type.loader.class">Symfony\Bundle\FrameworkBundle\Form\ContainerAwareTypeLoader</parameter>
-        <parameter key="form.guesser.validator.class">Symfony\Component\Form\FieldGuesser\ValidatorFieldGuesser</parameter>
+        <parameter key="form.guesser.validator.class">Symfony\Component\Form\Type\Guesser\ValidatorTypeGuesser</parameter>
         <parameter key="form.csrf_provider.class">Symfony\Component\Form\CsrfProvider\SessionCsrfProvider</parameter>
         <parameter key="form.theme.class">Symfony\Component\Form\Renderer\Theme\TwigTheme</parameter>
         <parameter key="form.theme.template">TwigBundle::div_layout.html.twig</parameter>
@@ -30,7 +30,7 @@
             <argument type="service" id="form.type.loader" />
         </service>
 
-        <!-- ValidatorFieldGuesser -->
+        <!-- ValidatorTypeGuesser -->
         <service id="form.guesser.validator" class="%form.guesser.validator.class%" public="false">
             <tag name="form.guesser" />
             <argument type="service" id="validator.mapping.class_metadata_factory" />

+ 4 - 4
src/Symfony/Component/Form/FormFactory.php

@@ -13,8 +13,8 @@ namespace Symfony\Component\Form;
 
 use Symfony\Component\Form\Type\FieldTypeInterface;
 use Symfony\Component\Form\Type\Loader\TypeLoaderInterface;
-use Symfony\Component\Form\FieldGuesser\FieldGuesserInterface;
-use Symfony\Component\Form\FieldGuesser\FieldGuess;
+use Symfony\Component\Form\Type\Guesser\TypeGuesserInterface;
+use Symfony\Component\Form\Type\Guesser\Guess;
 
 class FormFactory implements FormFactoryInterface
 {
@@ -27,7 +27,7 @@ class FormFactory implements FormFactoryInterface
         $this->typeLoader = $typeLoader;
     }
 
-    public function addGuesser(FieldGuesserInterface $guesser)
+    public function addGuesser(TypeGuesserInterface $guesser)
     {
         $this->guessers[] = $guesser;
     }
@@ -133,6 +133,6 @@ class FormFactory implements FormFactoryInterface
             }
         }
 
-        return FieldGuess::getBestGuess($guesses);
+        return Guess::getBestGuess($guesses);
     }
 }

+ 30 - 30
src/Symfony/Component/Form/FieldGuesser/EntityFieldGuesser.php

@@ -9,7 +9,7 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Component\Form\FieldGuesser;
+namespace Symfony\Component\Form\Type\Guesser;
 
 use Doctrine\ORM\EntityManager;
 
@@ -18,7 +18,7 @@ use Doctrine\ORM\EntityManager;
  *
  * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  */
-class EntityFieldGuesser implements FieldGuesserInterface
+class EntityTypeGuesser implements TypeGuesserInterface
 {
     /**
      * The Doctrine 2 entity manager
@@ -58,86 +58,86 @@ class EntityFieldGuesser implements FieldGuesserInterface
                 $multiple = $metadata->isCollectionValuedAssociation($property);
                 $mapping = $metadata->getAssociationMapping($property);
 
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'entity',
                     array(
                         'em' => $this->em,
                         'class' => $mapping['targetEntity'],
                         'multiple' => $multiple,
                     ),
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
             } else {
                 switch ($metadata->getTypeOfField($property))
                 {
         //            case 'array':
-        //                return new FieldTypeGuess(
+        //                return new TypeGuess(
         //                    'Collection',
         //                    array(),
-        //                    FieldGuess::HIGH_CONFIDENCE
+        //                    Guess::HIGH_CONFIDENCE
         //                );
                     case 'boolean':
-                        return new FieldTypeGuess(
+                        return new TypeGuess(
                             'checkbox',
                             array(),
-                            FieldGuess::HIGH_CONFIDENCE
+                            Guess::HIGH_CONFIDENCE
                         );
                     case 'datetime':
                     case 'vardatetime':
                     case 'datetimetz':
-                        return new FieldTypeGuess(
+                        return new TypeGuess(
                             'datetime',
                             array(),
-                            FieldGuess::HIGH_CONFIDENCE
+                            Guess::HIGH_CONFIDENCE
                         );
                     case 'date':
-                        return new FieldTypeGuess(
+                        return new TypeGuess(
                             'date',
                             array(),
-                            FieldGuess::HIGH_CONFIDENCE
+                            Guess::HIGH_CONFIDENCE
                         );
                     case 'decimal':
                     case 'float':
-                        return new FieldTypeGuess(
+                        return new TypeGuess(
                             'number',
                             array(),
-                            FieldGuess::MEDIUM_CONFIDENCE
+                            Guess::MEDIUM_CONFIDENCE
                         );
                     case 'integer':
                     case 'bigint':
                     case 'smallint':
-                        return new FieldTypeGuess(
+                        return new TypeGuess(
                             'integer',
                             array(),
-                            FieldGuess::MEDIUM_CONFIDENCE
+                            Guess::MEDIUM_CONFIDENCE
                         );
                     case 'string':
-                        return new FieldTypeGuess(
+                        return new TypeGuess(
                             'text',
                             array(),
-                            FieldGuess::MEDIUM_CONFIDENCE
+                            Guess::MEDIUM_CONFIDENCE
                         );
                     case 'text':
-                        return new FieldTypeGuess(
+                        return new TypeGuess(
                             'textarea',
                             array(),
-                            FieldGuess::MEDIUM_CONFIDENCE
+                            Guess::MEDIUM_CONFIDENCE
                         );
                     case 'time':
-                        return new FieldTypeGuess(
+                        return new TypeGuess(
                             'time',
                             array(),
-                            FieldGuess::HIGH_CONFIDENCE
+                            Guess::HIGH_CONFIDENCE
                         );
     //                case 'object': ???
                 }
             }
         }
 
-        return new FieldTypeGuess(
+        return new TypeGuess(
             'text',
             array(),
-            FieldGuess::LOW_CONFIDENCE
+            Guess::LOW_CONFIDENCE
         );
     }
 
@@ -151,15 +151,15 @@ class EntityFieldGuesser implements FieldGuesserInterface
 
             if ($metadata->hasField($property)) {
                 if (!$metadata->isNullable($property)) {
-                    return new FieldGuess(
+                    return new ValueGuess(
                         true,
-                        FieldGuess::HIGH_CONFIDENCE
+                        Guess::HIGH_CONFIDENCE
                     );
                 }
 
-                return new FieldGuess(
+                return new ValueGuess(
                     false,
-                    FieldGuess::MEDIUM_CONFIDENCE
+                    Guess::MEDIUM_CONFIDENCE
                 );
             }
         }
@@ -178,9 +178,9 @@ class EntityFieldGuesser implements FieldGuesserInterface
 
 
                 if (isset($mapping['length'])) {
-                    return new FieldGuess(
+                    return new ValueGuess(
                         $mapping['length'],
-                        FieldGuess::HIGH_CONFIDENCE
+                        Guess::HIGH_CONFIDENCE
                     );
                 }
             }

+ 10 - 29
src/Symfony/Component/Form/FieldGuesser/FieldGuess.php

@@ -9,19 +9,18 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Component\Form\FieldGuesser;
+namespace Symfony\Component\Form\Type\Guesser;
 
 /**
- * Contains a value guessed by a FieldGuesserInterface instance
+ * Base class for guesses made by TypeGuesserInterface implementation
  *
- * Each instance also contains a confidence value about the correctness of
- * the guessed value. Thus an instance with confidence HIGH_CONFIDENCE is
- * more likely to contain a correct value than an instance with confidence
- * LOW_CONFIDENCE.
+ * Each instance contains a confidence value about the correctness of the guess.
+ * Thus an instance with confidence HIGH_CONFIDENCE is more likely to be
+ * correct than an instance with confidence LOW_CONFIDENCE.
  *
  * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  */
-class FieldGuess
+abstract class Guess
 {
     /**
      * Marks an instance with a value that is very likely to be correct
@@ -45,18 +44,12 @@ class FieldGuess
      * The list of allowed confidence values
      * @var array
      */
-    protected static $confidences = array(
+    private static $confidences = array(
         self::HIGH_CONFIDENCE,
         self::MEDIUM_CONFIDENCE,
         self::LOW_CONFIDENCE,
     );
 
-    /**
-     * The guessed value
-     * @var mixed
-     */
-    protected $value;
-
     /**
      * The confidence about the correctness of the value
      *
@@ -64,7 +57,7 @@ class FieldGuess
      *
      * @var integer
      */
-    protected $confidence;
+    private $confidence;
 
     /**
      * Returns the guess most likely to be correct from a list of guesses
@@ -73,7 +66,7 @@ class FieldGuess
      * returned guess is any of them.
      *
      * @param  array $guesses     A list of guesses
-     * @return FieldGuess  The guess with the highest confidence
+     * @return Guess  The guess with the highest confidence
      */
     static public function getBestGuess(array $guesses)
     {
@@ -87,29 +80,17 @@ class FieldGuess
     /**
      * Constructor
      *
-     * @param mixed $value          The guessed value
      * @param integer $confidence   The confidence
      */
-    public function __construct($value, $confidence)
+    public function __construct($confidence)
     {
         if (!in_array($confidence, self::$confidences)) {
             throw new \UnexpectedValueException(sprintf('The confidence should be one of "%s"', implode('", "', self::$confidences)));
         }
 
-        $this->value = $value;
         $this->confidence = $confidence;
     }
 
-    /**
-     * Returns the guessed value
-     *
-     * @return mixed
-     */
-    public function getValue()
-    {
-        return $this->value;
-    }
-
     /**
      * Returns the confidence that the guessed value is correct
      *

+ 14 - 7
src/Symfony/Component/Form/FieldGuesser/FieldTypeGuess.php

@@ -9,7 +9,7 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Component\Form\FieldGuesser;
+namespace Symfony\Component\Form\Type\Guesser;
 
 /**
  * Contains a guessed class name and a list of options for creating an instance
@@ -17,13 +17,19 @@ namespace Symfony\Component\Form\FieldGuesser;
  *
  * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  */
-class FieldTypeGuess extends FieldGuess
+class TypeGuess extends Guess
 {
+    /**
+     * The guessed field type
+     * @var string
+     */
+    private $type;
+
     /**
      * The guessed options for creating an instance of the guessed class
      * @var array
      */
-    protected $options;
+    private $options;
 
     /**
      * Constructor
@@ -36,23 +42,24 @@ class FieldTypeGuess extends FieldGuess
      */
     public function __construct($type, array $options, $confidence)
     {
-        parent::__construct($type, $confidence);
+        parent::__construct($confidence);
 
+        $this->type = $type;
         $this->options = $options;
     }
 
     /**
-     * Returns the guessed class name
+     * Returns the guessed field type
      *
      * @return string
      */
     public function getType()
     {
-        return $this->getValue();
+        return $this->type;
     }
 
     /**
-     * Returns the guessed options for creating instances of the guessed class
+     * Returns the guessed options for creating instances of the guessed type
      *
      * @return array
      */

+ 5 - 5
src/Symfony/Component/Form/FieldGuesser/FieldGuesserInterface.php

@@ -9,21 +9,21 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Component\Form\FieldGuesser;
+namespace Symfony\Component\Form\Type\Guesser;
 
 /**
  * Guesses field classes and options for the properties of a class
  *
  * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  */
-interface FieldGuesserInterface
+interface TypeGuesserInterface
 {
     /**
      * Returns a field guess for a property name of a class
      *
      * @param  string $class           The fully qualified class name
      * @param  string $property        The name of the property to guess for
-     * @return FieldTypeGuess  A guess for the field's type and options
+     * @return TypeGuess  A guess for the field's type and options
      */
     function guessType($class, $property);
 
@@ -32,7 +32,7 @@ interface FieldGuesserInterface
      *
      * @param  string $class      The fully qualified class name
      * @param  string $property   The name of the property to guess for
-     * @return FieldGuess  A guess for the field's required setting
+     * @return Guess  A guess for the field's required setting
      */
     function guessRequired($class, $property);
 
@@ -41,7 +41,7 @@ interface FieldGuesserInterface
      *
      * @param  string $class      The fully qualified class name
      * @param  string $property   The name of the property to guess for
-     * @return FieldGuess  A guess for the field's maximum length
+     * @return Guess  A guess for the field's maximum length
      */
     function guessMaxLength($class, $property);
 }

+ 61 - 61
src/Symfony/Component/Form/FieldGuesser/ValidatorFieldGuesser.php

@@ -9,7 +9,7 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Component\Form\FieldGuesser;
+namespace Symfony\Component\Form\Type\Guesser;
 
 use Symfony\Component\Validator\Constraint;
 use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
@@ -19,7 +19,7 @@ use Symfony\Component\Validator\Mapping\ClassMetadataFactoryInterface;
  *
  * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  */
-class ValidatorFieldGuesser implements FieldGuesserInterface
+class ValidatorTypeGuesser implements TypeGuesserInterface
 {
     /**
      * Constructor
@@ -75,7 +75,7 @@ class ValidatorFieldGuesser implements FieldGuesserInterface
      * @param string $property    The property for which to find constraints
      * @param \Closure $guessForConstraint   The closure that returns a guess
      *                            for a given constraint
-     * @return FieldGuess  The guessed value with the highest confidence
+     * @return Guess  The guessed value with the highest confidence
      */
     protected function guess($class, $property, \Closure $guessForConstraint)
     {
@@ -96,14 +96,14 @@ class ValidatorFieldGuesser implements FieldGuesserInterface
             }
         }
 
-        return FieldGuess::getBestGuess($guesses);
+        return Guess::getBestGuess($guesses);
     }
 
     /**
      * Guesses a field class name for a given constraint
      *
      * @param  Constraint $constraint  The constraint to guess for
-     * @return FieldTypeGuess  The guessed field class and options
+     * @return TypeGuess  The guessed field class and options
      */
     public function guessTypeForConstraint(Constraint $constraint)
     {
@@ -112,143 +112,143 @@ class ValidatorFieldGuesser implements FieldGuesserInterface
                 switch ($constraint->type) {
                     case 'boolean':
                     case 'bool':
-                        return new FieldTypeGuess(
+                        return new TypeGuess(
                             'checkbox',
                             array(),
-                            FieldGuess::MEDIUM_CONFIDENCE
+                            Guess::MEDIUM_CONFIDENCE
                         );
                     case 'double':
                     case 'float':
                     case 'numeric':
                     case 'real':
-                        return new FieldTypeGuess(
+                        return new TypeGuess(
                             'number',
                             array(),
-                            FieldGuess::MEDIUM_CONFIDENCE
+                            Guess::MEDIUM_CONFIDENCE
                         );
                     case 'integer':
                     case 'int':
                     case 'long':
-                        return new FieldTypeGuess(
+                        return new TypeGuess(
                             'integer',
                             array(),
-                            FieldGuess::MEDIUM_CONFIDENCE
+                            Guess::MEDIUM_CONFIDENCE
                         );
                     case 'string':
-                        return new FieldTypeGuess(
+                        return new TypeGuess(
                             'text',
                             array(),
-                            FieldGuess::LOW_CONFIDENCE
+                            Guess::LOW_CONFIDENCE
                         );
                     case '\DateTime':
-                        return new FieldTypeGuess(
+                        return new TypeGuess(
                             'date',
                             array(),
-                            FieldGuess::MEDIUM_CONFIDENCE
+                            Guess::MEDIUM_CONFIDENCE
                         );
                 }
                 break;
             case 'Symfony\Component\Validator\Constraints\Choice':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'choice',
                     array('choices' => $constraint->choices),
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\Country':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'country',
                     array(),
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\Date':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'date',
                     array('type' => 'string'),
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\DateTime':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'datetime',
                     array('type' => 'string'),
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\Email':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'text',
                     array(),
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\File':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'file',
                     array(),
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\Image':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'file',
                     array(),
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\Ip':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'text',
                     array(),
-                    FieldGuess::MEDIUM_CONFIDENCE
+                    Guess::MEDIUM_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\Language':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'language',
                     array(),
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\Locale':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'locale',
                     array(),
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\Max':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'number',
                     array(),
-                    FieldGuess::LOW_CONFIDENCE
+                    Guess::LOW_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\MaxLength':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'text',
                     array(),
-                    FieldGuess::LOW_CONFIDENCE
+                    Guess::LOW_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\Min':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'number',
                     array(),
-                    FieldGuess::LOW_CONFIDENCE
+                    Guess::LOW_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\MinLength':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'text',
                     array(),
-                    FieldGuess::LOW_CONFIDENCE
+                    Guess::LOW_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\Regex':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'text',
                     array(),
-                    FieldGuess::LOW_CONFIDENCE
+                    Guess::LOW_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\Time':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'time',
                     array('type' => 'string'),
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\Url':
-                return new FieldTypeGuess(
+                return new TypeGuess(
                     'url',
                     array(),
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
         }
     }
@@ -257,25 +257,25 @@ class ValidatorFieldGuesser implements FieldGuesserInterface
      * Guesses whether a field is required based on the given constraint
      *
      * @param  Constraint $constraint  The constraint to guess for
-     * @return FieldGuess       The guess whether the field is required
+     * @return Guess       The guess whether the field is required
      */
     public function guessRequiredForConstraint(Constraint $constraint)
     {
         switch (get_class($constraint)) {
             case 'Symfony\Component\Validator\Constraints\NotNull':
-                return new FieldGuess(
+                return new ValueGuess(
                     true,
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\NotBlank':
-                return new FieldGuess(
+                return new ValueGuess(
                     true,
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
             default:
-                return new FieldGuess(
+                return new ValueGuess(
                     false,
-                    FieldGuess::LOW_CONFIDENCE
+                    Guess::LOW_CONFIDENCE
                 );
         }
     }
@@ -284,20 +284,20 @@ class ValidatorFieldGuesser implements FieldGuesserInterface
      * Guesses a field's maximum length based on the given constraint
      *
      * @param  Constraint $constraint  The constraint to guess for
-     * @return FieldGuess       The guess for the maximum length
+     * @return Guess       The guess for the maximum length
      */
     public function guessMaxLengthForConstraint(Constraint $constraint)
     {
         switch (get_class($constraint)) {
             case 'Symfony\Component\Validator\Constraints\MaxLength':
-                return new FieldGuess(
+                return new ValueGuess(
                     $constraint->limit,
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
             case 'Symfony\Component\Validator\Constraints\Max':
-                return new FieldGuess(
+                return new ValueGuess(
                     strlen((string)$constraint->limit),
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 );
         }
     }

+ 50 - 0
src/Symfony/Component/Form/Type/Guesser/ValueGuess.php

@@ -0,0 +1,50 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\Type\Guesser;
+
+/**
+ * Contains a guessed value
+ *
+ * @author Bernhard Schussek <bernhard.schussek@symfony.com>
+ */
+class ValueGuess extends Guess
+{
+    /**
+     * The guessed value
+     * @var array
+     */
+    private $value;
+
+    /**
+     * Constructor
+     *
+     * @param string $value         The guessed value
+     * @param integer $confidence   The confidence that the guessed class name
+     *                              is correct
+     */
+    public function __construct($value, $confidence)
+    {
+        parent::__construct($confidence);
+
+        $this->value = $value;
+    }
+
+    /**
+     * Returns the guessed value
+     *
+     * @return mixed
+     */
+    public function getValue()
+    {
+        return $this->value;
+    }
+}

+ 0 - 34
tests/Symfony/Tests/Component/Form/FieldGuesser/FieldGuessTest.php

@@ -1,34 +0,0 @@
-<?php
-
-/*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Tests\Component\Form\FieldGuesser;
-
-use Symfony\Component\Form\FieldGuesser\FieldGuess;
-
-class FieldGuessTest extends \PHPUnit_Framework_TestCase
-{
-    public function testGetBestGuessReturnsGuessWithHighestConfidence()
-    {
-        $guess1 = new FieldGuess('foo', FieldGuess::MEDIUM_CONFIDENCE);
-        $guess2 = new FieldGuess('bar', FieldGuess::LOW_CONFIDENCE);
-        $guess3 = new FieldGuess('baz', FieldGuess::HIGH_CONFIDENCE);
-
-        $this->assertEquals($guess3, FieldGuess::getBestGuess(array($guess1, $guess2, $guess3)));
-    }
-
-    /**
-     * @expectedException \UnexpectedValueException
-     */
-    public function testGuessExpectsValidConfidence()
-    {
-        new FieldGuess('foo', 5);
-    }
-}

+ 25 - 24
tests/Symfony/Tests/Component/Form/FormFactoryTest.php

@@ -12,8 +12,9 @@
 namespace Symfony\Tests\Component\Form\FormFactory;
 
 use Symfony\Component\Form\FormFactory;
-use Symfony\Component\Form\FieldGuesser\FieldGuess;
-use Symfony\Component\Form\FieldGuesser\FieldTypeGuess;
+use Symfony\Component\Form\Type\Guesser\Guess;
+use Symfony\Component\Form\Type\Guesser\ValueGuess;
+use Symfony\Component\Form\Type\Guesser\TypeGuess;
 
 class FormFactoryTest extends \PHPUnit_Framework_TestCase
 {
@@ -29,24 +30,24 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
 
     public function testCreateBuilderForPropertyCreatesFieldWithHighestConfidence()
     {
-        $guesser1 = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
+        $guesser1 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
         $guesser1->expects($this->once())
             ->method('guessType')
             ->with('Application\Author', 'firstName')
-            ->will($this->returnValue(new FieldTypeGuess(
+            ->will($this->returnValue(new TypeGuess(
                 'text',
                 array('max_length' => 10),
-                FieldGuess::MEDIUM_CONFIDENCE
+                Guess::MEDIUM_CONFIDENCE
             )));
 
-        $guesser2 = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
+        $guesser2 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
         $guesser2->expects($this->once())
             ->method('guessType')
             ->with('Application\Author', 'firstName')
-            ->will($this->returnValue(new FieldTypeGuess(
+            ->will($this->returnValue(new TypeGuess(
                 'password',
                 array('max_length' => 7),
-                FieldGuess::HIGH_CONFIDENCE
+                Guess::HIGH_CONFIDENCE
             )));
 
         $factory = $this->createMockFactory(array('createBuilder'));
@@ -65,7 +66,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
 
     public function testCreateBuilderCreatesTextFieldIfNoGuess()
     {
-        $guesser = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
+        $guesser = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
         $guesser->expects($this->once())
                 ->method('guessType')
                 ->with('Application\Author', 'firstName')
@@ -86,14 +87,14 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
 
     public function testOptionsCanBeOverridden()
     {
-        $guesser = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
+        $guesser = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
         $guesser->expects($this->once())
                 ->method('guessType')
                 ->with('Application\Author', 'firstName')
-                ->will($this->returnValue(new FieldTypeGuess(
+                ->will($this->returnValue(new TypeGuess(
                     'text',
                     array('max_length' => 10),
-                    FieldGuess::MEDIUM_CONFIDENCE
+                    Guess::MEDIUM_CONFIDENCE
                 )));
 
         $factory = $this->createMockFactory(array('createBuilder'));
@@ -115,22 +116,22 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
 
     public function testCreateBuilderUsesMaxLengthIfFound()
     {
-        $guesser1 = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
+        $guesser1 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
         $guesser1->expects($this->once())
                 ->method('guessMaxLength')
                 ->with('Application\Author', 'firstName')
-                ->will($this->returnValue(new FieldGuess(
+                ->will($this->returnValue(new ValueGuess(
                     15,
-                    FieldGuess::MEDIUM_CONFIDENCE
+                    Guess::MEDIUM_CONFIDENCE
                 )));
 
-        $guesser2 = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
+        $guesser2 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
         $guesser2->expects($this->once())
                 ->method('guessMaxLength')
                 ->with('Application\Author', 'firstName')
-                ->will($this->returnValue(new FieldGuess(
+                ->will($this->returnValue(new ValueGuess(
                     20,
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 )));
 
         $factory = $this->createMockFactory(array('createBuilder'));
@@ -152,22 +153,22 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
 
     public function testCreateBuilderUsesRequiredSettingWithHighestConfidence()
     {
-        $guesser1 = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
+        $guesser1 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
         $guesser1->expects($this->once())
                 ->method('guessRequired')
                 ->with('Application\Author', 'firstName')
-                ->will($this->returnValue(new FieldGuess(
+                ->will($this->returnValue(new ValueGuess(
                     true,
-                    FieldGuess::MEDIUM_CONFIDENCE
+                    Guess::MEDIUM_CONFIDENCE
                 )));
 
-        $guesser2 = $this->getMock('Symfony\Component\Form\FieldGuesser\FieldGuesserInterface');
+        $guesser2 = $this->getMock('Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
         $guesser2->expects($this->once())
                 ->method('guessRequired')
                 ->with('Application\Author', 'firstName')
-                ->will($this->returnValue(new FieldGuess(
+                ->will($this->returnValue(new ValueGuess(
                     false,
-                    FieldGuess::HIGH_CONFIDENCE
+                    Guess::HIGH_CONFIDENCE
                 )));
 
         $factory = $this->createMockFactory(array('createBuilder'));

+ 36 - 0
tests/Symfony/Tests/Component/Form/Type/Guesser/GuessTest.php

@@ -0,0 +1,36 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Tests\Component\Form\Type\Guesser;
+
+use Symfony\Component\Form\Type\Guesser\Guess;
+
+class TestGuess extends Guess {}
+
+class GuessTest extends \PHPUnit_Framework_TestCase
+{
+    public function testGetBestGuessReturnsGuessWithHighestConfidence()
+    {
+        $guess1 = new TestGuess(Guess::MEDIUM_CONFIDENCE);
+        $guess2 = new TestGuess(Guess::LOW_CONFIDENCE);
+        $guess3 = new TestGuess(Guess::HIGH_CONFIDENCE);
+
+        $this->assertSame($guess3, Guess::getBestGuess(array($guess1, $guess2, $guess3)));
+    }
+
+    /**
+     * @expectedException \UnexpectedValueException
+     */
+    public function testGuessExpectsValidConfidence()
+    {
+        new TestGuess(5);
+    }
+}