Вход Регистрация
Файл: symfony-2.7/src/Symfony/Component/Form/Extension/Validator/ValidatorTypeGuesser.php
Строк: 535
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace SymfonyComponentFormExtensionValidator;

use 
SymfonyComponentFormFormTypeGuesserInterface;
use 
SymfonyComponentFormGuessGuess;
use 
SymfonyComponentFormGuessTypeGuess;
use 
SymfonyComponentFormGuessValueGuess;
use 
SymfonyComponentValidatorConstraint;
use 
SymfonyComponentValidatorMappingClassMetadataInterface;
use 
SymfonyComponentValidatorMetadataFactoryInterface;

class 
ValidatorTypeGuesser implements FormTypeGuesserInterface
{
    private 
$metadataFactory;

    public function 
__construct(MetadataFactoryInterface $metadataFactory)
    {
        
$this->metadataFactory $metadataFactory;
    }

    
/**
     * {@inheritdoc}
     */
    
public function guessType($class$property)
    {
        
$guesser $this;

        return 
$this->guess($class$property, function (Constraint $constraint) use ($guesser) {
            return 
$guesser->guessTypeForConstraint($constraint);
        });
    }

    
/**
     * {@inheritdoc}
     */
    
public function guessRequired($class$property)
    {
        
$guesser $this;

        return 
$this->guess($class$property, function (Constraint $constraint) use ($guesser) {
            return 
$guesser->guessRequiredForConstraint($constraint);
        
// If we don't find any constraint telling otherwise, we can assume
        // that a field is not required (with LOW_CONFIDENCE)
        
}, false);
    }

    
/**
     * {@inheritdoc}
     */
    
public function guessMaxLength($class$property)
    {
        
$guesser $this;

        return 
$this->guess($class$property, function (Constraint $constraint) use ($guesser) {
            return 
$guesser->guessMaxLengthForConstraint($constraint);
        });
    }

    
/**
     * {@inheritdoc}
     */
    
public function guessPattern($class$property)
    {
        
$guesser $this;

        return 
$this->guess($class$property, function (Constraint $constraint) use ($guesser) {
            return 
$guesser->guessPatternForConstraint($constraint);
        });
    }

    
/**
     * Guesses a field class name for a given constraint.
     *
     * @param Constraint $constraint The constraint to guess for
     *
     * @return TypeGuess|null The guessed field class and options
     */
    
public function guessTypeForConstraint(Constraint $constraint)
    {
        switch (
get_class($constraint)) {
            case 
'SymfonyComponentValidatorConstraintsType':
                switch (
$constraint->type) {
                    case 
'array':
                        return new 
TypeGuess('collection', array(), Guess::MEDIUM_CONFIDENCE);
                    case 
'boolean':
                    case 
'bool':
                        return new 
TypeGuess('checkbox', array(), Guess::MEDIUM_CONFIDENCE);

                    case 
'double':
                    case 
'float':
                    case 
'numeric':
                    case 
'real':
                        return new 
TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE);

                    case 
'integer':
                    case 
'int':
                    case 
'long':
                        return new 
TypeGuess('integer', array(), Guess::MEDIUM_CONFIDENCE);

                    case 
'DateTime':
                        return new 
TypeGuess('date', array(), Guess::MEDIUM_CONFIDENCE);

                    case 
'string':
                        return new 
TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
                }
                break;

            case 
'SymfonyComponentValidatorConstraintsCountry':
                return new 
TypeGuess('country', array(), Guess::HIGH_CONFIDENCE);

            case 
'SymfonyComponentValidatorConstraintsDate':
                return new 
TypeGuess('date', array('input' => 'string'), Guess::HIGH_CONFIDENCE);

            case 
'SymfonyComponentValidatorConstraintsDateTime':
                return new 
TypeGuess('datetime', array('input' => 'string'), Guess::HIGH_CONFIDENCE);

            case 
'SymfonyComponentValidatorConstraintsEmail':
                return new 
TypeGuess('email', array(), Guess::HIGH_CONFIDENCE);

            case 
'SymfonyComponentValidatorConstraintsFile':
            case 
'SymfonyComponentValidatorConstraintsImage':
                return new 
TypeGuess('file', array(), Guess::HIGH_CONFIDENCE);

            case 
'SymfonyComponentValidatorConstraintsLanguage':
                return new 
TypeGuess('language', array(), Guess::HIGH_CONFIDENCE);

            case 
'SymfonyComponentValidatorConstraintsLocale':
                return new 
TypeGuess('locale', array(), Guess::HIGH_CONFIDENCE);

            case 
'SymfonyComponentValidatorConstraintsTime':
                return new 
TypeGuess('time', array('input' => 'string'), Guess::HIGH_CONFIDENCE);

            case 
'SymfonyComponentValidatorConstraintsUrl':
                return new 
TypeGuess('url', array(), Guess::HIGH_CONFIDENCE);

            case 
'SymfonyComponentValidatorConstraintsIp':
                return new 
TypeGuess('text', array(), Guess::MEDIUM_CONFIDENCE);

            case 
'SymfonyComponentValidatorConstraintsLength':
            case 
'SymfonyComponentValidatorConstraintsRegex':
                return new 
TypeGuess('text', array(), Guess::LOW_CONFIDENCE);

            case 
'SymfonyComponentValidatorConstraintsRange':
                return new 
TypeGuess('number', array(), Guess::LOW_CONFIDENCE);

            case 
'SymfonyComponentValidatorConstraintsCount':
                return new 
TypeGuess('collection', array(), Guess::LOW_CONFIDENCE);

            case 
'SymfonyComponentValidatorConstraintsTrue':
            case 
'SymfonyComponentValidatorConstraintsFalse':
                return new 
TypeGuess('checkbox', array(), Guess::MEDIUM_CONFIDENCE);
        }
    }

    
/**
     * Guesses whether a field is required based on the given constraint.
     *
     * @param Constraint $constraint The constraint to guess for
     *
     * @return ValueGuess|null The guess whether the field is required
     */
    
public function guessRequiredForConstraint(Constraint $constraint)
    {
        switch (
get_class($constraint)) {
            case 
'SymfonyComponentValidatorConstraintsNotNull':
            case 
'SymfonyComponentValidatorConstraintsNotBlank':
            case 
'SymfonyComponentValidatorConstraintsTrue':
                return new 
ValueGuess(trueGuess::HIGH_CONFIDENCE);
        }
    }

    
/**
     * Guesses a field's maximum length based on the given constraint.
     *
     * @param Constraint $constraint The constraint to guess for
     *
     * @return ValueGuess|null The guess for the maximum length
     */
    
public function guessMaxLengthForConstraint(Constraint $constraint)
    {
        switch (
get_class($constraint)) {
            case 
'SymfonyComponentValidatorConstraintsLength':
                if (
is_numeric($constraint->max)) {
                    return new 
ValueGuess($constraint->maxGuess::HIGH_CONFIDENCE);
                }
                break;

            case 
'SymfonyComponentValidatorConstraintsType':
                if (
in_array($constraint->type, array('double''float''numeric''real'))) {
                    return new 
ValueGuess(nullGuess::MEDIUM_CONFIDENCE);
                }
                break;

            case 
'SymfonyComponentValidatorConstraintsRange':
                if (
is_numeric($constraint->max)) {
                    return new 
ValueGuess(strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
                }
                break;
        }
    }

    
/**
     * Guesses a field's pattern based on the given constraint.
     *
     * @param Constraint $constraint The constraint to guess for
     *
     * @return ValueGuess|null The guess for the pattern
     */
    
public function guessPatternForConstraint(Constraint $constraint)
    {
        switch (
get_class($constraint)) {
            case 
'SymfonyComponentValidatorConstraintsLength':
                if (
is_numeric($constraint->min)) {
                    return new 
ValueGuess(sprintf('.{%s,}', (string) $constraint->min), Guess::LOW_CONFIDENCE);
                }
                break;

            case 
'SymfonyComponentValidatorConstraintsRegex':
                
$htmlPattern $constraint->getHtmlPattern();

                if (
null !== $htmlPattern) {
                    return new 
ValueGuess($htmlPatternGuess::HIGH_CONFIDENCE);
                }
                break;

            case 
'SymfonyComponentValidatorConstraintsRange':
                if (
is_numeric($constraint->min)) {
                    return new 
ValueGuess(sprintf('.{%s,}'strlen((string) $constraint->min)), Guess::LOW_CONFIDENCE);
                }
                break;

            case 
'SymfonyComponentValidatorConstraintsType':
                if (
in_array($constraint->type, array('double''float''numeric''real'))) {
                    return new 
ValueGuess(nullGuess::MEDIUM_CONFIDENCE);
                }
                break;
        }
    }

    
/**
     * 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$propertyClosure $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($defaultValueGuess::LOW_CONFIDENCE);
        }

        return 
Guess::getBestGuess($guesses);
    }
}
Онлайн: 2
Реклама