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
Implemented new interface in DoctrineOrmTypeGuesser
  • Loading branch information
Stefano Sala committed Mar 31, 2014
commit 36f89a8f9d51bc2dbff4d9b4fea250296c5d2a43
47 changes: 42 additions & 5 deletions src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,44 @@ public function guessType($class, $property)
/**
* {@inheritDoc}
*/
public function guessRequired($class, $property)
public function guessAttributes($class, $property)
{
$attributes = array();

if ($guess = $this->guessRequired($class, $property)) {
$attributes['required'] = $guess;
}

if ($guess = $this->guessMaxLength($class, $property)) {
$attributes['maxlength'] = $guess;
}

if ($guess = $this->guessMinValue($class, $property)) {
$attributes['min'] = $guess;
}

if ($guess = $this->guessMaxValue($class, $property)) {
$attributes['max'] = $guess;
}

return $attributes;
}

private function addAttribute(array $attributes, $key, Guess $guess = null)
{
if (null === $guess) {
return $attributes;
}

if ($value = $guess->getValue()) {
return array_merge($attributes, array($key => $guess->getValue()));
}
}

/**
* {@inheritDoc}
*/
protected function guessRequired($class, $property)
{
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you changed to @deprecated you can use @inheritdoc again.

Copy link
Author

Choose a reason for hiding this comment

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

Right, thanks!

$classMetadatas = $this->getMetadata($class);

Expand Down Expand Up @@ -122,7 +159,7 @@ public function guessRequired($class, $property)
/**
* {@inheritDoc}
*/
public function guessMaxLength($class, $property)
protected function guessMaxLength($class, $property)
Copy link
Contributor

Choose a reason for hiding this comment

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

why protected? As far as I see, it's not used for inheritance. So it should either be private or public (for BC).
Applies to all of these changes.

{
$ret = $this->getMetadata($class);
if ($ret && $ret[0]->hasField($property) && !$ret[0]->hasAssociation($property)) {
Expand All @@ -141,7 +178,7 @@ public function guessMaxLength($class, $property)
/**
* {@inheritDoc}
*/
public function guessMinValue($class, $property)
protected function guessMinValue($class, $property)
{
// Unfortunately we can't guess anything from database
return null;
Expand All @@ -150,7 +187,7 @@ public function guessMinValue($class, $property)
/**
* {@inheritDoc}
*/
public function guessMaxValue($class, $property)
protected function guessMaxValue($class, $property)
{
// Unfortunately we can't guess anything from database
return null;
Expand All @@ -159,7 +196,7 @@ public function guessMaxValue($class, $property)
/**
* {@inheritDoc}
*/
public function guessPattern($class, $property)
protected function guessPattern($class, $property)
{
$ret = $this->getMetadata($class);
if ($ret && $ret[0]->hasField($property) && !$ret[0]->hasAssociation($property)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class DoctrineOrmTypeGuesserTest extends \PHPUnit_Framework_TestCase
*/
public function testRequiredGuesser($classMetadata, $expected)
{
$this->assertEquals($expected, $this->getGuesser($classMetadata)->guessRequired('TestEntity', 'field'));
$attributes = $this->getGuesser($classMetadata)->guessAttributes('TestEntity', 'field');

$this->assertEquals($expected, isset($attributes['required']) ? $attributes['required'] : null);
}

public function requiredProvider()
Expand All @@ -32,21 +34,21 @@ public function requiredProvider()

// Simple field, not nullable
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->expects($this->once())->method('hasField')->with('field')->will($this->returnValue(true));
$classMetadata->expects($this->any())->method('hasField')->with('field')->will($this->returnValue(true));
$classMetadata->expects($this->once())->method('isNullable')->with('field')->will($this->returnValue(false));

$return[] = array($classMetadata, new ValueGuess(true, Guess::HIGH_CONFIDENCE));

// Simple field, nullable
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->expects($this->once())->method('hasField')->with('field')->will($this->returnValue(true));
$classMetadata->expects($this->any())->method('hasField')->with('field')->will($this->returnValue(true));
$classMetadata->expects($this->once())->method('isNullable')->with('field')->will($this->returnValue(true));

$return[] = array($classMetadata, new ValueGuess(false, Guess::MEDIUM_CONFIDENCE));

// One-to-one, nullable (by default)
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->expects($this->once())->method('hasField')->with('field')->will($this->returnValue(false));
$classMetadata->expects($this->any())->method('hasField')->with('field')->will($this->returnValue(false));
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->will($this->returnValue(true));

$mapping = array('joinColumns' => array(array()));
Expand All @@ -56,7 +58,7 @@ public function requiredProvider()

// One-to-one, nullable (explicit)
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->expects($this->once())->method('hasField')->with('field')->will($this->returnValue(false));
$classMetadata->expects($this->any())->method('hasField')->with('field')->will($this->returnValue(false));
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->will($this->returnValue(true));

$mapping = array('joinColumns' => array(array('nullable'=>true)));
Expand All @@ -66,7 +68,7 @@ public function requiredProvider()

// One-to-one, not nullable
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->expects($this->once())->method('hasField')->with('field')->will($this->returnValue(false));
$classMetadata->expects($this->any())->method('hasField')->with('field')->will($this->returnValue(false));
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->will($this->returnValue(true));

$mapping = array('joinColumns' => array(array('nullable'=>false)));
Expand All @@ -76,7 +78,7 @@ public function requiredProvider()

// One-to-many, no clue
$classMetadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')->disableOriginalConstructor()->getMock();
$classMetadata->expects($this->once())->method('hasField')->with('field')->will($this->returnValue(false));
$classMetadata->expects($this->any())->method('hasField')->with('field')->will($this->returnValue(false));
$classMetadata->expects($this->once())->method('isAssociationWithSingleJoinColumn')->with('field')->will($this->returnValue(false));

$return[] = array($classMetadata, null);
Expand Down
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