Пример #1
0
 public function testGetBestGuessReturnsGuessWithHighestConfidence()
 {
     $guess1 = new TestGuess(Guess::MEDIUM_CONFIDENCE);
     $guess2 = new TestGuess(Guess::LOW_CONFIDENCE);
     $guess3 = new TestGuess(Guess::HIGH_CONFIDENCE);
     $this->assertSame($guess3, Guess::getBestGuess(array($guess1, $guess2, $guess3)));
 }
Пример #2
0
 /**
  * Executes a closure for each guesser and returns the best guess from the
  * return values
  *
  * @param \Closure $closure The closure to execute. Accepts a guesser
  *                            as argument and should return a Guess instance
  *
  * @return FieldFactoryGuess  The guess with the highest confidence
  */
 private function guess(\Closure $closure)
 {
     $guesses = array();
     foreach ($this->guessers as $guesser) {
         if ($guess = $closure($guesser)) {
             $guesses[] = $guess;
         }
     }
     return Guess::getBestGuess($guesses);
 }
Пример #3
0
 private function guess(callable $closure)
 {
     $guesses = [];
     foreach ($this->guessers as $guesser) {
         if ($guess = $closure($guesser)) {
             $guesses[] = $guess;
         }
     }
     return Guess::getBestGuess($guesses);
 }
Пример #4
0
 /**
  * Iterates over the constraints of a property, executes a constraints on
  * them and returns the best guess.
  *
  * @param string   $class        The class to read the constraints from
  * @param string   $property     The property for which to find constraints
  * @param \Closure $closure      The closure that returns a guess
  *                               for a given constraint
  * @param mixed    $defaultValue The default value assumed if no other value
  *                               can be guessed.
  *
  * @return Guess|null The guessed value with the highest confidence
  */
 protected function guess($class, $property, \Closure $closure, $defaultValue = null)
 {
     $guesses = array();
     $classMetadata = $this->metadataFactory->getMetadataFor($class);
     if ($classMetadata instanceof ClassMetadataInterface && $classMetadata->hasPropertyMetadata($property)) {
         $memberMetadatas = $classMetadata->getPropertyMetadata($property);
         foreach ($memberMetadatas as $memberMetadata) {
             $constraints = $memberMetadata->getConstraints();
             foreach ($constraints as $constraint) {
                 if ($guess = $closure($constraint)) {
                     $guesses[] = $guess;
                 }
             }
         }
     }
     if (null !== $defaultValue) {
         $guesses[] = new ValueGuess($defaultValue, Guess::LOW_CONFIDENCE);
     }
     return Guess::getBestGuess($guesses);
 }
Пример #5
0
 /**
  * Iterates over the constraints of a property, executes a constraints on
  * them and returns the best guess
  *
  * @param string $class       The class to read the constraints from
  * @param string $property    The property for which to find constraints
  * @param \Closure $guessForConstraint   The closure that returns a guess
  *                            for a given constraint
  * @return Guess  The guessed value with the highest confidence
  */
 protected function guess($class, $property, \Closure $guessForConstraint)
 {
     $guesses = array();
     $classMetadata = $this->metadataFactory->getClassMetadata($class);
     if ($classMetadata->hasMemberMetadatas($property)) {
         $memberMetadatas = $classMetadata->getMemberMetadatas($property);
         foreach ($memberMetadatas as $memberMetadata) {
             $constraints = $memberMetadata->getConstraints();
             foreach ($constraints as $constraint) {
                 if ($guess = $guessForConstraint($constraint)) {
                     $guesses[] = $guess;
                 }
             }
         }
     }
     return Guess::getBestGuess($guesses);
 }
Пример #6
0
 /**
  * Constructor
  *
  * @param array $options    The options
  * @param int   $confidence The confidence that the guessed options is correct
  */
 public function __construct(array $options, $confidence)
 {
     parent::__construct($confidence);
     $this->options = $options;
 }