Rev 10 | Rev 12 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
7 | PointedEar | 1 | <?php |
2 | |||
3 | /** |
||
4 | * A footnote list contains {@link #Footnote Footnotes} |
||
5 | * |
||
6 | * @author Thomas 'PointedEars' Lahn <php@PointedEars.de> |
||
7 | */ |
||
8 | class FootnoteList |
||
9 | { |
||
10 | /** |
||
11 | * The footnotes of this list |
||
12 | * |
||
13 | * @var Array |
||
14 | */ |
||
15 | protected $footnotes; |
||
16 | |||
17 | /** |
||
18 | * Last used number sign for a footnote |
||
19 | * |
||
20 | * @var int |
||
21 | */ |
||
22 | protected $lastNumberSign; |
||
23 | |||
24 | public function __construct() |
||
25 | { |
||
26 | $this->clear(); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Clears the footnote list |
||
31 | */ |
||
32 | public function clear() |
||
33 | { |
||
34 | $this->footnotes = array(); |
||
35 | $this->lastNumberSign = 0; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Adds a footnote to the list (unless already specified) |
||
40 | * |
||
41 | * @param string $name |
||
42 | * Name of the footnote |
||
43 | * @param string $sign |
||
44 | * Sign of the footnote. If empty, the next available number is used. |
||
45 | * @param string $text |
||
46 | * Text for the footnote |
||
47 | * @return string |
||
48 | * The code for printing the footnote reference. |
||
49 | */ |
||
50 | public function add($name, $sign = '', $text = '') |
||
51 | { |
||
52 | $footnotes =& $this->footnotes; |
||
53 | |||
54 | if (!isset($footnotes[$name])) |
||
55 | { |
||
56 | if (!$sign) |
||
57 | { |
||
58 | $sign = ++$this->lastNumberSign; |
||
59 | } |
||
11 | PointedEar | 60 | else if (is_int($sign)) |
61 | { |
||
62 | $this->lastNumberSign = $sign + 1; |
||
63 | } |
||
8 | PointedEar | 64 | |
65 | $footnotes[$name] = new Footnote($name, $sign, $text); |
||
7 | PointedEar | 66 | } |
67 | |||
8 | PointedEar | 68 | return $footnotes[$name]->getRef(); |
7 | PointedEar | 69 | } |
70 | |||
71 | /** |
||
72 | * Prints the list of footnotes |
||
73 | */ |
||
74 | public function printMe() |
||
75 | { |
||
76 | $footnotes =& $this->footnotes; |
||
77 | |||
8 | PointedEar | 78 | function cmp($a, $b) |
79 | { |
||
80 | if ($a->sign < $b->sign) |
||
81 | { |
||
82 | return -1; |
||
83 | } |
||
84 | else if ($a->sign > $b->sign) |
||
85 | { |
||
86 | return 1; |
||
87 | } |
||
88 | else |
||
89 | { |
||
90 | return 0; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | uasort($footnotes, 'cmp'); |
||
95 | |||
96 | ?><table class="footnotes"> |
||
97 | <?php |
||
98 | |||
7 | PointedEar | 99 | foreach ($footnotes as $name => &$footnote) |
100 | { |
||
101 | $footnote->printMe(); |
||
102 | } |
||
8 | PointedEar | 103 | |
104 | ?></table><?php |
||
7 | PointedEar | 105 | } |
106 | |||
107 | /** |
||
108 | * Prints the list of footnotes and clears the list in memory |
||
109 | */ |
||
110 | public function flush() |
||
111 | { |
||
112 | $this->printMe(); |
||
113 | $this->clear(); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * A footnote to be used in a {@link #FootnoteList "footnote list"} |
||
119 | * |
||
120 | * @author Thomas 'PointedEars' Lahn <php@PointedEars.de> |
||
121 | */ |
||
122 | class Footnote |
||
123 | { |
||
124 | /** |
||
8 | PointedEar | 125 | * The name of this footnote |
126 | * |
||
127 | * @var string |
||
128 | */ |
||
129 | protected $name = ''; |
||
130 | |||
131 | /** |
||
7 | PointedEar | 132 | * The sign used for referring to this footnote |
133 | * |
||
134 | * @var string |
||
135 | */ |
||
136 | protected $sign = ''; |
||
137 | |||
138 | /** |
||
139 | * The text for this footnote |
||
140 | * |
||
141 | * @var string |
||
142 | */ |
||
143 | protected $text = ''; |
||
144 | |||
145 | /** |
||
8 | PointedEar | 146 | * The number of times this footnote has been referred |
147 | * |
||
148 | * @var int |
||
149 | */ |
||
150 | protected $references = 0; |
||
151 | |||
152 | /** |
||
7 | PointedEar | 153 | * Creates a footnote |
154 | * |
||
8 | PointedEar | 155 | * @param string $name |
156 | * The name of this footnote |
||
7 | PointedEar | 157 | * @param string $sign |
158 | * The sign that should be used for referring to this footnote |
||
159 | * @param string $text |
||
160 | * The text for this footnote |
||
161 | */ |
||
8 | PointedEar | 162 | public function __construct($name, $sign, $text) |
7 | PointedEar | 163 | { |
8 | PointedEar | 164 | $this->name = $name; |
7 | PointedEar | 165 | $this->sign = $sign; |
166 | $this->text = $text; |
||
167 | } |
||
8 | PointedEar | 168 | |
169 | /** |
||
170 | * Universal getter |
||
171 | * |
||
172 | * @param string $name |
||
173 | * Name of the property to be read-accessed. Currently only 'sign' |
||
174 | * is supported. |
||
10 | PointedEar | 175 | * @throws InvalidArgumentException if a non-existing property is accessed |
8 | PointedEar | 176 | * @return mixed |
177 | * Property value |
||
178 | */ |
||
179 | public function __get($name) |
||
180 | { |
||
181 | if ($name === 'sign') |
||
182 | { |
||
183 | return $this->sign; |
||
184 | } |
||
185 | else |
||
186 | { |
||
10 | PointedEar | 187 | throw new InvalidArgumentException( |
188 | 'No such property ' . get_class($this) . "::\$$name"); |
||
8 | PointedEar | 189 | } |
190 | } |
||
7 | PointedEar | 191 | |
192 | /** |
||
193 | * Returns the reference for this footnote |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
8 | PointedEar | 197 | public function getRef() |
7 | PointedEar | 198 | { |
8 | PointedEar | 199 | $s = $this->name; |
200 | |||
201 | $ret = "<sup><a href='#footnote-{$s}'" |
||
202 | . ($this->references === 0 |
||
203 | ? " name='fn-{$s}-ref' id='fn-{$s}-ref'" |
||
204 | : '') |
||
205 | . " class='footnote'>{$this->sign}</a></sup>"; |
||
206 | |||
207 | ++$this->references; |
||
208 | |||
209 | return $ret; |
||
7 | PointedEar | 210 | } |
211 | |||
212 | /** |
||
213 | * Prints this footnote in a footnote list |
||
214 | */ |
||
215 | public function printMe() |
||
216 | { |
||
8 | PointedEar | 217 | $s = $this->name; |
218 | |||
219 | echo " <tr> |
||
220 | <th><sup><a name='footnote-{$s}' id='footnote-{$s}' class='footnote' |
||
221 | >{$this->sign}</a></sup><a href='#fn-{$s}-ref' name='footnote-{$s}' |
||
222 | id='footnote-{$s}' class='backref'>↑</a></th> |
||
223 | <td>{$this->text}</td> |
||
224 | </tr> |
||
225 | "; |
||
7 | PointedEar | 226 | } |
227 | } |
||
228 | |||
229 | ?> |