Skip to content

[Form] Refactor guessing of form options and added min and max guessing #9759

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

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[Form][PoC] Work on guesses
  • Loading branch information
Stefano Sala committed Mar 31, 2014
commit 6ca221029cdf649c912c3e7f6c09d91eeec05bdf
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ public function guessType($class, $property)
});
}

/**
* {@inheritDoc}
*/
public function guessOptions($class, $property, $type)
Copy link
Contributor

Choose a reason for hiding this comment

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

You should add the type hint ResolvedFormTypeInterface to the $type argument.

{
$options = array();

switch ($type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of the switch, you can do:

if ($type->getOptionsResolver()->isKnown('max_length')) {

case 'text':
if (($guess = $this->guessMaxLength($class, $property)) && null !== $guess->getValue()) {
$options = array_merge(array('max_length' => $guess->getValue()), $options);
}
break;
}

return $options;
}

/**
* {@inheritDoc}
*/
Expand All @@ -56,7 +74,7 @@ public function guessRequired($class, $property)
/**
* {@inheritDoc}
*/
public function guessMaxLength($class, $property)
protected function guessMaxLength($class, $property)
{
$guesser = $this;

Expand Down Expand Up @@ -213,7 +231,7 @@ public function guessRequiredForConstraint(Constraint $constraint)
*
* @return ValueGuess|null The guess for the maximum length
*/
public function guessMaxLengthForConstraint(Constraint $constraint)
protected function guessMaxLengthForConstraint(Constraint $constraint)
{
switch (get_class($constraint)) {
case 'Symfony\Component\Validator\Constraints\Length':
Expand Down
53 changes: 4 additions & 49 deletions src/Symfony/Component/Form/FormTypeGuesserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,58 +27,13 @@ interface FormTypeGuesserInterface
public function guessType($class, $property);

/**
* Returns a guess whether a property of a class is required
* Returns an array of guessed options
*
* @param string $class The fully qualified class name
* @param string $property The name of the property to guess for
* @param string $type Boh.
*
* @return Guess\ValueGuess A guess for the field's required setting
* @return array An array of guesses for the field's option
*/
public function guessRequired($class, $property);

/**
* Returns a guess about the field's maximum length
*
* @param string $class The fully qualified class name
* @param string $property The name of the property to guess for
*
* @return Guess\ValueGuess|null A guess for the field's maximum length
*/
public function guessMaxLength($class, $property);

/**
* Returns a guess about the field's maximum value
*
* @param string $class The fully qualified class name
* @param string $property The name of the property to guess for
*
* @return Guess\ValueGuess|null A guess for the field's maximum value
*/
public function guessMaxValue($class, $property);

/**
* Returns a guess about the field's minimum value
*
* @param string $class The fully qualified class name
* @param string $property The name of the property to guess for
*
* @return Guess\ValueGuess|null A guess for the field's minimum value
*/
public function guessMinValue($class, $property);

/**
* Returns a guess about the field's pattern
*
* - When you have a min value, you guess a min length of this min (LOW_CONFIDENCE) , lines below
* - If this value is a float type, this is wrong so you guess null with MEDIUM_CONFIDENCE to override the previous guess.
* Example:
* You want a float greater than 5, 4.512313 is not valid but length(4.512314) > length(5)
* @link https://github.com/symfony/symfony/pull/3927
*
* @param string $class The fully qualified class name
* @param string $property The name of the property to guess for
*
* @return Guess\ValueGuess|null A guess for the field's required pattern
*/
public function guessPattern($class, $property);
public function guessOptions($class, $property, $type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,50 +30,31 @@ public function setUp()
$this->markTestSkipped('The "Validator" component is not available');
}

$metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');
$this->metadataFactory = $this->getMock('Symfony\Component\Validator\MetadataFactoryInterface');

$this->typeGuesser = new ValidatorTypeGuesser($metadataFactory);
$this->typeGuesser = new ValidatorTypeGuesser($this->metadataFactory);
}

public function testGuessMaxLengthForConstraintWithMaxValue()
{
$constraint = new Length(array('max' => '2'));
$class = new \stdClass();

$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
$this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result);
$this->assertEquals(2, $result->getValue());
$this->assertEquals(Guess::HIGH_CONFIDENCE, $result->getConfidence());
}
$this->setupMetadata($class, 'foo', array(new Length(array('max' => '2'))));

public function testGuessMaxLengthForConstraintWithMinValue()
{
$constraint = new Length(array('min' => '2'));
$result = $this->typeGuesser->guessOptions($class, 'foo', 'text');

$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
$this->assertNull($result);
$this->assertEquals(array('max_length' => 2), $result);
}

/**
* @dataProvider dataProviderTestGuessMaxLengthForConstraintWithType
*/
public function testGuessMaxLengthForConstraintWithType($type)
public function testGuessMaxLengthForConstraintWithMinValue()
{
$constraint = new Type($type);
$class = new \stdClass();

$result = $this->typeGuesser->guessMaxLengthForConstraint($constraint);
$this->assertInstanceOf('Symfony\Component\Form\Guess\ValueGuess', $result);
$this->assertEquals(null, $result->getValue());
$this->assertEquals(Guess::MEDIUM_CONFIDENCE, $result->getConfidence());
}
$this->setupMetadata($class, 'foo', array(new Length(array('min' => '2'))));

public static function dataProviderTestGuessMaxLengthForConstraintWithType()
{
return array (
array('double'),
array('float'),
array('numeric'),
array('real')
);
$result = $this->typeGuesser->guessOptions($class, 'foo', 'text');

$this->assertEquals(array(), $result);
}

public function testGuessMinValueForConstraintWithMinValue()
Expand Down Expand Up @@ -111,4 +92,29 @@ public function testGuessMaxValueForConstraintWithMinValue()
$result = $this->typeGuesser->guessMaxValueForConstraint($constraint);
$this->assertNull($result);
}

private function setupMetadata($class, $property, array $constraints)
{
$this->elementMetadata = $this->getMock('Symfony\Component\Validator\Mapping\ElementMetadata');
$this->elementMetadata->expects($this->once())
->method('getConstraints')
->will($this->returnValue($constraints));

$this->metadata = $this->getMockBuilder('Symfony\Component\Validator\Mapping\ClassMetadata')
->disableOriginalConstructor()
->getMock();
$this->metadata->expects($this->once())
->method('hasMemberMetadatas')
->with($property)
->will($this->returnValue(true));
$this->metadata->expects($this->once())
->method('getMemberMetadatas')
->with($property)
->will($this->returnValue(array($this->elementMetadata)));

$this->metadataFactory->expects($this->once())
->method('getMetadataFor')
->with($class)
->will($this->returnValue($this->metadata));
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy