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 / I18Nv2 / CommonList.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  5.6 KB  |  243 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: I18Nv2 :: CommonList                                         |
  4. // +----------------------------------------------------------------------+
  5. // | This source file is subject to version 3.0 of the PHP license,       |
  6. // | that is available at http://www.php.net/license/3_0.txt              |
  7. // | If you did not receive a copy of the PHP license and are unable      |
  8. // | to obtain it through the world-wide-web, please send a note to       |
  9. // | license@php.net so we can mail you a copy immediately.               |
  10. // +----------------------------------------------------------------------+
  11. // | Copyright (c) 2004 Michael Wallner <mike@iworks.at>                  |
  12. // +----------------------------------------------------------------------+
  13. //
  14. // $Id: CommonList.php,v 1.13 2005/10/03 10:34:18 mike Exp $
  15.  
  16. /**
  17.  * I18Nv2::CommonList
  18.  * 
  19.  * @author      Michael Wallner <mike@php.net>
  20.  * @package     I18Nv2
  21.  * @category    Internationalization
  22.  */
  23.  
  24. /** 
  25.  * I18Nv2_CommonList
  26.  * 
  27.  * Base class for I18Nv2_Country and I18Nv2_Language that performs some basic
  28.  * work, so code doesn't get written twice or even more often in the future.
  29.  *
  30.  * @author      Michael Wallner <mike@php.net>
  31.  * @version     $Revision: 1.13 $
  32.  * @access      public
  33.  */
  34. class I18Nv2_CommonList
  35. {
  36.     /**
  37.      * Codes
  38.      * 
  39.      * @access  protected
  40.      * @var     array
  41.      */
  42.     var $codes = array();
  43.     
  44.     /**
  45.      * Language
  46.      * 
  47.      * @access  protected
  48.      * @var     string
  49.      */
  50.     var $language = '';
  51.     
  52.     /**
  53.      * Encoding
  54.      * 
  55.      * @access  protected
  56.      * @var     string
  57.      */
  58.     var $encoding = '';
  59.     
  60.     /**
  61.      * Constructor
  62.      *
  63.      * @access  public
  64.      * @param   string  $language
  65.      * @param   string  $encoding
  66.      */
  67.     function I18Nv2_CommonList($language = null, $encoding = null)
  68.     {
  69.         if (!$this->setLanguage($language)) {
  70.             if (class_exists('I18Nv2')) {
  71.                 $l = I18Nv2::lastLocale(0, true);
  72.                 if (!isset($l) || !$this->setLanguage($l['language'])) {
  73.                     $this->setLanguage('en');
  74.                 }
  75.             } else {
  76.                 $this->setLanguage('en');
  77.             }
  78.         }
  79.         if (!$this->setEncoding($encoding)) {
  80.             $this->setEncoding('UTF-8');
  81.         }
  82.     }
  83.  
  84.     /**
  85.      * Set active language
  86.      * 
  87.      * Note that each time you set a different language the corresponding
  88.      * language file has to be loaded again, too.
  89.      *
  90.      * @access  public
  91.      * @return  bool
  92.      * @param   string  $language
  93.      */
  94.     function setLanguage($language)
  95.     {
  96.         if (!isset($language)) {
  97.             return false;
  98.         }
  99.         $language = strToLower($language);
  100.         if ($language === $this->language) {
  101.             return true;
  102.         }
  103.         if ($this->loadLanguage($language)) {
  104.             $this->language = $language;
  105.             return true;
  106.         }
  107.         return false;
  108.     }
  109.     
  110.     /**
  111.      * Get current language
  112.      * 
  113.      * @access  public
  114.      * @return  string
  115.      */
  116.     function getLanguage()
  117.     {
  118.         return $this->language;
  119.     }
  120.     
  121.     /**
  122.      * Set active encoding
  123.      *
  124.      * @access  public
  125.      * @return  bool
  126.      * @param   string  $encoding
  127.      */
  128.     function setEncoding($encoding)
  129.     {
  130.         if (!isset($encoding)) {
  131.             return false;
  132.         }
  133.         $this->encoding = strToUpper($encoding);
  134.         return true;
  135.     }
  136.     
  137.     /** 
  138.      * Get current encoding
  139.      * 
  140.      * @access  public
  141.      * @return  string
  142.      */
  143.     function getEncoding()
  144.     {
  145.         return $this->encoding;
  146.     }
  147.  
  148.     /**
  149.      * Check if code is valid
  150.      * 
  151.      * @access  public
  152.      * @return  bool
  153.      * @param   string  $code   code
  154.      */
  155.     function isValidCode($code)
  156.     {
  157.         return isset($this->codes[$this->changeKeyCase($code)]);
  158.     }
  159.  
  160.     /**
  161.      * Return corresponding name of code
  162.      * 
  163.      * @access  public
  164.      * @return  string  name
  165.      * @param   string  $code   code
  166.      */
  167.     function getName($code)
  168.     {
  169.         $code = $this->changeKeyCase($code);
  170.         if (!isset($this->codes[$code])) {
  171.             return '';
  172.         }
  173.         if ('UTF-8' !== $this->encoding) {
  174.             return iconv('UTF-8', $this->encoding .'//TRANSLIT', $this->codes[$code]);
  175.         }
  176.         return $this->codes[$code];
  177.     }
  178.  
  179.     /**
  180.      * Return all the codes
  181.      *
  182.      * @access  public
  183.      * @return  array   all codes as associative array
  184.      */
  185.     function getAllCodes()
  186.     {
  187.         if ('UTF-8' !== $this->encoding) {
  188.             $codes = $this->codes;
  189.             array_walk($codes, array(&$this, '_iconv'));
  190.             return $codes;
  191.         }
  192.         return $this->codes;
  193.     }
  194.     
  195.     /**
  196.      * @access  private
  197.      * @return  void
  198.      */
  199.     function _iconv(&$code, $key)
  200.     {
  201.         $code = iconv('UTF-8', $this->encoding .'//TRANSLIT', $code);
  202.     }
  203.     
  204.     /** 
  205.      * Load Language
  206.      * 
  207.      * @access  proteceted
  208.      * @return  bool
  209.      * @param   string  $language
  210.      */
  211.     function loadLanguage($language)
  212.     {
  213.         return false;
  214.     }
  215.     
  216.     /**
  217.      * Change Key Case
  218.      *
  219.      * @access  protected
  220.      * @return  string
  221.      * @param   string  $code
  222.      */
  223.     function changeKeyCase($code)
  224.     {
  225.         return $code;
  226.     }
  227.     
  228.     /**
  229.      * Decorate this list
  230.      *
  231.      * @access  public
  232.      * @return  object  I18NV2_DecoratedList
  233.      * @param   string  $type
  234.      */
  235.     function &toDecoratedList($type)
  236.     {
  237.         require_once 'I18Nv2/DecoratedList/'. $type .'.php';
  238.         $decoratedList = 'I18Nv2_DecoratedList_' . $type;
  239.         return new $decoratedList($this);
  240.     }    
  241. }
  242. ?>
  243.