Source of file GeneratorTwigHelper.php

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

/home/websites/teicee/packagist/site/phpdoc/conf/../vendor/teicee/make-bundle/src/Helper/GeneratorTwigHelper.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
<?php
namespace TIC\MakeBundle\Helper;

/**
 *
 */
final class GeneratorTwigHelper
{
/*
	private $fileManager;

	public function __construct(FileManager $fileManager)
	{
		$this->fileManager = $fileManager;
	}
*/

	/**
	 *
	 */
	public function getEntityFieldPrintCode($entity, $field): string
	{
		$twigField = preg_replace_callback('/(?!^)_([a-z0-9])/', function ($s) {
			return strtoupper($s[1]);
		}, $field['fieldName']);
		$twigValue = $entity.'.'.str_replace('_', '', $twigField);
		
		$printCode = '';
		switch ($field['type']) {
			// characters
			case 'string' :
			case 'ascii_string' :
			case 'guid' :
				$printCode = $twigValue;
				break;
			case 'text' :
				$printCode = $twigValue;
				break;
			
			// numbers
			case 'boolean' :
#				$printCode = $twigValue.' ? \'Yes\' : \'No\'';
				$printCode = $twigValue.'|bsBoolean';
				break;
			case 'integer' :
			case 'smallint' :
			case 'bigint' :
				$printCode = $twigValue.'|number_format(0,","," ")';
				break;
			case 'float' :
			case 'decimal' :
				$printCode = $twigValue.'|number_format(2,","," ")';
				break;
			
			// dates
			case 'datetimetz' :
			case 'datetimetz_immutable' :
#				$printCode = $twigValue.' ? '.$twigValue.'|date(\'Y-m-d H:i:s T\') : \'\'';
				$printCode = $twigValue.'|ldatetime';
			case 'datetime' :
			case 'datetime_immutable' :
#				$printCode = $twigValue.' ? '.$twigValue.'|date(\'Y-m-d H:i:s\') : \'\'';
				$printCode = $twigValue.'|ldatetime';
				break;
			case 'date' :
			case 'date_immutable' :
#				$printCode = $twigValue.' ? '.$twigValue.'|date(\'Y-m-d\') : \'\'';
				$printCode = $twigValue.'|ldate';
				break;
			case 'time' :
			case 'time_immutable' :
#				$printCode = $twigValue.' ? '.$twigValue.'|date(\'H:i:s\') : \'\'';
				$printCode = $twigValue.'|ltime';
				break;
			case 'dateinterval' :
#				$printCode = $twigValue.' ? '.$twigValue.'.format(\'%y year(s), %m month(s), %d day(s)\') : \'\'';
				$printCode = $twigValue.'|duration';
				break;
			
			// arrays
			case 'array' :
			case 'simple_array' :
				$printCode = $twigValue.' ? '.$twigValue.'|join(\', \') : \'\'';
				break;
			case 'json' :  // deprecated since 2.6
			case 'json_array' :
				$printCode = $twigValue.' ? '.$twigValue.'|json_encode : \'\'';
				break;
			
			// objects
			case 'object' :
				break;
			
			// binary
			case 'blob' :
			case 'binary' :
				break;
			
			// relations
			case 'ManyToOne' :
			case 'OneToMany' :
			case 'ManyToMany' :
			case 'OneToOne' :
				break;
		}
		
#		dump($printCode);
		return $printCode;
	}

/*
    public function getHeadPrintCode($title): string
    {
        if ($this->fileManager->fileExists($this->fileManager->getPathForTemplate('base.html.twig'))) {
            return <<<TWIG
{% extends 'base.html.twig' %}

{% block title %}$title{% endblock %}

TWIG;
        }

        return <<<HTML
<!DOCTYPE html>

<title>$title</title>

HTML;
    }
*/

    public function getFileLink($path, $text = null, $line = 0): string
    {
        $text = $text ?: $path;

        return "<a href=\"{{ '$path'|file_link($line) }}\">$text</a>";
    }
}