1: <?php
2: namespace TIC\MakeBundle\Helper;
3:
4: /**
5: *
6: */
7: final class GeneratorTwigHelper
8: {
9: /*
10: private $fileManager;
11:
12: public function __construct(FileManager $fileManager)
13: {
14: $this->fileManager = $fileManager;
15: }
16: */
17:
18: /**
19: *
20: */
21: public function getEntityFieldPrintCode($entity, $field): string
22: {
23: $twigField = preg_replace_callback('/(?!^)_([a-z0-9])/', function ($s) {
24: return strtoupper($s[1]);
25: }, $field['fieldName']);
26: $twigValue = $entity.'.'.str_replace('_', '', $twigField);
27:
28: $printCode = '';
29: switch ($field['type']) {
30: // characters
31: case 'string' :
32: case 'ascii_string' :
33: case 'guid' :
34: $printCode = $twigValue;
35: break;
36: case 'text' :
37: $printCode = $twigValue;
38: break;
39:
40: // numbers
41: case 'boolean' :
42: # $printCode = $twigValue.' ? \'Yes\' : \'No\'';
43: $printCode = $twigValue.'|bsBoolean';
44: break;
45: case 'integer' :
46: case 'smallint' :
47: case 'bigint' :
48: $printCode = $twigValue.'|number_format(0,","," ")';
49: break;
50: case 'float' :
51: case 'decimal' :
52: $printCode = $twigValue.'|number_format(2,","," ")';
53: break;
54:
55: // dates
56: case 'datetimetz' :
57: case 'datetimetz_immutable' :
58: # $printCode = $twigValue.' ? '.$twigValue.'|date(\'Y-m-d H:i:s T\') : \'\'';
59: $printCode = $twigValue.'|ldatetime';
60: case 'datetime' :
61: case 'datetime_immutable' :
62: # $printCode = $twigValue.' ? '.$twigValue.'|date(\'Y-m-d H:i:s\') : \'\'';
63: $printCode = $twigValue.'|ldatetime';
64: break;
65: case 'date' :
66: case 'date_immutable' :
67: # $printCode = $twigValue.' ? '.$twigValue.'|date(\'Y-m-d\') : \'\'';
68: $printCode = $twigValue.'|ldate';
69: break;
70: case 'time' :
71: case 'time_immutable' :
72: # $printCode = $twigValue.' ? '.$twigValue.'|date(\'H:i:s\') : \'\'';
73: $printCode = $twigValue.'|ltime';
74: break;
75: case 'dateinterval' :
76: # $printCode = $twigValue.' ? '.$twigValue.'.format(\'%y year(s), %m month(s), %d day(s)\') : \'\'';
77: $printCode = $twigValue.'|duration';
78: break;
79:
80: // arrays
81: case 'array' :
82: case 'simple_array' :
83: $printCode = $twigValue.' ? '.$twigValue.'|join(\', \') : \'\'';
84: break;
85: case 'json' : // deprecated since 2.6
86: case 'json_array' :
87: $printCode = $twigValue.' ? '.$twigValue.'|json_encode : \'\'';
88: break;
89:
90: // objects
91: case 'object' :
92: break;
93:
94: // binary
95: case 'blob' :
96: case 'binary' :
97: break;
98:
99: // relations
100: case 'ManyToOne' :
101: case 'OneToMany' :
102: case 'ManyToMany' :
103: case 'OneToOne' :
104: break;
105: }
106:
107: # dump($printCode);
108: return $printCode;
109: }
110:
111: /*
112: public function getHeadPrintCode($title): string
113: {
114: if ($this->fileManager->fileExists($this->fileManager->getPathForTemplate('base.html.twig'))) {
115: return <<<TWIG
116: {% extends 'base.html.twig' %}
117:
118: {% block title %}$title{% endblock %}
119:
120: TWIG;
121: }
122:
123: return <<<HTML
124: <!DOCTYPE html>
125:
126: <title>$title</title>
127:
128: HTML;
129: }
130: */
131:
132: public function getFileLink($path, $text = null, $line = 0): string
133: {
134: $text = $text ?: $path;
135:
136: return "<a href=\"{{ '$path'|file_link($line) }}\">$text</a>";
137: }
138: }
139: