This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Guesser;
use Sonata\AdminBundle\Model\ModelManagerInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Guess\Guess;
/**
* The code is based on Symfony2 Form Components.
*
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
class TypeGuesserChain implements TypeGuesserInterface
{
/**
* @var array
*/
protected $guessers = [];
/**
* @param array $guessers
*/
public function __construct(array $guessers)
{
foreach ($guessers as $guesser) {
if (!$guesser instanceof TypeGuesserInterface) {
throw new UnexpectedTypeException($guesser, 'Sonata\AdminBundle\Guesser\TypeGuesserInterface');
}
if ($guesser instanceof self) {
$this->guessers = array_merge($this->guessers, $guesser->guessers);
} else {
$this->guessers[] = $guesser;
}
}
}
/**
* {@inheritdoc}
*/
public function guessType($class, $property, ModelManagerInterface $modelManager)
{
return $this->guess(function ($guesser) use ($class, $property, $modelManager) {
return $guesser->guessType($class, $property, $modelManager);
});
}
/**
* 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 Guess The guess with the highest confidence
*/
private function guess(\Closure $closure)
{
$guesses = [];
foreach ($this->guessers as $guesser) {
if ($guess = $closure($guesser)) {
$guesses[] = $guess;
}
}
return Guess::getBestGuess($guesses);
}
}

View File

@@ -0,0 +1,29 @@
<?php
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Guesser;
use Sonata\AdminBundle\Model\ModelManagerInterface;
/**
* @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
*/
interface TypeGuesserInterface
{
/**
* @param string $class
* @param string $property
* @param ModelManagerInterface $modelManager
*
* @return mixed
*/
public function guessType($class, $property, ModelManagerInterface $modelManager);
}