home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Translation2 / Container.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  10.1 KB  |  351 lines

  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. class Translation2_Container
  53. {
  54.     // {{{ Class vars
  55.  
  56.     /**
  57.      * Additional options for the storage container
  58.      * @var array
  59.      */
  60.     var $options = array();
  61.  
  62.     /**
  63.      * @var array
  64.      * @access private
  65.      */
  66.     var $currentLang = array();
  67.  
  68.     /**
  69.      * @var array
  70.      * @access private
  71.      */
  72.     var $langs = array();
  73.  
  74.     // }}}
  75.     // {{{ Constructor
  76.  
  77.     /**
  78.      * Constructor
  79.      * Has to be overwritten by each storage class
  80.      *
  81.      * @access public
  82.      */
  83.     function Translation2_Container()
  84.     {
  85.     }
  86.  
  87.     // }}}
  88.     // {{{ _parseOptions()
  89.  
  90.     /**
  91.      * Parse options passed to the container class
  92.      *
  93.      * @param array $array options
  94.      *
  95.      * @return void
  96.      * @access protected
  97.      */
  98.     function _parseOptions($array)
  99.     {
  100.         if (!is_array($array)) {
  101.             return;
  102.         }
  103.         foreach ($array as $key => $value) {
  104.             if (isset($this->options[$key])) {
  105.                 $this->options[$key] = $value;
  106.             }
  107.         }
  108.     }
  109.  
  110.     // }}}
  111.     // {{{ _getLangID()
  112.  
  113.     /**
  114.      * Get a valid langID or raise an error when no valid language is set
  115.      *
  116.      * @param string $langID language ID
  117.      *
  118.      * @return string language ID or PEAR_Error on error
  119.      * @access private
  120.      */
  121.     function _getLangID($langID)
  122.     {
  123.         if (!empty($langID) || (0 === $langID)) {
  124.             return $langID;
  125.         }
  126.         if (!empty($this->currentLang['id']) || (0 === $this->currentLang['id'])) {
  127.             return $this->currentLang['id'];
  128.         }
  129.         $msg = 'No valid language set. Use Translation2::setLang().';
  130.         return $this->raiseError($msg, TRANSLATION2_ERROR_UNKNOWN_LANG);
  131.     }
  132.  
  133.     // }}}
  134.     // {{{ setCharset()
  135.  
  136.     /**
  137.      * Set charset used to read/store the translations
  138.      *
  139.      * @param string $charset character set (encoding)
  140.      *
  141.      * @return PEAR_Error on error
  142.      */
  143.     function setCharset($charset)
  144.     {
  145.         if (method_exists($this->storage, 'setCharset')) {
  146.             return $this->storage->setCharset($charset);
  147.         }
  148.         return $this->raiseError(TRANSLATION2_ERROR_UNSUPPORTED, null, null,
  149.             'method not implemented', __FUNCTION__);
  150.     }
  151.  
  152.     // }}}
  153.     // {{{ setLang()
  154.  
  155.     /**
  156.      * Sets the current language
  157.      *
  158.      * @param string $langID language ID
  159.      *
  160.      * @return array language information
  161.      */
  162.     function setLang($langID)
  163.     {
  164.         $this->getLangs(); //load available languages, if not loaded yet (ignore return value)
  165.         if (!array_key_exists($langID, $this->langs)) {
  166.             return $this->raiseError('unknown language: "'.$langID.'"',
  167.                                     TRANSLATION2_ERROR_UNKNOWN_LANG,
  168.                                     PEAR_ERROR_RETURN,
  169.                                     E_USER_WARNING);
  170.         }
  171.         $this->currentLang = $this->langs[$langID];
  172.         return $this->langs[$langID];
  173.     }
  174.  
  175.     // }}}
  176.     // {{{ getLang()
  177.  
  178.     /**
  179.      * Gets the current lang
  180.      *
  181.      * @param string $format what must be returned
  182.      *
  183.      * @return mixed array with current lang data or null if not set yet
  184.      */
  185.     function getLang($format = 'id')
  186.     {
  187.         return isset($this->currentLang['id']) ? $this->currentLang : null;
  188.     }
  189.  
  190.     // }}}
  191.     // {{{ getLangData()
  192.  
  193.     /**
  194.      * Gets the array data for the lang
  195.      *
  196.      * @param string $langID language ID
  197.      * @param string $format what must be returned
  198.      *
  199.      * @return mixed array with lang data or null if not available
  200.      */
  201.     function getLangData($langID, $format = 'id')
  202.     {
  203.         $langs = $this->getLangs('array');
  204.         return isset($langs[$langID]) ? $langs[$langID] : null;
  205.     }
  206.  
  207.     // }}}
  208.     // {{{ getLangs()
  209.  
  210.     /**
  211.      * Gets the available languages
  212.      *
  213.      * @param string $format ['array' | 'ids' | 'names' | 'encodings']
  214.      *
  215.      * @return array
  216.      */
  217.     function getLangs($format = 'array')
  218.     {
  219.         //if not cached yet, fetch langs data from the container
  220.         if (empty($this->langs) || !count($this->langs)) {
  221.             $this->fetchLangs(); //container-specific method
  222.         }
  223.  
  224.         $tmp = array();
  225.         switch ($format) {
  226.         case 'array':
  227.             foreach ($this->langs as $aLang) {
  228.                 $aLang['lang_id']  = $aLang['id'];
  229.                 $tmp[$aLang['id']] = $aLang;
  230.             }
  231.             break;
  232.         case 'ids':
  233.             foreach ($this->langs as $aLang) {
  234.                 $tmp[] = $aLang['id'];
  235.             }
  236.             break;
  237.         case 'encodings':
  238.             foreach ($this->langs as $aLang) {
  239.                 $tmp[] = $aLang['encoding'];
  240.             }
  241.             break;
  242.         case 'names':
  243.         default:
  244.             foreach ($this->langs as $aLang) {
  245.                 $tmp[$aLang['id']] = $aLang['name'];
  246.             }
  247.         }
  248.         return $tmp;
  249.     }
  250.  
  251.     // }}}
  252.     // {{{ fetchLangs()
  253.  
  254.     /**
  255.      * Fetch the available langs if they're not cached yet.
  256.      * Containers should implement this method.
  257.      *
  258.      * @return PEAR_Error on error
  259.      */
  260.     function fetchLangs()
  261.     {
  262.         return $this->raiseError('method "fetchLangs" not supported',
  263.                                  TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
  264.     }
  265.  
  266.     // }}}
  267.     // {{{ getPage()
  268.  
  269.     /**
  270.      * Returns an array of the strings in the selected page
  271.      * Containers should implement this method.
  272.      *
  273.      * @param string $pageID page/group ID
  274.      * @param string $langID language ID
  275.      *
  276.      * @return array
  277.      */
  278.     function getPage($pageID = null, $langID = null)
  279.     {
  280.         return $this->raiseError('method "getPage" not supported',
  281.                                  TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
  282.     }
  283.  
  284.     // }}}
  285.     // {{{ getOne()
  286.  
  287.     /**
  288.      * Get a single item from the container, without caching the whole page
  289.      * Containers should implement this method.
  290.      *
  291.      * @param string $stringID string ID
  292.      * @param string $pageID   page/group ID
  293.      * @param string $langID   language ID
  294.      *
  295.      * @return string
  296.      */
  297.     function getOne($stringID, $pageID = null, $langID = null)
  298.     {
  299.         return $this->raiseError('method "getOne" not supported',
  300.                                  TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
  301.     }
  302.  
  303.     // }}}
  304.     // {{{ getStringID()
  305.  
  306.     /**
  307.      * Get the stringID for the given string
  308.      *
  309.      * @param string $string string
  310.      * @param string $pageID page/group ID
  311.      *
  312.      * @return string
  313.      */
  314.     function getStringID($string, $pageID = null)
  315.     {
  316.         return $this->raiseError('method "getStringID" not supported',
  317.                                  TRANSLATION_ERROR_METHOD_NOT_SUPPORTED);
  318.     }
  319.  
  320.     // }}}
  321.     // {{{ raiseError()
  322.  
  323.     /**
  324.      * Trigger a PEAR error
  325.      *
  326.      * @param string $msg    error message
  327.      * @param int    $code   error code
  328.      * @param int    $mode   PEAR error mode
  329.      * @param int    $option error severity
  330.      *
  331.      * @return void|PEAR_Error
  332.      * @access public
  333.      */
  334.     function raiseError($msg, $code, $mode = PEAR_ERROR_TRIGGER, $option = E_USER_WARNING)
  335.     {
  336.         if (isset($GLOBALS['_PEAR_default_error_mode'])) {
  337.             $mode = $GLOBALS['_PEAR_default_error_mode'];
  338.         }
  339.         if (isset($GLOBALS['_PEAR_default_error_options'])) {
  340.             $option = $GLOBALS['_PEAR_default_error_options'];
  341.         }
  342.         if ($mode == PEAR_ERROR_RETURN) {
  343.             return PEAR::raiseError($msg, $code, $mode, $option);
  344.         } else {
  345.             PEAR::raiseError($msg, $code, $mode, $option);
  346.         }
  347.     }
  348.  
  349.     // }}}
  350. }
  351. ?>