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.26 2008/08/31 13:58:30 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|PEAR_Errorlanguage information
  160.      */
  161.     function setLang($langID)
  162.     {
  163.         $res $this->getLangs()//load available languages, if not loaded yet
  164.         if (PEAR::isError($res)) {
  165.             return $res;
  166.         }
  167.         if (!array_key_exists($langID$this->langs)) {
  168.             return $this->raiseError('unknown language: "'.$langID.'"',
  169.                                     TRANSLATION2_ERROR_UNKNOWN_LANG,
  170.                                     PEAR_ERROR_RETURN,
  171.                                     E_USER_WARNING);
  172.         }
  173.         $this->currentLang $this->langs[$langID];
  174.         return $this->langs[$langID];
  175.     }
  176.  
  177.     // }}}
  178.     // {{{ getLang()
  179.  
  180.     /**
  181.      * Gets the current lang
  182.      *
  183.      * @param string $format what must be returned
  184.      *
  185.      * @return mixed array with current lang data or null if not set yet
  186.      */
  187.     function getLang($format 'id')
  188.     {
  189.         return isset($this->currentLang['id']$this->currentLang : null;
  190.     }
  191.  
  192.     // }}}
  193.     // {{{ getLangData()
  194.  
  195.     /**
  196.      * Gets the array data for the lang
  197.      *
  198.      * @param string $langID language ID
  199.      * @param string $format what must be returned
  200.      *
  201.      * @return mixed array with lang data or null if not available
  202.      */
  203.     function getLangData($langID$format 'id')
  204.     {
  205.         $langs $this->getLangs('array');
  206.         return isset($langs[$langID]$langs[$langID: null;
  207.     }
  208.  
  209.     // }}}
  210.     // {{{ getLangs()
  211.  
  212.     /**
  213.      * Gets the available languages
  214.      *
  215.      * @param string $format ['array' | 'ids' | 'names' | 'encodings']
  216.      *
  217.      * @return array|PEAR_Error
  218.      */
  219.     function getLangs($format 'array')
  220.     {
  221.         //if not cached yet, fetch langs data from the container
  222.         if (empty($this->langs|| !count($this->langs)) {
  223.             $res $this->fetchLangs()//container-specific method
  224.             if (PEAR::isError($res)) {
  225.                 return $res;
  226.             }
  227.         }
  228.  
  229.         $tmp = array();
  230.         switch ($format{
  231.         case 'array':
  232.             foreach ($this->langs as $aLang{
  233.                 $aLang['lang_id']  $aLang['id'];
  234.                 $tmp[$aLang['id']] $aLang;
  235.             }
  236.             break;
  237.         case 'id':
  238.         case 'ids':
  239.             foreach ($this->langs as $aLang{
  240.                 $tmp[$aLang['id'];
  241.             }
  242.             break;
  243.         case 'encoding':
  244.         case 'encodings':
  245.             foreach ($this->langs as $aLang{
  246.                 $tmp[$aLang['encoding'];
  247.             }
  248.             break;
  249.         case 'name':
  250.         case 'names':
  251.         default:
  252.             foreach ($this->langs as $aLang{
  253.                 $tmp[$aLang['id']] $aLang['name'];
  254.             }
  255.         }
  256.         return $tmp;
  257.     }
  258.  
  259.     // }}}
  260.     // {{{ fetchLangs()
  261.  
  262.     /**
  263.      * Fetch the available langs if they're not cached yet.
  264.      * Containers should implement this method.
  265.      *
  266.      * @return PEAR_Error on error
  267.      */
  268.     function fetchLangs()
  269.     {
  270.         return $this->raiseError('method "fetchLangs" not supported',
  271.                                  TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
  272.     }
  273.  
  274.     // }}}
  275.     // {{{ getPage()
  276.  
  277.     /**
  278.      * Returns an array of the strings in the selected page
  279.      * Containers should implement this method.
  280.      *
  281.      * @param string $pageID page/group ID
  282.      * @param string $langID language ID
  283.      *
  284.      * @return array 
  285.      */
  286.     function getPage($pageID = null$langID = null)
  287.     {
  288.         return $this->raiseError('method "getPage" not supported',
  289.                                  TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
  290.     }
  291.  
  292.     // }}}
  293.     // {{{ getOne()
  294.  
  295.     /**
  296.      * Get a single item from the container, without caching the whole page
  297.      * Containers should implement this method.
  298.      *
  299.      * @param string $stringID string ID
  300.      * @param string $pageID   page/group ID
  301.      * @param string $langID   language ID
  302.      *
  303.      * @return string 
  304.      */
  305.     function getOne($stringID$pageID = null$langID = null)
  306.     {
  307.         return $this->raiseError('method "getOne" not supported',
  308.                                  TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
  309.     }
  310.  
  311.     // }}}
  312.     // {{{ getStringID()
  313.  
  314.     /**
  315.      * Get the stringID for the given string
  316.      *
  317.      * @param string $string string
  318.      * @param string $pageID page/group ID
  319.      *
  320.      * @return string 
  321.      */
  322.     function getStringID($string$pageID = null)
  323.     {
  324.         return $this->raiseError('method "getStringID" not supported',
  325.                                  TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
  326.     }
  327.  
  328.     // }}}
  329.     // {{{ raiseError()
  330.  
  331.     /**
  332.      * Trigger a PEAR error
  333.      *
  334.      * @param string $msg    error message
  335.      * @param int    $code   error code
  336.      * @param int    $mode   PEAR error mode
  337.      * @param int    $option error severity
  338.      *
  339.      * @return void|PEAR_Error
  340.      * @access public
  341.      */
  342.     function raiseError($msg$code$mode = PEAR_ERROR_TRIGGER$option = E_USER_WARNING)
  343.     {
  344.         if (isset($GLOBALS['_PEAR_default_error_mode'])) {
  345.             $mode $GLOBALS['_PEAR_default_error_mode'];
  346.         }
  347.         if (isset($GLOBALS['_PEAR_default_error_options'])) {
  348.             $option $GLOBALS['_PEAR_default_error_options'];
  349.         }
  350.         if ($mode == PEAR_ERROR_RETURN{
  351.             return PEAR::raiseError($msg$code$mode$option);
  352.         else {
  353.             PEAR::raiseError($msg$code$mode$option);
  354.         }
  355.     }
  356.  
  357.     // }}}
  358. }
  359. ?>

Documentation generated on Fri, 14 Nov 2008 11:30:13 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.