PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/helper.inc.php

https://github.com/akerbos/bib2tpl
PHP | 176 lines | 86 code | 15 blank | 75 comment | 6 complexity | 5962a5cb238c332d0250af7ddf66d96c MD5 | raw file
  1. <?php
  2. /*
  3. * By Raphael Reitzig, 2012
  4. * version 2.0
  5. * code@verrech.net
  6. * http://lmazy.verrech.net
  7. */
  8. ?>
  9. <?php
  10. /*
  11. This program is free software: you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation, either version 3 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. ?>
  23. <?php
  24. /**
  25. * Provides helping functions in order to keep clutter from the main file.
  26. *
  27. * @author Raphael Reitzig <code@verrech.net>
  28. * @version 2.0
  29. * @package bib2tpl
  30. * @license http://www.gnu.org/licenses/gpl.html GPLv3
  31. * @copyright Š 2012, Raphael Reitzig
  32. */
  33. class Helper
  34. {
  35. /**
  36. * Copy of main class's options
  37. * @var array
  38. * @access private
  39. */
  40. private $options;
  41. /**
  42. * Constructor.
  43. *
  44. * @access public
  45. * @param array $options Options array with same semantics as main class.
  46. */
  47. function __construct(&$options=array())
  48. {
  49. $this->options = $options;
  50. }
  51. /**
  52. * Obtains a month number from the passed entry.
  53. *
  54. * @access private
  55. * @param array $entry An entry
  56. * @return string The passed entry's month number. <code>00</code> if
  57. * the month could not be recognized.
  58. */
  59. private function e2mn(&$entry) {
  60. $month = empty($entry['month']) ? '' : $entry['month'];
  61. $result = '00';
  62. $month = strtolower($month);
  63. // This is gonna get ugly; other solutions?
  64. $pattern = '/^'.$month.'/';
  65. if ( preg_match('/^\d[\d]$/', $month) )
  66. {
  67. return strlen($month) == 1 ? '0'.$month : $month;
  68. }
  69. else
  70. {
  71. foreach ( $this->options['lang']['months'] as $number => $name )
  72. {
  73. if ( preg_match($pattern , $name) )
  74. {
  75. $result = $number;
  76. break;
  77. }
  78. }
  79. }
  80. return $result;
  81. }
  82. /**
  83. * Compares two group keys for the purpose of sorting.
  84. *
  85. * @access public
  86. * @param string $k1 group key one
  87. * @param string $k2 group key two
  88. * @return int integer (<,=,>) zero if k1 is (less than,equal,larger than) k2
  89. */
  90. function group_cmp($k1, $k2)
  91. {
  92. return $this->options['order_groups'] !== 'desc'
  93. ? strcmp($k1, $k2)
  94. : -strcmp($k1, $k2);
  95. }
  96. /**
  97. * Compares two entries for the purpose of sorting.
  98. *
  99. * @access public
  100. * @param string $k1 entry key one
  101. * @param string $k2 entry key two
  102. * @return int integer (<,=,>) zero if entry[$k1] is
  103. * (less than,equal,larger than) entry[k2]
  104. */
  105. function entry_cmp($e1, $e2)
  106. {
  107. if ( $this->options['sort_by'] === 'DATE' )
  108. {
  109. $order = strcmp((!empty($e1['year']) ? $e1['year'] : '0000').$this->e2mn($e1),
  110. (!empty($e2['year']) ? $e2['year'] : '0000').$this->e2mn($e2));
  111. }
  112. elseif ( $this->options['sort_by'] === 'author' ) {
  113. $order = strcmp($e1['sortauthor'], $e2['sortauthor']);
  114. }
  115. elseif ( $this->options['sort_by'] === 'firstauthor' ) {
  116. $order = strcmp($e1['author'][0]['sort'], $e2['author'][0]['sort']);
  117. }
  118. else
  119. {
  120. $order = strcmp((!empty($e1[$this->options['sort_by']]) ? $e1[$this->options['sort_by']] : ''),
  121. (!empty($e2[$this->options['sort_by']]) ? $e2[$this->options['sort_by']] : ''));
  122. }
  123. if ( $this->options['order'] === 'desc' )
  124. {
  125. $order = -$order;
  126. }
  127. return $order;
  128. }
  129. /**
  130. * Counts array elements in the specified array at the specified level.
  131. * For depth<=1, lcount equals count.
  132. *
  133. * @access public
  134. * @param array $array Array to count
  135. * @param int $depth Counting depth. Default 1.
  136. * @return int Number of array elements in $array at nesting level $depth
  137. */
  138. static function lcount(&$array, $depth=1)
  139. {
  140. $sum = 0;
  141. $depth--;
  142. if ( $depth > 0 )
  143. {
  144. foreach ( $array as $elem )
  145. {
  146. $sum += is_array($elem) ? self::lcount($elem, $depth) : 0;
  147. }
  148. }
  149. else
  150. {
  151. foreach ( $array as $elem )
  152. {
  153. $sum += is_array($elem) ? 1 : 0;
  154. }
  155. }
  156. return $sum;
  157. }
  158. }
  159. ?>