Subversion Repositories PHPX

Rev

Rev 8 | Rev 11 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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