Guess.php 2.9 KB

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