1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Initiates a counter that is incremented every time you call it |
---|
5 | * <pre> |
---|
6 | * * name : the counter name, define it if you want to have multiple concurrent counters |
---|
7 | * * start : the start value, if it's set, it will reset the counter to this value, defaults to 1 |
---|
8 | * * skip : the value to add to the counter at each call, defaults to 1 |
---|
9 | * * direction : "up" (default) or "down" to define whether the counter increments or decrements |
---|
10 | * * print : if false, the counter will not output the current count, defaults to true |
---|
11 | * * assign : if set, the counter is saved into the given variable and does not output anything, overriding the print parameter |
---|
12 | * </pre> |
---|
13 | * This software is provided 'as-is', without any express or implied warranty. |
---|
14 | * In no event will the authors be held liable for any damages arising from the use of this software. |
---|
15 | * |
---|
16 | * @author Jordi Boggiano <j.boggiano@seld.be> |
---|
17 | * @copyright Copyright (c) 2008, Jordi Boggiano |
---|
18 | * @license http://dwoo.org/LICENSE Modified BSD License |
---|
19 | * @link http://dwoo.org/ |
---|
20 | * @version 1.0.0 |
---|
21 | * @date 2008-10-23 |
---|
22 | * @package Dwoo |
---|
23 | */ |
---|
24 | class Dwoo_Plugin_counter extends Dwoo_Plugin |
---|
25 | { |
---|
26 | protected $counters = array(); |
---|
27 | |
---|
28 | public function process($name = 'default', $start = null, $skip = null, $direction = null, $print = null, $assign = null) |
---|
29 | { |
---|
30 | // init counter |
---|
31 | if (!isset($this->counters[$name])) { |
---|
32 | $this->counters[$name] = array |
---|
33 | ( |
---|
34 | 'count' => $start===null ? 1 : (int) $start, |
---|
35 | 'skip' => $skip===null ? 1 : (int) $skip, |
---|
36 | 'print' => $print===null ? true : (bool) $print, |
---|
37 | 'assign' => $assign===null ? null : (string) $assign, |
---|
38 | 'direction' => strtolower($direction)==='down' ? -1 : 1, |
---|
39 | ); |
---|
40 | } |
---|
41 | // increment |
---|
42 | else |
---|
43 | { |
---|
44 | // override setting if present |
---|
45 | if ($skip !== null) { |
---|
46 | $this->counters[$name]['skip'] = (int) $skip; |
---|
47 | } |
---|
48 | |
---|
49 | if ($direction !== null) { |
---|
50 | $this->counters[$name]['direction'] = strtolower($direction)==='down' ? -1 : 1; |
---|
51 | } |
---|
52 | |
---|
53 | if ($print !== null) { |
---|
54 | $this->counters[$name]['print'] = (bool) $print; |
---|
55 | } |
---|
56 | |
---|
57 | if ($assign !== null) { |
---|
58 | $this->counters[$name]['assign'] = (string) $assign; |
---|
59 | } |
---|
60 | |
---|
61 | if ($start !== null) { |
---|
62 | $this->counters[$name]['count'] = (int) $start; |
---|
63 | } else { |
---|
64 | $this->counters[$name]['count'] += ($this->counters[$name]['skip'] * $this->counters[$name]['direction']); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | $out = $this->counters[$name]['count']; |
---|
69 | |
---|
70 | if ($this->counters[$name]['assign'] !== null) { |
---|
71 | $this->dwoo->assignInScope($out, $this->counters[$name]['assign']); |
---|
72 | } elseif ($this->counters[$name]['print'] === true) { |
---|
73 | return $out; |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|