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 PropelTypeGuesser
  • Loading branch information
Stefano Sala committed Mar 31, 2014
commit 30cbb884cd325a8f0a4444fd33b1342b3d686db6
11 changes: 0 additions & 11 deletions src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,6 @@ public function guessAttributes($class, $property)
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}
*/
Expand Down
36 changes: 31 additions & 5 deletions src/Symfony/Bridge/Propel1/Form/PropelTypeGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,33 @@ 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;
}

/**
* {@inheritDoc}
*/
protected function guessRequired($class, $property)
{
if ($column = $this->getColumn($class, $property)) {
return new ValueGuess($column->isNotNull(), Guess::HIGH_CONFIDENCE);
Expand All @@ -120,7 +146,7 @@ public function guessRequired($class, $property)
/**
* {@inheritDoc}
*/
public function guessMaxLength($class, $property)
protected function guessMaxLength($class, $property)
{
if ($column = $this->getColumn($class, $property)) {
if ($column->isText()) {
Expand All @@ -139,7 +165,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 @@ -148,7 +174,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 @@ -157,7 +183,7 @@ public function guessMaxValue($class, $property)
/**
* {@inheritDoc}
*/
public function guessPattern($class, $property)
protected function guessPattern($class, $property)
{
if ($column = $this->getColumn($class, $property)) {
switch ($column->getType()) {
Expand Down
35 changes: 18 additions & 17 deletions src/Symfony/Bridge/Propel1/Tests/Form/PropelTypeGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,50 @@ public function setUp()

public function testGuessMaxLengthWithText()
{
$value = $this->guesser->guessMaxLength(self::CLASS_NAME, 'value');
$attributes = $this->guesser->guessAttributes(self::CLASS_NAME, 'value');

$this->assertNotNull($value);
$this->assertEquals(255, $value->getValue());
$this->assertArrayHasKey('maxlength', $attributes);
$this->assertEquals(255, $attributes['maxlength']->getValue());
}

public function testGuessMaxLengthWithFloat()
{
$value = $this->guesser->guessMaxLength(self::CLASS_NAME, 'price');
$attributes = $this->guesser->guessAttributes(self::CLASS_NAME, 'price');

$this->assertNotNull($value);
$this->assertNull($value->getValue());
$this->assertArrayHasKey('maxlength', $attributes);
$this->assertEquals(null, $attributes['maxlength']->getValue());
Copy link
Contributor

Choose a reason for hiding this comment

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

assertNull() same below.

}

public function testGuessMinLengthWithText()
{
$value = $this->guesser->guessPattern(self::CLASS_NAME, 'value');
$attributes = $this->guesser->guessAttributes(self::CLASS_NAME, 'price');

$this->assertNull($value);
$this->assertArrayHasKey('maxlength', $attributes);
$this->assertEquals(null, $attributes['maxlength']->getValue());
}

public function testGuessMinLengthWithFloat()
{
$value = $this->guesser->guessPattern(self::CLASS_NAME, 'price');
$attributes = $this->guesser->guessAttributes(self::CLASS_NAME, 'price');

$this->assertNotNull($value);
$this->assertNull($value->getValue());
$this->assertArrayHasKey('maxlength', $attributes);
$this->assertEquals(null, $attributes['maxlength']->getValue());
}

public function testGuessRequired()
{
$value = $this->guesser->guessRequired(self::CLASS_NAME, 'id');
$attributes = $this->guesser->guessAttributes(self::CLASS_NAME, 'id');

$this->assertNotNull($value);
$this->assertTrue($value->getValue());
$this->assertArrayHasKey('required', $attributes);
$this->assertEquals(true, $attributes['required']->getValue());
}

public function testGuessRequiredWithNullableColumn()
{
$value = $this->guesser->guessRequired(self::CLASS_NAME, 'value');
$attributes = $this->guesser->guessAttributes(self::CLASS_NAME, 'value');

$this->assertNotNull($value);
$this->assertFalse($value->getValue());
$this->assertArrayHasKey('required', $attributes);
$this->assertEquals(false, $attributes['required']->getValue());
}

public function testGuessTypeWithoutTable()
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