4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Guess
;
14 use Symfony\Component\Form\Exception\InvalidArgumentException
;
17 * Base class for guesses made by TypeGuesserInterface implementation
19 * Each instance contains a confidence value about the correctness of the guess.
20 * Thus an instance with confidence HIGH_CONFIDENCE is more likely to be
21 * correct than an instance with confidence LOW_CONFIDENCE.
23 * @author Bernhard Schussek <bschussek@gmail.com>
28 * Marks an instance with a value that is extremely likely to be correct
31 const VERY_HIGH_CONFIDENCE
= 3;
34 * Marks an instance with a value that is very likely to be correct
37 const HIGH_CONFIDENCE
= 2;
40 * Marks an instance with a value that is likely to be correct
43 const MEDIUM_CONFIDENCE
= 1;
46 * Marks an instance with a value that may be correct
49 const LOW_CONFIDENCE
= 0;
52 * The confidence about the correctness of the value
54 * One of VERY_HIGH_CONFIDENCE, HIGH_CONFIDENCE, MEDIUM_CONFIDENCE
62 * Returns the guess most likely to be correct from a list of guesses
64 * If there are multiple guesses with the same, highest confidence, the
65 * returned guess is any of them.
67 * @param array $guesses A list of guesses
69 * @return Guess The guess with the highest confidence
71 public static function getBestGuess(array $guesses)
76 foreach ($guesses as $guess) {
77 if ($maxConfidence < $confidence = $guess->getConfidence()) {
78 $maxConfidence = $confidence;
89 * @param integer $confidence The confidence
91 * @throws InvalidArgumentException if the given value of confidence is unknown
93 public function __construct($confidence)
95 if (self
::VERY_HIGH_CONFIDENCE
!== $confidence && self
::HIGH_CONFIDENCE
!== $confidence &&
96 self
::MEDIUM_CONFIDENCE
!== $confidence && self
::LOW_CONFIDENCE
!== $confidence) {
97 throw new InvalidArgumentException('The confidence should be one of the constants defined in Guess.');
100 $this->confidence
= $confidence;
104 * Returns the confidence that the guessed value is correct
106 * @return integer One of the constants VERY_HIGH_CONFIDENCE,
107 * HIGH_CONFIDENCE, MEDIUM_CONFIDENCE and LOW_CONFIDENCE
109 public function getConfidence()
111 return $this->confidence
;