]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Guess/Guess.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Guess / Guess.php
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Form\Guess;
13
14 use Symfony\Component\Form\Exception\InvalidArgumentException;
15
16 /**
17 * Base class for guesses made by TypeGuesserInterface implementation
18 *
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.
22 *
23 * @author Bernhard Schussek <bschussek@gmail.com>
24 */
25 abstract class Guess
26 {
27 /**
28 * Marks an instance with a value that is extremely likely to be correct
29 * @var integer
30 */
31 const VERY_HIGH_CONFIDENCE = 3;
32
33 /**
34 * Marks an instance with a value that is very likely to be correct
35 * @var integer
36 */
37 const HIGH_CONFIDENCE = 2;
38
39 /**
40 * Marks an instance with a value that is likely to be correct
41 * @var integer
42 */
43 const MEDIUM_CONFIDENCE = 1;
44
45 /**
46 * Marks an instance with a value that may be correct
47 * @var integer
48 */
49 const LOW_CONFIDENCE = 0;
50
51 /**
52 * The confidence about the correctness of the value
53 *
54 * One of VERY_HIGH_CONFIDENCE, HIGH_CONFIDENCE, MEDIUM_CONFIDENCE
55 * and LOW_CONFIDENCE.
56 *
57 * @var integer
58 */
59 private $confidence;
60
61 /**
62 * Returns the guess most likely to be correct from a list of guesses
63 *
64 * If there are multiple guesses with the same, highest confidence, the
65 * returned guess is any of them.
66 *
67 * @param array $guesses A list of guesses
68 *
69 * @return Guess The guess with the highest confidence
70 */
71 public static function getBestGuess(array $guesses)
72 {
73 $result = null;
74 $maxConfidence = -1;
75
76 foreach ($guesses as $guess) {
77 if ($maxConfidence < $confidence = $guess->getConfidence()) {
78 $maxConfidence = $confidence;
79 $result = $guess;
80 }
81 }
82
83 return $result;
84 }
85
86 /**
87 * Constructor
88 *
89 * @param integer $confidence The confidence
90 *
91 * @throws InvalidArgumentException if the given value of confidence is unknown
92 */
93 public function __construct($confidence)
94 {
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.');
98 }
99
100 $this->confidence = $confidence;
101 }
102
103 /**
104 * Returns the confidence that the guessed value is correct
105 *
106 * @return integer One of the constants VERY_HIGH_CONFIDENCE,
107 * HIGH_CONFIDENCE, MEDIUM_CONFIDENCE and LOW_CONFIDENCE
108 */
109 public function getConfidence()
110 {
111 return $this->confidence;
112 }
113 }