|
0
|
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 |
/** |
|
|
15 |
* Base class for guesses made by TypeGuesserInterface implementation |
|
|
16 |
* |
|
|
17 |
* Each instance contains a confidence value about the correctness of the guess. |
|
|
18 |
* Thus an instance with confidence HIGH_CONFIDENCE is more likely to be |
|
|
19 |
* correct than an instance with confidence LOW_CONFIDENCE. |
|
|
20 |
* |
|
|
21 |
* @author Bernhard Schussek <bernhard.schussek@symfony.com> |
|
|
22 |
*/ |
|
|
23 |
abstract class Guess |
|
|
24 |
{ |
|
|
25 |
/** |
|
|
26 |
* Marks an instance with a value that is very likely to be correct |
|
|
27 |
* @var integer |
|
|
28 |
*/ |
|
|
29 |
const HIGH_CONFIDENCE = 2; |
|
|
30 |
|
|
|
31 |
/** |
|
|
32 |
* Marks an instance with a value that is likely to be correct |
|
|
33 |
* @var integer |
|
|
34 |
*/ |
|
|
35 |
const MEDIUM_CONFIDENCE = 1; |
|
|
36 |
|
|
|
37 |
/** |
|
|
38 |
* Marks an instance with a value that may be correct |
|
|
39 |
* @var integer |
|
|
40 |
*/ |
|
|
41 |
const LOW_CONFIDENCE = 0; |
|
|
42 |
|
|
|
43 |
/** |
|
|
44 |
* The list of allowed confidence values |
|
|
45 |
* @var array |
|
|
46 |
*/ |
|
|
47 |
private static $confidences = array( |
|
|
48 |
self::HIGH_CONFIDENCE, |
|
|
49 |
self::MEDIUM_CONFIDENCE, |
|
|
50 |
self::LOW_CONFIDENCE, |
|
|
51 |
); |
|
|
52 |
|
|
|
53 |
/** |
|
|
54 |
* The confidence about the correctness of the value |
|
|
55 |
* |
|
|
56 |
* One of HIGH_CONFIDENCE, MEDIUM_CONFIDENCE and LOW_CONFIDENCE. |
|
|
57 |
* |
|
|
58 |
* @var integer |
|
|
59 |
*/ |
|
|
60 |
private $confidence; |
|
|
61 |
|
|
|
62 |
/** |
|
|
63 |
* Returns the guess most likely to be correct from a list of guesses |
|
|
64 |
* |
|
|
65 |
* If there are multiple guesses with the same, highest confidence, the |
|
|
66 |
* returned guess is any of them. |
|
|
67 |
* |
|
|
68 |
* @param array $guesses A list of guesses |
|
|
69 |
* @return Guess The guess with the highest confidence |
|
|
70 |
*/ |
|
|
71 |
static public function getBestGuess(array $guesses) |
|
|
72 |
{ |
|
|
73 |
usort($guesses, function ($a, $b) { |
|
|
74 |
return $b->getConfidence() - $a->getConfidence(); |
|
|
75 |
}); |
|
|
76 |
|
|
|
77 |
return count($guesses) > 0 ? $guesses[0] : null; |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
/** |
|
|
81 |
* Constructor |
|
|
82 |
* |
|
|
83 |
* @param integer $confidence The confidence |
|
|
84 |
*/ |
|
|
85 |
public function __construct($confidence) |
|
|
86 |
{ |
|
|
87 |
if (!in_array($confidence, self::$confidences)) { |
|
|
88 |
throw new \UnexpectedValueException(sprintf('The confidence should be one of "%s"', implode('", "', self::$confidences))); |
|
|
89 |
} |
|
|
90 |
|
|
|
91 |
$this->confidence = $confidence; |
|
|
92 |
} |
|
|
93 |
|
|
|
94 |
/** |
|
|
95 |
* Returns the confidence that the guessed value is correct |
|
|
96 |
* |
|
|
97 |
* @return integer One of the constants HIGH_CONFIDENCE, MEDIUM_CONFIDENCE |
|
|
98 |
* and LOW_CONFIDENCE |
|
|
99 |
*/ |
|
|
100 |
public function getConfidence() |
|
|
101 |
{ |
|
|
102 |
return $this->confidence; |
|
|
103 |
} |
|
|
104 |
} |