2011-08-30 15:17:46 +02:00
|
|
|
<?php
|
|
|
|
|
2011-09-28 17:55:54 +02:00
|
|
|
/**
|
|
|
|
* This file is part of the PropelBundle package.
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*
|
|
|
|
* @license MIT License
|
|
|
|
*/
|
|
|
|
|
2011-08-30 15:17:46 +02:00
|
|
|
namespace Propel\PropelBundle\Form;
|
|
|
|
|
|
|
|
use Symfony\Component\Form\FormTypeGuesserInterface;
|
|
|
|
use Symfony\Component\Form\Guess\Guess;
|
|
|
|
use Symfony\Component\Form\Guess\TypeGuess;
|
|
|
|
use Symfony\Component\Form\Guess\ValueGuess;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Propel Type guesser.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
*/
|
|
|
|
class PropelTypeGuesser implements FormTypeGuesserInterface
|
|
|
|
{
|
|
|
|
private $cache = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function guessType($class, $property)
|
|
|
|
{
|
|
|
|
if (!$table = $this->getTable($class)) {
|
|
|
|
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($table->getRelations() as $relation) {
|
|
|
|
if (in_array($relation->getType(), array(\RelationMap::MANY_TO_ONE, \RelationMap::ONE_TO_MANY))) {
|
|
|
|
if ($property == $relation->getForeignTable()->getName()) {
|
|
|
|
return new TypeGuess('model', array(
|
2011-08-31 07:47:55 +02:00
|
|
|
'class' => $relation->getForeignTable()->getClassName(),
|
2011-08-30 15:17:46 +02:00
|
|
|
'multiple' => \RelationMap::MANY_TO_ONE === $relation->getType() ? false : true,
|
|
|
|
), Guess::HIGH_CONFIDENCE);
|
|
|
|
}
|
2011-09-29 00:01:33 +02:00
|
|
|
} elseif ($relation->getType() === \RelationMap::MANY_TO_MANY) {
|
2011-09-28 13:42:08 +02:00
|
|
|
if (strtolower($property) == strtolower($relation->getPluralName())) {
|
|
|
|
return new TypeGuess('model', array(
|
|
|
|
'class' => $relation->getLocalTable()->getClassName(),
|
|
|
|
'multiple' => true,
|
|
|
|
), Guess::HIGH_CONFIDENCE);
|
|
|
|
}
|
2011-08-30 15:17:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$column = $this->getColumn($class, $property)) {
|
|
|
|
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($column->getType()) {
|
|
|
|
case \PropelColumnTypes::BOOLEAN:
|
|
|
|
case \PropelColumnTypes::BOOLEAN_EMU:
|
|
|
|
return new TypeGuess('checkbox', array(), Guess::HIGH_CONFIDENCE);
|
|
|
|
case \PropelColumnTypes::TIMESTAMP:
|
2011-08-31 07:42:16 +02:00
|
|
|
case \PropelColumnTypes::BU_TIMESTAMP:
|
2011-08-30 15:17:46 +02:00
|
|
|
return new TypeGuess('datetime', array(), Guess::HIGH_CONFIDENCE);
|
|
|
|
case \PropelColumnTypes::DATE:
|
2011-08-31 07:42:16 +02:00
|
|
|
case \PropelColumnTypes::BU_DATE:
|
2011-08-30 15:17:46 +02:00
|
|
|
return new TypeGuess('date', array(), Guess::HIGH_CONFIDENCE);
|
|
|
|
case \PropelColumnTypes::TIME:
|
|
|
|
return new TypeGuess('time', array(), Guess::HIGH_CONFIDENCE);
|
|
|
|
case \PropelColumnTypes::FLOAT:
|
|
|
|
case \PropelColumnTypes::REAL:
|
|
|
|
case \PropelColumnTypes::DOUBLE:
|
|
|
|
case \PropelColumnTypes::DECIMAL:
|
|
|
|
return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE);
|
|
|
|
case \PropelColumnTypes::TINYINT:
|
|
|
|
case \PropelColumnTypes::SMALLINT:
|
|
|
|
case \PropelColumnTypes::INTEGER:
|
|
|
|
case \PropelColumnTypes::BIGINT:
|
|
|
|
case \PropelColumnTypes::NUMERIC:
|
|
|
|
return new TypeGuess('integer', array(), Guess::MEDIUM_CONFIDENCE);
|
|
|
|
case \PropelColumnTypes::CHAR:
|
|
|
|
case \PropelColumnTypes::VARCHAR:
|
|
|
|
return new TypeGuess('text', array(), Guess::MEDIUM_CONFIDENCE);
|
|
|
|
case \PropelColumnTypes::LONGVARCHAR:
|
2011-08-31 07:42:16 +02:00
|
|
|
case \PropelColumnTypes::BLOB:
|
|
|
|
case \PropelColumnTypes::CLOB:
|
|
|
|
case \PropelColumnTypes::CLOB_EMU:
|
2011-08-30 15:17:46 +02:00
|
|
|
return new TypeGuess('textarea', array(), Guess::MEDIUM_CONFIDENCE);
|
|
|
|
default:
|
|
|
|
return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function guessRequired($class, $property)
|
|
|
|
{
|
|
|
|
if ($column = $this->getColumn($class, $property)) {
|
|
|
|
return new ValueGuess($column->isNotNull(), Guess::HIGH_CONFIDENCE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function guessMaxLength($class, $property)
|
|
|
|
{
|
|
|
|
if (($column = $this->getColumn($class, $property)) && $column->isText()) {
|
|
|
|
return new ValueGuess($column->getSize(), Guess::HIGH_CONFIDENCE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function guessMinLength($class, $property)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getTable($class)
|
|
|
|
{
|
|
|
|
if (isset($this->cache[$class])) {
|
|
|
|
return $this->cache[$class];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (class_exists($queryClass = $class.'Query')) {
|
|
|
|
$query = new $queryClass();
|
|
|
|
|
|
|
|
return $this->cache[$class] = $query->getTableMap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getColumn($class, $property)
|
|
|
|
{
|
|
|
|
if (isset($this->cache[$class.'::'.$property])) {
|
|
|
|
return $this->cache[$class.'::'.$property];
|
|
|
|
}
|
|
|
|
|
|
|
|
$table = $this->getTable($class);
|
|
|
|
|
|
|
|
if ($table && $table->hasColumn($property)) {
|
|
|
|
return $this->cache[$class.'::'.$property] = $table->getColumn($property);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|