]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/sysplugins/smarty_internal_compile_for.php
Merge pull request #4233 from MrPetovan/task/4116-move-smarty-to-composer
[friendica.git] / vendor / smarty / smarty / libs / sysplugins / smarty_internal_compile_for.php
1 <?php
2 /**
3  * Smarty Internal Plugin Compile For
4  * Compiles the {for} {forelse} {/for} tags
5  *
6  * @package    Smarty
7  * @subpackage Compiler
8  * @author     Uwe Tews
9  */
10
11 /**
12  * Smarty Internal Plugin Compile For Class
13  *
14  * @package    Smarty
15  * @subpackage Compiler
16  */
17 class Smarty_Internal_Compile_For extends Smarty_Internal_CompileBase
18 {
19     /**
20      * Compiles code for the {for} tag
21      * Smarty 3 does implement two different syntax's:
22      * - {for $var in $array}
23      * For looping over arrays or iterators
24      * - {for $x=0; $x<$y; $x++}
25      * For general loops
26      * The parser is generating different sets of attribute by which this compiler can
27      * determine which syntax is used.
28      *
29      * @param  array  $args      array with attributes from parser
30      * @param  object $compiler  compiler object
31      * @param  array  $parameter array with compilation parameter
32      *
33      * @return string compiled code
34      */
35     public function compile($args, $compiler, $parameter)
36     {
37         $compiler->loopNesting ++;
38         if ($parameter == 0) {
39             $this->required_attributes = array('start', 'to');
40             $this->optional_attributes = array('max', 'step');
41         } else {
42             $this->required_attributes = array('start', 'ifexp', 'var', 'step');
43             $this->optional_attributes = array();
44         }
45         $this->mapCache = array();
46         // check and get attributes
47         $_attr = $this->getAttributes($compiler, $args);
48
49         $output = "<?php\n";
50         if ($parameter == 1) {
51             foreach ($_attr[ 'start' ] as $_statement) {
52                 if (is_array($_statement[ 'var' ])) {
53                     $var = $_statement[ 'var' ][ 'var' ];
54                     $index = $_statement[ 'var' ][ 'smarty_internal_index' ];
55                 } else {
56                     $var = $_statement[ 'var' ];
57                     $index = '';
58                 }
59                 $output .= "\$_smarty_tpl->tpl_vars[$var] = new Smarty_Variable(null, \$_smarty_tpl->isRenderingCache);\n";
60                 $output .= "\$_smarty_tpl->tpl_vars[$var]->value{$index} = {$_statement['value']};\n";
61             }
62             if (is_array($_attr[ 'var' ])) {
63                 $var = $_attr[ 'var' ][ 'var' ];
64                 $index = $_attr[ 'var' ][ 'smarty_internal_index' ];
65             } else {
66                 $var = $_attr[ 'var' ];
67                 $index = '';
68             }
69             $output .= "if ($_attr[ifexp]) {\nfor (\$_foo=true;$_attr[ifexp]; \$_smarty_tpl->tpl_vars[$var]->value{$index}$_attr[step]) {\n";
70         } else {
71             $_statement = $_attr[ 'start' ];
72             if (is_array($_statement[ 'var' ])) {
73                 $var = $_statement[ 'var' ][ 'var' ];
74                 $index = $_statement[ 'var' ][ 'smarty_internal_index' ];
75             } else {
76                 $var = $_statement[ 'var' ];
77                 $index = '';
78             }
79             $output .= "\$_smarty_tpl->tpl_vars[$var] = new Smarty_Variable(null, \$_smarty_tpl->isRenderingCache);";
80             if (isset($_attr[ 'step' ])) {
81                 $output .= "\$_smarty_tpl->tpl_vars[$var]->step = $_attr[step];";
82             } else {
83                 $output .= "\$_smarty_tpl->tpl_vars[$var]->step = 1;";
84             }
85             if (isset($_attr[ 'max' ])) {
86                 $output .= "\$_smarty_tpl->tpl_vars[$var]->total = (int) min(ceil((\$_smarty_tpl->tpl_vars[$var]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$var]->step)),$_attr[max]);\n";
87             } else {
88                 $output .= "\$_smarty_tpl->tpl_vars[$var]->total = (int) ceil((\$_smarty_tpl->tpl_vars[$var]->step > 0 ? $_attr[to]+1 - ($_statement[value]) : $_statement[value]-($_attr[to])+1)/abs(\$_smarty_tpl->tpl_vars[$var]->step));\n";
89             }
90             $output .= "if (\$_smarty_tpl->tpl_vars[$var]->total > 0) {\n";
91             $output .= "for (\$_smarty_tpl->tpl_vars[$var]->value{$index} = $_statement[value], \$_smarty_tpl->tpl_vars[$var]->iteration = 1;\$_smarty_tpl->tpl_vars[$var]->iteration <= \$_smarty_tpl->tpl_vars[$var]->total;\$_smarty_tpl->tpl_vars[$var]->value{$index} += \$_smarty_tpl->tpl_vars[$var]->step, \$_smarty_tpl->tpl_vars[$var]->iteration++) {\n";
92             $output .= "\$_smarty_tpl->tpl_vars[$var]->first = \$_smarty_tpl->tpl_vars[$var]->iteration == 1;";
93             $output .= "\$_smarty_tpl->tpl_vars[$var]->last = \$_smarty_tpl->tpl_vars[$var]->iteration == \$_smarty_tpl->tpl_vars[$var]->total;";
94         }
95         $output .= "?>";
96
97         $this->openTag($compiler, 'for', array('for', $compiler->nocache));
98         // maybe nocache because of nocache variables
99         $compiler->nocache = $compiler->nocache | $compiler->tag_nocache;
100         // return compiled code
101         return $output;
102     }
103 }
104
105 /**
106  * Smarty Internal Plugin Compile Forelse Class
107  *
108  * @package    Smarty
109  * @subpackage Compiler
110  */
111 class Smarty_Internal_Compile_Forelse extends Smarty_Internal_CompileBase
112 {
113     /**
114      * Compiles code for the {forelse} tag
115      *
116      * @param  array  $args      array with attributes from parser
117      * @param  object $compiler  compiler object
118      * @param  array  $parameter array with compilation parameter
119      *
120      * @return string compiled code
121      */
122     public function compile($args, $compiler, $parameter)
123     {
124         // check and get attributes
125         $_attr = $this->getAttributes($compiler, $args);
126
127         list($openTag, $nocache) = $this->closeTag($compiler, array('for'));
128         $this->openTag($compiler, 'forelse', array('forelse', $nocache));
129
130         return "<?php }} else { ?>";
131     }
132 }
133
134 /**
135  * Smarty Internal Plugin Compile Forclose Class
136  *
137  * @package    Smarty
138  * @subpackage Compiler
139  */
140 class Smarty_Internal_Compile_Forclose extends Smarty_Internal_CompileBase
141 {
142     /**
143      * Compiles code for the {/for} tag
144      *
145      * @param  array  $args      array with attributes from parser
146      * @param  object $compiler  compiler object
147      * @param  array  $parameter array with compilation parameter
148      *
149      * @return string compiled code
150      */
151     public function compile($args, $compiler, $parameter)
152     {
153         $compiler->loopNesting --;
154         // check and get attributes
155         $_attr = $this->getAttributes($compiler, $args);
156         // must endblock be nocache?
157         if ($compiler->nocache) {
158             $compiler->tag_nocache = true;
159         }
160
161         list($openTag, $compiler->nocache) = $this->closeTag($compiler, array('for', 'forelse'));
162
163         $output = "<?php }\n";
164         if ($openTag != 'forelse') {
165             $output .= "}\n";
166         }
167         $output .= "?>\n";
168         return $output;
169     }
170 }