Subversion Repositories PHPX

Rev

Rev 8 | Rev 11 | 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 &lt;php@PointedEars.de&gt;
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
      }
8 PointedEar 60
 
61
      $footnotes[$name] = new Footnote($name, $sign, $text);
7 PointedEar 62
    }
63
 
8 PointedEar 64
    return $footnotes[$name]->getRef();
7 PointedEar 65
  }
66
 
67
  /**
68
   * Prints the list of footnotes
69
   */
70
  public function printMe()
71
  {
72
    $footnotes =& $this->footnotes;
73
 
8 PointedEar 74
    function cmp($a, $b)
75
    {
76
      if ($a->sign < $b->sign)
77
      {
78
        return -1;
79
      }
80
      else if ($a->sign > $b->sign)
81
      {
82
        return 1;
83
      }
84
      else
85
      {
86
        return 0;
87
      }
88
    }
89
 
90
    uasort($footnotes, 'cmp');
91
 
92
    ?><table class="footnotes">
93
    <?php
94
 
7 PointedEar 95
    foreach ($footnotes as $name => &$footnote)
96
    {
97
      $footnote->printMe();
98
    }
8 PointedEar 99
 
100
    ?></table><?php
7 PointedEar 101
  }
102
 
103
  /**
104
   * Prints the list of footnotes and clears the list in memory
105
   */
106
  public function flush()
107
  {
108
    $this->printMe();
109
    $this->clear();
110
  }
111
}
112
 
113
/**
114
 * A footnote to be used in a {@link #FootnoteList "footnote list"}
115
 *
116
 * @author Thomas 'PointedEars' Lahn &lt;php@PointedEars.de&gt;
117
 */
118
class Footnote
119
{
120
  /**
8 PointedEar 121
   * The name of this footnote
122
   *
123
   * @var string
124
   */
125
  protected $name = '';
126
 
127
  /**
7 PointedEar 128
   * The sign used for referring to this footnote
129
   *
130
   * @var string
131
   */
132
  protected $sign = '';
133
 
134
  /**
135
   * The text for this footnote
136
   *
137
   * @var string
138
   */
139
  protected $text = '';
140
 
141
  /**
8 PointedEar 142
   * The number of times this footnote has been referred
143
   *
144
   * @var int
145
   */
146
  protected $references = 0;
147
 
148
  /**
7 PointedEar 149
   * Creates a footnote
150
   *
8 PointedEar 151
   * @param string $name
152
   *   The name of this footnote
7 PointedEar 153
   * @param string $sign
154
   *   The sign that should be used for referring to this footnote
155
   * @param string $text
156
   *   The text for this footnote
157
   */
8 PointedEar 158
  public function __construct($name, $sign, $text)
7 PointedEar 159
  {
8 PointedEar 160
    $this->name = $name;
7 PointedEar 161
    $this->sign = $sign;
162
    $this->text = $text;
163
  }
8 PointedEar 164
 
165
  /**
166
   * Universal getter
167
   *
168
   * @param string $name
169
   *   Name of the property to be read-accessed.  Currently only 'sign'
170
   *   is supported.
10 PointedEar 171
   * @throws InvalidArgumentException if a non-existing property is accessed
8 PointedEar 172
   * @return mixed
173
   *   Property value
174
   */
175
  public function __get($name)
176
  {
177
    if ($name === 'sign')
178
    {
179
      return $this->sign;
180
    }
181
    else
182
    {
10 PointedEar 183
      throw new InvalidArgumentException(
184
        'No such property ' . get_class($this) . "::\$$name");
8 PointedEar 185
    }
186
  }
7 PointedEar 187
 
188
  /**
189
   * Returns the reference for this footnote
190
   *
191
   * @return string
192
   */
8 PointedEar 193
  public function getRef()
7 PointedEar 194
  {
8 PointedEar 195
    $s = $this->name;
196
 
197
    $ret = "<sup><a href='#footnote-{$s}'"
198
      . ($this->references === 0
199
        ? " name='fn-{$s}-ref' id='fn-{$s}-ref'"
200
        : '')
201
      . " class='footnote'>{$this->sign}</a></sup>";
202
 
203
    ++$this->references;
204
 
205
    return $ret;
7 PointedEar 206
  }
207
 
208
  /**
209
   * Prints this footnote in a footnote list
210
   */
211
  public function printMe()
212
  {
8 PointedEar 213
    $s = $this->name;
214
 
215
    echo "  <tr>
216
        <th><sup><a name='footnote-{$s}' id='footnote-{$s}' class='footnote'
217
          >{$this->sign}</a></sup><a href='#fn-{$s}-ref' name='footnote-{$s}'
218
          id='footnote-{$s}' class='backref'>&#8593;</a></th>
219
        <td>{$this->text}</td>
220
      </tr>
221
    ";
7 PointedEar 222
  }
223
}
224
 
225
?>