Subversion Repositories PHPX

Rev

Rev 10 | Rev 12 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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