Translation2
[ class tree: Translation2 ] [ index: Translation2 ] [ all elements ]

Source for file Container.php

Documentation is available at Container.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Translation2_Container base class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. The name of the author may not be used to endorse or promote products
  17.  *    derived from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  20.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22.  * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  23.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * @category  Internationalization
  31.  * @package   Translation2
  32.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  33.  * @copyright 2004-2005 Lorenzo Alberton
  34.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  35.  * @version   CVS: $Id: Container.php,v 1.24 2008/02/02 18:03:04 quipo Exp $
  36.  * @link      http://pear.php.net/package/Translation2
  37.  */
  38.  
  39. /**
  40.  * Base class for Translation2 drivers/containers
  41.  *
  42.  * Extend this class to provide custom containers.
  43.  * Some containers are already bundled with the package.
  44.  *
  45.  * @category  Internationalization
  46.  * @package   Translation2
  47.  * @author    Lorenzo Alberton <l.alberton@quipo.it>
  48.  * @copyright 2004-2005 Lorenzo Alberton
  49.  * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  50.  * @link      http://pear.php.net/package/Translation2
  51.  */
  52. {
  53.     // {{{ Class vars
  54.  
  55.     /**
  56.      * Additional options for the storage container
  57.      * @var array 
  58.      */
  59.     var $options = array();
  60.  
  61.     /**
  62.      * @var array 
  63.      * @access private
  64.      */
  65.     var $currentLang = array();
  66.  
  67.     /**
  68.      * @var array 
  69.      * @access private
  70.      */
  71.     var $langs = array();
  72.  
  73.     // }}}
  74.     // {{{ Constructor
  75.  
  76.     /**
  77.      * Constructor
  78.      * Has to be overwritten by each storage class
  79.      *
  80.      * @access public
  81.      */
  82.     function Translation2_Container()
  83.     {
  84.     }
  85.  
  86.     // }}}
  87.     // {{{ _parseOptions()
  88.  
  89.     /**
  90.      * Parse options passed to the container class
  91.      *
  92.      * @param array $array options
  93.      *
  94.      * @return void 
  95.      * @access protected
  96.      */
  97.     function _parseOptions($array)
  98.     {
  99.         if (!is_array($array)) {
  100.             return;
  101.         }
  102.         foreach ($array as $key => $value{
  103.             if (isset($this->options[$key])) {
  104.                 $this->options[$key$value;
  105.             }
  106.         }
  107.     }
  108.  
  109.     // }}}
  110.     // {{{ _getLangID()
  111.  
  112.     /**
  113.      * Get a valid langID or raise an error when no valid language is set
  114.      *
  115.      * @param string $langID language ID
  116.      *
  117.      * @return string language ID or PEAR_Error on error
  118.      * @access private
  119.      */
  120.     function _getLangID($langID)
  121.     {
  122.         if (!empty($langID|| (0 === $langID)) {
  123.             return $langID;
  124.         }
  125.         if (!empty($this->currentLang['id']|| (0 === $this->currentLang['id'])) {
  126.             return $this->currentLang['id'];
  127.         }
  128.         $msg 'No valid language set. Use Translation2::setLang().';
  129.         return $this->raiseError($msgTRANSLATION2_ERROR_UNKNOWN_LANG);
  130.     }
  131.  
  132.     // }}}
  133.     // {{{ setCharset()
  134.  
  135.     /**
  136.      * Set charset used to read/store the translations
  137.      *
  138.      * @param string $charset character set (encoding)
  139.      *
  140.      * @return PEAR_Error on error
  141.      */
  142.     function setCharset($charset)
  143.     {
  144.         if (method_exists($this->storage'setCharset')) {
  145.             return $this->storage->setCharset($charset);
  146.         }
  147.         return $this->raiseError(TRANSLATION2_ERROR_UNSUPPORTEDnullnull,
  148.             'method not implemented'__FUNCTION__);
  149.     }
  150.  
  151.     // }}}
  152.     // {{{ setLang()
  153.  
  154.     /**
  155.      * Sets the current language
  156.      *
  157.      * @param string $langID language ID
  158.      *
  159.      * @return array language information
  160.      */
  161.     function setLang($langID)
  162.     {
  163.         $this->getLangs()//load available languages, if not loaded yet (ignore return value)
  164.         if (!array_key_exists($langID$this->langs)) {
  165.             return $this->raiseError('unknown language: "'.$langID.'"',
  166.                                     TRANSLATION2_ERROR_UNKNOWN_LANG,
  167.                                     PEAR_ERROR_RETURN,
  168.                                     E_USER_WARNING);
  169.         }
  170.         $this->currentLang $this->langs[$langID];
  171.         return $this->langs[$langID];
  172.     }
  173.  
  174.     // }}}
  175.     // {{{ getLang()
  176.  
  177.     /**
  178.      * Gets the current lang
  179.      *
  180.      * @param string $format what must be returned
  181.      *
  182.      * @return mixed array with current lang data or null if not set yet
  183.      */
  184.     function getLang($format 'id')
  185.     {
  186.         return isset($this->currentLang['id']$this->currentLang : null;
  187.     }
  188.  
  189.     // }}}
  190.     // {{{ getLangData()
  191.  
  192.     /**
  193.      * Gets the array data for the lang
  194.      *
  195.      * @param string $langID language ID
  196.      * @param string $format what must be returned
  197.      *
  198.      * @return mixed array with lang data or null if not available
  199.      */
  200.     function getLangData($langID$format 'id')
  201.     {
  202.         $langs $this->getLangs('array');
  203.         return isset($langs[$langID]$langs[$langID: null;
  204.     }
  205.  
  206.     // }}}
  207.     // {{{ getLangs()
  208.  
  209.     /**
  210.      * Gets the available languages
  211.      *
  212.      * @param string $format ['array' | 'ids' | 'names' | 'encodings']
  213.      *
  214.      * @return array 
  215.      */
  216.     function getLangs($format 'array')
  217.     {
  218.         //if not cached yet, fetch langs data from the container
  219.         if (empty($this->langs|| !count($this->langs)) {
  220.             $this->fetchLangs()//container-specific method
  221.         }
  222.  
  223.         $tmp = array();
  224.         switch ($format{
  225.         case 'array':
  226.             foreach ($this->langs as $aLang{
  227.                 $aLang['lang_id']  $aLang['id'];
  228.                 $tmp[$aLang['id']] $aLang;
  229.             }
  230.             break;
  231.         case 'ids':
  232.             foreach ($this->langs as $aLang{
  233.                 $tmp[$aLang['id'];
  234.             }
  235.             break;
  236.         case 'encodings':
  237.             foreach ($this->langs as $aLang{
  238.                 $tmp[$aLang['encoding'];
  239.             }
  240.             break;
  241.         case 'names':
  242.         default:
  243.             foreach ($this->langs as $aLang{
  244.                 $tmp[$aLang['id']] $aLang['name'];
  245.             }
  246.         }
  247.         return $tmp;
  248.     }
  249.  
  250.     // }}}
  251.     // {{{ fetchLangs()
  252.  
  253.     /**
  254.      * Fetch the available langs if they're not cached yet.
  255.      * Containers should implement this method.
  256.      *
  257.      * @return PEAR_Error on error
  258.      */
  259.     function fetchLangs()
  260.     {
  261.         return $this->raiseError('method "fetchLangs" not supported',
  262.                                  TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
  263.     }
  264.  
  265.     // }}}
  266.     // {{{ getPage()
  267.  
  268.     /**
  269.      * Returns an array of the strings in the selected page
  270.      * Containers should implement this method.
  271.      *
  272.      * @param string $pageID page/group ID
  273.      * @param string $langID language ID
  274.      *
  275.      * @return array 
  276.      */
  277.     function getPage($pageID = null$langID = null)
  278.     {
  279.         return $this->raiseError('method "getPage" not supported',
  280.                                  TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
  281.     }
  282.  
  283.     // }}}
  284.     // {{{ getOne()
  285.  
  286.     /**
  287.      * Get a single item from the container, without caching the whole page
  288.      * Containers should implement this method.
  289.      *
  290.      * @param string $stringID string ID
  291.      * @param string $pageID   page/group ID
  292.      * @param string $langID   language ID
  293.      *
  294.      * @return string 
  295.      */
  296.     function getOne($stringID$pageID = null$langID = null)
  297.     {
  298.         return $this->raiseError('method "getOne" not supported',
  299.                                  TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
  300.     }
  301.  
  302.     // }}}
  303.     // {{{ getStringID()
  304.  
  305.     /**
  306.      * Get the stringID for the given string
  307.      *
  308.      * @param string $string string
  309.      * @param string $pageID page/group ID
  310.      *
  311.      * @return string 
  312.      */
  313.     function getStringID($string$pageID = null)
  314.     {
  315.         return $this->raiseError('method "getStringID" not supported',
  316.                                  TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
  317.     }
  318.  
  319.     // }}}
  320.     // {{{ raiseError()
  321.  
  322.     /**
  323.      * Trigger a PEAR error
  324.      *
  325.      * @param string $msg    error message
  326.      * @param int    $code   error code
  327.      * @param int    $mode   PEAR error mode
  328.      * @param int    $option error severity
  329.      *
  330.      * @return void|PEAR_Error
  331.      * @access public
  332.      */
  333.     function raiseError($msg$code$mode = PEAR_ERROR_TRIGGER$option = E_USER_WARNING)
  334.     {
  335.         if (isset($GLOBALS['_PEAR_default_error_mode'])) {
  336.             $mode $GLOBALS['_PEAR_default_error_mode'];
  337.         }
  338.         if (isset($GLOBALS['_PEAR_default_error_options'])) {
  339.             $option $GLOBALS['_PEAR_default_error_options'];
  340.         }
  341.         if ($mode == PEAR_ERROR_RETURN{
  342.             return PEAR::raiseError($msg$code$mode$option);
  343.         else {
  344.             PEAR::raiseError($msg$code$mode$option);
  345.         }
  346.     }
  347.  
  348.     // }}}
  349. }
  350. ?>

Documentation generated on Tue, 06 May 2008 06:00:16 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.