- <?php
- /*
- * Lokorin.com
- * Copyright 2004-2006
- * Licensed under the GNU LGPL. See COPYING for full terms.
- */
- /**
- * A library for handeling abbreviations.
- * @author Andreas Launila
- * @version $Revision: 1.5 $
- * @package com.lokorin.lokorin.lib
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- */
-
- /**
- * For access to common constants and functions.
- */
- require_once('common.php');
-
- /**
- * A class for handling abbreviations that are entered into the site's
- * abbrevation database. This class should be concidered singelton and should
- * only be used using the one and only globally defined instance. Implements
- * the singleton pattern.
- * @author Andreas Launila
- * @package com.lokorin.lokorin.lib
- */
- class AbbreviationHandler {
- /**
- * An array where they keys are abbreviations and the corresponding values
- * are the meanings of those keys.
- * @var array
- */
- private $abbrMap;
- /**
- * The one and only instance of the class.
- * @var AbbreviationHandler
- */
- static private $instance;
-
- /**
- * Gets the one and only instance of the class.
- * @return CssTemplateConverter The one and only instance.
- */
- static public function getInstance() {
- if(!isset(AbbreviationHandler::$instance)) {
- AbbreviationHandler::$instance = new AbbreviationHandler();
- }
- return AbbreviationHandler::$instance;
- }
-
- /**
- * Constructor for AbbreviationHandler.
- */
- private function __construct() {
- global $db;
-
- $rows = $db->querySelect(TABLE_ABBR, array('abbreviation', 'meaning'));
- $this->abbrMap = array();
- foreach($rows as $row) {
- $this->abbrMap[$row['abbreviation']] = $row['meaning'];
- }
- }
-
- /**
- * Handles all abbreviations in a text by appending XHTML compliant
- * explainations to each recognised abbreviation.
- * @param string $text The text in which all abbrevaitions should be
- * handled.
- * @return string XHTML compliant code with all recognised abbriviations
- * explained.
- */
- public function handleAbbreviations($text) {
- foreach($this->abbrMap as $abbr => $meaning) {
- $text = preg_replace("/(['\"\s])".$abbr."([^\w])/",
- '$1'.$this->formatAbbreviation($abbr, $meaning).'$2', $text);
- }
- return $text;
- }
-
- /**
- * Formats an abbreviation into XHTML compliant code.
- * @param string $abbreviated The abbreviated form.
- * @param string $expanded The expanded form of the abbreviation.
- * @return string XHTML compliant code that represents the abbreviation
- * explaination.
- */
- public function formatAbbreviation($abbreviated, $expanded) {
- return '<abbr title="'.$expanded.'">'.$abbreviated.'</abbr>';
- }
- }
-
- ?>