1: <?php
2:
3: namespace Deimos\Flow\Extension\TSimple;
4:
5: use Deimos\Flow\FlowFunction;
6:
7: class TVariable extends FlowFunction
8: {
9:
10: 11: 12:
13: protected $attributes = [];
14:
15: 16: 17:
18: protected function callback()
19: {
20: $callback = '';
21: $isCallback = array_shift($this->data) === '|';
22: $isAttribute = false;
23: $iteration = 0;
24:
25: foreach ($this->data as $dataValue)
26: {
27: if ($isCallback)
28: {
29:
30: if ($dataValue === ':')
31: {
32: $isAttribute = true;
33: $isCallback = false;
34: continue;
35: }
36:
37: $callback .= $dataValue;
38: continue;
39: }
40: else if ($isAttribute)
41: {
42: if ($dataValue === ':')
43: {
44: $iteration++;
45: continue;
46: }
47:
48: if (empty($this->attributes[$iteration]))
49: {
50: $this->attributes[$iteration] = '';
51: }
52:
53: $this->attributes[$iteration] .= $dataValue;
54: }
55:
56: $isCallback = $dataValue === '|';
57: }
58:
59: return $callback;
60: }
61:
62: public function view()
63: {
64:
65: $data = implode($this->data);
66: preg_match('~^(?<variable>' . self::REGEXP_VARIABLE . ')~', $data, $variable);
67:
68: if (empty($variable['variable']))
69: {
70: $variable = array_shift($this->data);
71: }
72: else
73: {
74: $variable = $variable['variable'];
75: }
76:
77: $variable = $this->variable($variable);
78:
79: $callback = $this->callback();
80:
81: $isDefault = $callback === 'default';
82:
83: if (empty($callback) || $isDefault)
84: {
85: $callback = 'escape';
86: }
87:
88: if ($isDefault && $variable{0} === '$')
89: {
90: $storage = sprintf(
91: '(empty(%s)?$this->configure->di()->escape(%s):$this->configure->di()->escape(%s))',
92: $variable,
93: array_shift($this->attributes),
94: $variable
95: );
96: }
97: else
98: {
99: $storage = '$this->configure->di()->call(\'' . $callback . '\', ';
100: $export = var_export(array_merge([$variable], $this->attributes), true);
101: $regExp = sprintf('~\'(%s)\'~', self::REGEXP_VARIABLE);
102: $export = preg_replace($regExp, '$1', $export);
103: $export = preg_replace('~\'(-?[\d\.]+)\'~', '$1', $export);
104: $storage .= str_replace(["\n", "\r"], '', $export) . ')';
105: }
106:
107: return '<?php echo ' . $storage . '; ?>';
108: }
109:
110: }