diff options
Diffstat (limited to 'vendor/symfony/form/Symfony/Component/Form/Guess')
3 files changed, 233 insertions, 0 deletions
diff --git a/vendor/symfony/form/Symfony/Component/Form/Guess/Guess.php b/vendor/symfony/form/Symfony/Component/Form/Guess/Guess.php new file mode 100644 index 00000000..b33c3d80 --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Guess/Guess.php | |||
@@ -0,0 +1,113 @@ | |||
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 | } | ||
diff --git a/vendor/symfony/form/Symfony/Component/Form/Guess/TypeGuess.php b/vendor/symfony/form/Symfony/Component/Form/Guess/TypeGuess.php new file mode 100644 index 00000000..3241e603 --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Guess/TypeGuess.php | |||
@@ -0,0 +1,70 @@ | |||
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 | * Contains a guessed class name and a list of options for creating an instance | ||
16 | * of that class | ||
17 | * | ||
18 | * @author Bernhard Schussek <bschussek@gmail.com> | ||
19 | */ | ||
20 | class TypeGuess extends Guess | ||
21 | { | ||
22 | /** | ||
23 | * The guessed field type | ||
24 | * @var string | ||
25 | */ | ||
26 | private $type; | ||
27 | |||
28 | /** | ||
29 | * The guessed options for creating an instance of the guessed class | ||
30 | * @var array | ||
31 | */ | ||
32 | private $options; | ||
33 | |||
34 | /** | ||
35 | * Constructor | ||
36 | * | ||
37 | * @param string $type The guessed field type | ||
38 | * @param array $options The options for creating instances of the | ||
39 | * guessed class | ||
40 | * @param integer $confidence The confidence that the guessed class name | ||
41 | * is correct | ||
42 | */ | ||
43 | public function __construct($type, array $options, $confidence) | ||
44 | { | ||
45 | parent::__construct($confidence); | ||
46 | |||
47 | $this->type = $type; | ||
48 | $this->options = $options; | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | * Returns the guessed field type | ||
53 | * | ||
54 | * @return string | ||
55 | */ | ||
56 | public function getType() | ||
57 | { | ||
58 | return $this->type; | ||
59 | } | ||
60 | |||
61 | /** | ||
62 | * Returns the guessed options for creating instances of the guessed type | ||
63 | * | ||
64 | * @return array | ||
65 | */ | ||
66 | public function getOptions() | ||
67 | { | ||
68 | return $this->options; | ||
69 | } | ||
70 | } | ||
diff --git a/vendor/symfony/form/Symfony/Component/Form/Guess/ValueGuess.php b/vendor/symfony/form/Symfony/Component/Form/Guess/ValueGuess.php new file mode 100644 index 00000000..2e3333ba --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Guess/ValueGuess.php | |||
@@ -0,0 +1,50 @@ | |||
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 | * Contains a guessed value | ||
16 | * | ||
17 | * @author Bernhard Schussek <bschussek@gmail.com> | ||
18 | */ | ||
19 | class ValueGuess extends Guess | ||
20 | { | ||
21 | /** | ||
22 | * The guessed value | ||
23 | * @var array | ||
24 | */ | ||
25 | private $value; | ||
26 | |||
27 | /** | ||
28 | * Constructor | ||
29 | * | ||
30 | * @param string $value The guessed value | ||
31 | * @param integer $confidence The confidence that the guessed class name | ||
32 | * is correct | ||
33 | */ | ||
34 | public function __construct($value, $confidence) | ||
35 | { | ||
36 | parent::__construct($confidence); | ||
37 | |||
38 | $this->value = $value; | ||
39 | } | ||
40 | |||
41 | /** | ||
42 | * Returns the guessed value | ||
43 | * | ||
44 | * @return mixed | ||
45 | */ | ||
46 | public function getValue() | ||
47 | { | ||
48 | return $this->value; | ||
49 | } | ||
50 | } | ||