summaryrefslogtreecommitdiff
path: root/lib/phptal/PHPTAL/RepeatController.php
blob: 5d33914024665d72274a7e700962b7c400bf4681 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
 * PHPTAL templating engine
 *
 * PHP Version 5
 *
 * @category HTML
 * @package  PHPTAL
 * @author   Laurent Bedubourg <lbedubourg@motion-twin.com>
 * @author   Kornel Lesiński <kornel@aardvarkmedia.co.uk>
 * @author   Iván Montes <drslump@pollinimini.net>
 * @license  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
 * @version  SVN: $Id$
 * @link     http://phptal.org/
 */

/**
 * Stores tal:repeat information during template execution.
 *
 * An instance of this class is created and stored into PHPTAL context on each
 * tal:repeat usage.
 *
 * repeat/item/index
 * repeat/item/number
 * ...
 * are provided by this instance.
 *
 * 'repeat' is an stdClass instance created to handle RepeatControllers,
 * 'item' is an instance of this class.
 *
 * @package PHPTAL
 * @subpackage Php
 * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
 */
class PHPTAL_RepeatController implements Iterator
{
    public $key;
    private $current;
    private $valid;
    private $validOnNext;

    private $uses_groups = false;

    protected $iterator;
    public $index;
    public $end;

    /**
     * computed lazily
     */
    private $length = null;

    /**
     * Construct a new RepeatController.
     *
     * @param $source array, string, iterator, iterable.
     */
    public function __construct($source)
    {
        if ( is_string($source) ) {
            $this->iterator = new ArrayIterator( str_split($source) );  // FIXME: invalid for UTF-8 encoding, use preg_match_all('/./u') trick
        } elseif ( is_array($source) ) {
            $this->iterator = new ArrayIterator($source);
        } elseif ($source instanceof IteratorAggregate) {
            $this->iterator = $source->getIterator();
        } elseif ($source instanceof DOMNodeList) {
            $array = array();
            foreach ($source as $k=>$v) {
                $array[$k] = $v;
            }
            $this->iterator = new ArrayIterator($array);
        } elseif ($source instanceof Iterator) {
            $this->iterator = $source;
        } elseif ($source instanceof Traversable) {
            $this->iterator = new IteratorIterator($source);
        } elseif ($source instanceof Closure) {
            $this->iterator = new ArrayIterator( (array) $source() );
        } elseif ($source instanceof stdClass) {
            $this->iterator = new ArrayIterator( (array) $source );
        } else {
            $this->iterator = new ArrayIterator( array() );
        }
    }

    /**
     * Returns the current element value in the iteration
     *
     * @return Mixed    The current element value
     */
    public function current()
    {
        return $this->current;
    }

    /**
     * Returns the current element key in the iteration
     *
     * @return String/Int   The current element key
     */
    public function key()
    {
        return $this->key;
    }

    /**
     * Tells if the iteration is over
     *
     * @return bool     True if the iteration is not finished yet
     */
    public function valid()
    {
        $valid = $this->valid || $this->validOnNext;
        $this->validOnNext = $this->valid;

        return $valid;
    }

    public function length()
    {
        if ($this->length === null) {
            if ($this->iterator instanceof Countable) {
                return $this->length = count($this->iterator);
            } elseif ( is_object($this->iterator) ) {
                // for backwards compatibility with existing PHPTAL templates
                if ( method_exists($this->iterator, 'size') ) {
                    return $this->length = $this->iterator->size();
                } elseif ( method_exists($this->iterator, 'length') ) {
                    return $this->length = $this->iterator->length();
                }
            }
            $this->length = '_PHPTAL_LENGTH_UNKNOWN_';
        }

        if ($this->length === '_PHPTAL_LENGTH_UNKNOWN_') // return length if end is discovered
        {
            return $this->end ? $this->index + 1 : null;
        }
        return $this->length;
    }

    /**
     * Restarts the iteration process going back to the first element
     *
     */
    public function rewind()
    {
        $this->index = 0;
        $this->length = null;
        $this->end = false;

        $this->iterator->rewind();

        // Prefetch the next element
        if ($this->iterator->valid()) {
            $this->validOnNext = true;
            $this->prefetch();
        } else {
            $this->validOnNext = false;
        }

        if ($this->uses_groups) {
            // Notify the grouping helper of the change
            $this->groups->reset();
        }
    }

    /**
     * Fetches the next element in the iteration and advances the pointer
     *
     */
    public function next()
    {
        $this->index++;

        // Prefetch the next element
        if ($this->validOnNext) $this->prefetch();

        if ($this->uses_groups) {
            // Notify the grouping helper of the change
            $this->groups->reset();
        }
    }

    /**
     * Ensures that $this->groups works.
     *
     * Groups are rarely-used feature, which is why they're lazily loaded.
     */
    private function initializeGroups()
    {
        if (!$this->uses_groups) {
            $this->groups = new PHPTAL_RepeatControllerGroups();
            $this->uses_groups = true;
        }
    }

    /**
     * Gets an object property
     *
     * @return $var  Mixed  The variable value
     */
    public function __get($var)
    {
        switch ($var) {
            case 'number':
                return $this->index + 1;
            case 'start':
                return $this->index === 0;
            case 'even':
                return ($this->index % 2) === 0;
            case 'odd':
                return ($this->index % 2) === 1;
            case 'length':
                return $this->length();
            case 'letter':
                return strtolower( $this->int2letter($this->index+1) );
            case 'Letter':
                return strtoupper( $this->int2letter($this->index+1) );
            case 'roman':
                return strtolower( $this->int2roman($this->index+1) );
            case 'Roman':
                return strtoupper( $this->int2roman($this->index+1) );

            case 'groups':
                $this->initializeGroups();
                return $this->groups;

            case 'first':
                $this->initializeGroups();
                // Compare the current one with the previous in the dictionary
                $res = $this->groups->first($this->current);
                return is_bool($res) ? $res : $this->groups;

            case 'last':
                $this->initializeGroups();
                // Compare the next one with the dictionary
                $res = $this->groups->last( $this->iterator->current() );
                return is_bool($res) ? $res : $this->groups;

            default:
                throw new PHPTAL_VariableNotFoundException("Unable to find part '$var' in repeat variable");
        }
    }

    /**
     * Fetches the next element from the source data store and
     * updates the end flag if needed.
     *
     * @access protected
     */
    protected function prefetch()
    {
        $this->valid = true;
        $this->current = $this->iterator->current();
        $this->key = $this->iterator->key();

        $this->iterator->next();
        if ( !$this->iterator->valid() ) {
            $this->valid = false;
            $this->end = true;
        }
    }

    /**
     * Converts an integer number (1 based) to a sequence of letters
     *
     * @param int $int  The number to convert
     *
     * @return String   The letters equivalent as a, b, c-z ... aa, ab, ac-zz ...
     * @access protected
     */
    protected function int2letter($int)
    {
        $lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $size = strlen($lookup);

        $letters = '';
        while ($int > 0) {
            $int--;
            $letters = $lookup[$int % $size] . $letters;
            $int = floor($int / $size);
        }
        return $letters;
    }

    /**
     * Converts an integer number (1 based) to a roman numeral
     *
     * @param int $int  The number to convert
     *
     * @return String   The roman numeral
     * @access protected
     */
    protected function int2roman($int)
    {
        $lookup = array(
            '1000'  => 'M',
            '900'   => 'CM',
            '500'   => 'D',
            '400'   => 'CD',
            '100'   => 'C',
            '90'    => 'XC',
            '50'    => 'L',
            '40'    => 'XL',
            '10'    => 'X',
            '9'     => 'IX',
            '5'     => 'V',
            '4'     => 'IV',
            '1'     => 'I',
        );

        $roman = '';
        foreach ($lookup as $max => $letters) {
            while ($int >= $max) {
                $roman .= $letters;
                $int -= $max;
            }
        }

        return $roman;
    }
}