-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Implemented CoreTypeGuesser for submit button detection #9592
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/Symfony/Component/Form/Extension/Core/CoreTypeGuesser.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?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\Extension\Core; | ||
|
||
use Symfony\Component\Form\FormTypeGuesserInterface; | ||
use Symfony\Component\Form\Guess\Guess; | ||
use Symfony\Component\Form\Guess\TypeGuess; | ||
use Symfony\Component\Form\Util\ClassReflectionUtil; | ||
|
||
/** | ||
* @author Andrew Moore <me@andrewmoore.ca> | ||
*/ | ||
class CoreTypeGuesser implements FormTypeGuesserInterface | ||
{ | ||
private static $nameToTypeMapping = array( | ||
'submit' => 'submit', | ||
'reset' => 'reset', | ||
); | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function guessType($class, $property) | ||
{ | ||
// Guess types for specialized button types | ||
if (!array_key_exists($property, self::$nameToTypeMapping)) { | ||
return null; | ||
} | ||
|
||
if (!ClassReflectionUtil::hasPropertyAvailable($class, $property)) { | ||
return new TypeGuess(self::$nameToTypeMapping[$property], array(), Guess::LOW_CONFIDENCE); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function guessRequired($class, $property) | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function guessMaxLength($class, $property) | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function guessPattern($class, $property) | ||
{ | ||
return null; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/Symfony/Component/Form/Tests/Extension/Core/CoreTypeGuesserTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?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\Tests\Extension\Core; | ||
|
||
use Symfony\Component\Form\Guess\Guess; | ||
use Symfony\Component\Form\Extension\Core\CoreTypeGuesser; | ||
|
||
class CoreTypeGuesserTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** @var CoreTypeGuesser */ | ||
private $guesser; | ||
|
||
const AUTHOR_FIXTURE = 'Symfony\Component\Form\Tests\Fixtures\Author'; | ||
const PROPERTIES_DEFINED_FIXTURE = 'Symfony\Component\Form\Tests\Fixtures\SubmitResetPropertyFixture'; | ||
|
||
protected function setUp() | ||
{ | ||
$this->guesser = new CoreTypeGuesser(); | ||
} | ||
|
||
public function testButtonTypeInferral() | ||
{ | ||
$submitGuess = $this->guesser->guessType(self::AUTHOR_FIXTURE, 'submit'); | ||
$resetGuess = $this->guesser->guessType(self::AUTHOR_FIXTURE, 'reset'); | ||
|
||
$this->assertInstanceOf('Symfony\Component\Form\Guess\TypeGuess', $submitGuess); | ||
$this->assertEquals('submit', $submitGuess->getType()); | ||
$this->assertEquals(Guess::LOW_CONFIDENCE, $submitGuess->getConfidence()); | ||
|
||
$this->assertInstanceOf('Symfony\Component\Form\Guess\TypeGuess', $resetGuess); | ||
$this->assertEquals('reset', $resetGuess->getType()); | ||
$this->assertEquals(Guess::LOW_CONFIDENCE, $resetGuess->getConfidence()); | ||
} | ||
|
||
public function testButtonPropertyExists() | ||
{ | ||
$submitGuess = $this->guesser->guessType(self::PROPERTIES_DEFINED_FIXTURE, 'submit'); | ||
$resetGuess = $this->guesser->guessType(self::PROPERTIES_DEFINED_FIXTURE, 'reset'); | ||
|
||
$this->assertNull($submitGuess); | ||
$this->assertNull($resetGuess); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Symfony/Component/Form/Tests/Fixtures/SubmitResetPropertyFixture.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?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\Tests\Fixtures; | ||
|
||
class SubmitResetPropertyFixture | ||
{ | ||
private $submit; | ||
public $reset; | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getSubmit() | ||
{ | ||
return $this->submit; | ||
} | ||
|
||
/** | ||
* @param mixed $submit | ||
*/ | ||
public function setSubmit($submit) | ||
{ | ||
$this->submit = $submit; | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
src/Symfony/Component/Form/Util/ClassReflectionUtil.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?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\Util; | ||
|
||
use Symfony\Component\PropertyAccess\StringUtil; | ||
|
||
/** | ||
* @author Andrew Moore <me@andrewmoore.ca> | ||
*/ | ||
class ClassReflectionUtil | ||
{ | ||
/** | ||
* This class should not be instantiated | ||
*/ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* Detects if a property or adder/setter is available for a given | ||
* property. | ||
* | ||
* @param string $class The class to verify against | ||
* @param string $property The property to verify | ||
* | ||
* @return Boolean | ||
*/ | ||
public static function hasPropertyAvailable($class, $property) | ||
{ | ||
if (!class_exists($class)) { | ||
return false; | ||
} | ||
|
||
$reflClass = new \ReflectionClass($class); | ||
|
||
$setter = 'set' . self::camelize($property); | ||
$classHasProperty = $reflClass->hasProperty($property); | ||
|
||
if ($reflClass->hasMethod($setter) && $reflClass->getMethod($setter)->isPublic()) { | ||
return true; | ||
} | ||
if ($reflClass->hasMethod('__set') && $reflClass->getMethod('__set')->isPublic()) { | ||
return true; | ||
} | ||
if ($classHasProperty && $reflClass->getProperty($property)->isPublic()) { | ||
return true; | ||
} | ||
|
||
$plural = self::camelize($property); | ||
$singular = StringUtil::singularify($plural); | ||
|
||
$adder = 'add' . $singular; | ||
$remover = 'remove' . $singular; | ||
|
||
$adderFound = self::isMethodAccessible($reflClass, $adder, 1); | ||
$removerFound = self::isMethodAccessible($reflClass, $remover, 1); | ||
|
||
if ($adderFound && $removerFound) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Camelizes a given string. | ||
* | ||
* @param string $string Some string | ||
* | ||
* @return string The camelized version of the string | ||
*/ | ||
private static function camelize($string) | ||
{ | ||
return preg_replace_callback( | ||
'/(^|_|\.)+(.)/', | ||
function ($match) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this a regular method? As your creating a closure every time the camelize method is called (which can be quite allot). |
||
return ('.' === $match[1] ? '_' : '') . strtoupper($match[2]); | ||
}, | ||
$string | ||
); | ||
} | ||
|
||
/** | ||
* Returns whether a method is public and has a specific number of required parameters. | ||
* | ||
* @param \ReflectionClass $class The class of the method | ||
* @param string $methodName The method name | ||
* @param integer $parameters The number of parameters | ||
* | ||
* @return Boolean Whether the method is public and has $parameters | ||
* required parameters | ||
*/ | ||
private static function isMethodAccessible(\ReflectionClass $class, $methodName, $parameters) | ||
{ | ||
if ($class->hasMethod($methodName)) { | ||
$method = $class->getMethod($methodName); | ||
|
||
if ($method->isPublic() && $method->getNumberOfRequiredParameters() === $parameters) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. run php-cs-fixer |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like much code duplication with the PropertyAccess component. We can probably reuse it which is a required dependency anyway. Maybe the new
isReadable
method, see #10570There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tobion you should have looked at the discussion first. The PR was already referenced