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