com.lokorin.lokorin.lib
[ class tree: com.lokorin.lokorin.lib ] [ index: com.lokorin.lokorin.lib ] [ all elements ]

Source for file lib_abbreviations.php

Documentation is available at lib_abbreviations.php

  1. <?php
  2. /*
  3. * Lokorin.com
  4. * Copyright 2004-2006
  5. * Licensed under the GNU LGPL. See COPYING for full terms.
  6. */
  7. /**
  8. * A library for handeling abbreviations.
  9. * @author Andreas Launila
  10. * @version $Revision: 1.5 $
  11. * @package com.lokorin.lokorin.lib
  12. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  13. */
  14.  
  15. /**
  16. * For access to common constants and functions.
  17. */
  18. require_once('common.php');
  19.  
  20. /**
  21. * A class for handling abbreviations that are entered into the site's
  22. * abbrevation database. This class should be concidered singelton and should
  23. * only be used using the one and only globally defined instance. Implements
  24. * the singleton pattern.
  25. * @author Andreas Launila
  26. * @package com.lokorin.lokorin.lib
  27. */
  28. class AbbreviationHandler {
  29. /**
  30. * An array where they keys are abbreviations and the corresponding values
  31. * are the meanings of those keys.
  32. * @var array
  33. */
  34. private $abbrMap;
  35. /**
  36. * The one and only instance of the class.
  37. * @var AbbreviationHandler
  38. */
  39. static private $instance;
  40. /**
  41. * Gets the one and only instance of the class.
  42. * @return CssTemplateConverter The one and only instance.
  43. */
  44. static public function getInstance() {
  45. if(!isset(AbbreviationHandler::$instance)) {
  46. AbbreviationHandler::$instance = new AbbreviationHandler();
  47. }
  48. return AbbreviationHandler::$instance;
  49. }
  50. /**
  51. * Constructor for AbbreviationHandler.
  52. */
  53. private function __construct() {
  54. global $db;
  55. $rows = $db->querySelect(TABLE_ABBR, array('abbreviation', 'meaning'));
  56. $this->abbrMap = array();
  57. foreach($rows as $row) {
  58. $this->abbrMap[$row['abbreviation']] = $row['meaning'];
  59. }
  60. }
  61. /**
  62. * Handles all abbreviations in a text by appending XHTML compliant
  63. * explainations to each recognised abbreviation.
  64. * @param string $text The text in which all abbrevaitions should be
  65. * handled.
  66. * @return string XHTML compliant code with all recognised abbriviations
  67. * explained.
  68. */
  69. public function handleAbbreviations($text) {
  70. foreach($this->abbrMap as $abbr => $meaning) {
  71. $text = preg_replace("/(['\"\s])".$abbr."([^\w])/",
  72. '$1'.$this->formatAbbreviation($abbr, $meaning).'$2', $text);
  73. }
  74. return $text;
  75. }
  76. /**
  77. * Formats an abbreviation into XHTML compliant code.
  78. * @param string $abbreviated The abbreviated form.
  79. * @param string $expanded The expanded form of the abbreviation.
  80. * @return string XHTML compliant code that represents the abbreviation
  81. * explaination.
  82. */
  83. public function formatAbbreviation($abbreviated, $expanded) {
  84. return '<abbr title="'.$expanded.'">'.$abbreviated.'</abbr>';
  85. }
  86. }
  87.  
  88. ?>

Documentation generated on Sun, 16 Apr 2006 21:03:04 +0200 by phpDocumentor 1.3.0RC4