public function testGetBestGuessReturnsGuessWithHighestConfidence() { $guess1 = new FieldFactoryGuess('foo', FieldFactoryGuess::MEDIUM_CONFIDENCE); $guess2 = new FieldFactoryGuess('bar', FieldFactoryGuess::LOW_CONFIDENCE); $guess3 = new FieldFactoryGuess('baz', FieldFactoryGuess::HIGH_CONFIDENCE); $this->assertEquals($guess3, FieldFactoryGuess::getBestGuess(array($guess1, $guess2, $guess3))); }
/** * Iterates over the constraints of a property, executes a constraints on * them and returns the best guess * * @param object $object The object 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 FieldFactoryGuess The guessed value with the highest confidence */ protected function guess($object, $property, \Closure $guessForConstraint) { $guesses = array(); $classMetadata = $this->metadataFactory->getClassMetadata(get_class($object)); $memberMetadatas = $classMetadata->getMemberMetadatas($property); foreach ($memberMetadatas as $memberMetadata) { $constraints = $memberMetadata->getConstraints(); foreach ($constraints as $constraint) { if ($guess = $guessForConstraint($constraint)) { $guesses[] = $guess; } } } return FieldFactoryGuess::getBestGuess($guesses); }
/** * 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 FieldFactoryGuess * instance * @return FieldFactoryGuess The guess with the highest confidence */ protected function guess(\Closure $closure) { $guesses = array(); foreach ($this->guessers as $guesser) { if ($guess = $closure($guesser)) { $guesses[] = $guess; } } return FieldFactoryGuess::getBestGuess($guesses); }