Source of file FormTypeRenderer.php

Size: 5,493 Bytes - Last Modified: 2023-11-16T22:56:03+01:00

/home/websites/teicee/packagist/site/phpdoc/conf/../vendor/teicee/make-bundle/src/Renderer/FormTypeRenderer.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
<?php
namespace TIC\MakeBundle\Renderer;

use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\Str;
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
use Symfony\Bundle\MakerBundle\Doctrine\EntityDetails;

/**
 * @internal
 */
final class FormTypeRenderer
{
	protected $skelPath = __DIR__.'/../Resources/skeleton/';

	private $generator;

	public function __construct(Generator $generator)
	{
		$this->generator = $generator;
	}

	public function render(ClassNameDetails $formClassDetails, array $formFields, ClassNameDetails $boundClassDetails = null, array $constraintClasses = [], array $extraUseClasses = []): ?string
	{
		$fieldTypeUseStatements = [];
		$fields = [];
		foreach ($formFields as $name => $fieldTypeOptions) {
			$fieldTypeOptions = $fieldTypeOptions ?? ['type' => null, 'options_code' => null];
			if (isset($fieldTypeOptions['type'])) {
				$fieldTypeUseStatements[] = $fieldTypeOptions['type'];
				$fieldTypeOptions['type'] = Str::getShortClassName($fieldTypeOptions['type']);
			}
			$fields[$name] = $fieldTypeOptions;
		}
		
		$mergedTypeUseStatements = array_unique(array_merge($fieldTypeUseStatements, $extraUseClasses));
		sort($mergedTypeUseStatements);
		
		return $this->generator->generateClass(
			$formClassDetails->getFullName(),
			$this->skelPath . 'form/Type.tpl.php',
			[
#				'ticHelper' => new GeneratorFormHelper(),
				'bounded_full_class_name'   => $boundClassDetails ? $boundClassDetails->getFullName()  : null,
				'bounded_class_name'        => $boundClassDetails ? $boundClassDetails->getShortName() : null,
				'field_type_use_statements' => $mergedTypeUseStatements,
				'constraint_use_statements' => $constraintClasses,
				'form_fields' => $fields,
			]
		);
	}

	/**
	 * Appel de render() en fournissant les $formFields enrichis à partir de $doctrineDetails.
	 * (permet de compléter les données fournies par Symfony\Bundle\MakerBundle\Doctrine->getFormFields())
	 */
#	public function generate(ClassNameDetails $formClassDetails, EntityDetails $doctrineDetails, ClassNameDetails $entityDetails, ClassNameDetails $boundClassDetails = null, array $constraintClasses = [], array $extraUseClasses = [])
	public function generate(ClassNameDetails $formClassDetails, EntityDetails $doctrineDetails, ClassNameDetails $entityDetails, array $constraintClasses = [], array $extraUseClasses = []): ?string
	{
		$fieldMappings = $doctrineDetails->getDisplayFields();
#		dump($fieldMappings);
		
		$fieldsWithTypes = [];
		foreach ($doctrineDetails->getFormFields() as $field => $data) {
			$fieldsWithTypes[$field] = null;
			if (! isset($fieldMappings[$field])) continue;
			
			// mapping attributes : type, nullable, length, scale, precision
			$type = null;
			$opts = array();
			switch ($fieldMappings[$field]['type']) {
				// characters
				case 'string' :
				case 'ascii_string' :
				case 'guid' :
					$type = "TIC\FormBundle\Form\Type\StringType";
					break;
				case 'text' :
					$type = "TIC\FormBundle\Form\Type\EditorType";
					break;
				
				// numbers
				case 'boolean' :
					$type = "TIC\FormBundle\Form\Type\BooleanType";
					break;
				case 'integer' :
				case 'smallint' :
				case 'bigint' :
					$type = "TIC\FormBundle\Form\Type\NumberType";
					break;
				case 'float' :
				case 'decimal' :
					$type = "TIC\FormBundle\Form\Type\NumberType";
					break;
				
				// dates
				case 'datetime' :
				case 'datetimetz' :
					$type = "TIC\FormBundle\Form\Type\DatetimeType";
					break;
				case 'datetime_immutable' :
				case 'datetimetz_immutable' :
					$type = "TIC\FormBundle\Form\Type\DatetimeType";
					$opts[] = $this->renderOptionLine('immutable', 'true');
					break;
				case 'date' :
					$type = "TIC\FormBundle\Form\Type\DateType";
					break;
				case 'date_immutable' :
					$type = "TIC\FormBundle\Form\Type\DateType";
					$opts[] = $this->renderOptionLine('immutable', 'true');
					break;
				case 'time' :
					$type = "TIC\FormBundle\Form\Type\TimeType";
					break;
				case 'time_immutable' :
					$type = "TIC\FormBundle\Form\Type\TimeType";
					$opts[] = $this->renderOptionLine('input', '"datetime_immutable"');
					break;
				case 'dateinterval' :
#					$type = "TIC\FormBundle\Form\Type\DateRangeType";
					break;
				
				// arrays
				case 'array' :
				case 'simple_array' :
				case 'json' :  // deprecated since 2.6
				case 'json_array' :
					$type = "TIC\FormBundle\Form\Type\ChoiceType";
					$opts[] = $this->renderOptionLine('multiple', 'true');
					break;
				
				// objects
				case 'object' :
#					$type = "";
					break;
				
				// binary
				case 'blob' :
				case 'binary' :
#					$type = "TIC\FormBundle\Form\Type\FileType";
					break;
				
				// relations
				case 'ManyToOne' :
				case 'OneToMany' :
				case 'ManyToMany' :
				case 'OneToOne' :
					break;
			}
			
			if (isset($fieldMappings[$field]['nullable']) && $fieldMappings[$field]['nullable']) {
				$opts[] = $this->renderOptionLine('required', 'false');
			}
			
			$fieldsWithTypes[$field] = [
				'type'         => $type,
				'options_code' => implode("\n", $opts),
			];
		}
		
#		$this->render($formClassDetails, $fieldsWithTypes, $boundClassDetails, $constraintClasses, $extraUseClasses);
		return $this->render($formClassDetails, $fieldsWithTypes, $entityDetails, $constraintClasses, $extraUseClasses);
	}

	/**
	 *
	 */
	protected function renderOptionLine(string $key, $val): string
	{
		return sprintf("\t\t\t\t%-20s => %s,", "'".$key."'", $val);
	}

}