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 / Decorator.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  10.9 KB  |  414 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Translation2_Decorator 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: Decorator.php,v 1.22 2007/10/28 23:14:49 quipo Exp $
  36.  * @link      http://pear.php.net/package/Translation2
  37.  */
  38.  
  39. /**
  40.  * Translation2_Decorator. Base Decorator class for Translation2
  41.  *
  42.  * Extend this class to provide custom decorators.
  43.  * Some decorators 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.  * @abstract
  52.  */
  53. class Translation2_Decorator extends Translation2
  54. {
  55.     // {{{ class vars
  56.  
  57.     /**
  58.      * Translation2 object being decorated
  59.      * @var object
  60.      * @access protected
  61.      */
  62.     var $translation2;
  63.  
  64.     /**
  65.      * @var object
  66.      * @access protected
  67.      */
  68.     var $storage;
  69.  
  70.     /**
  71.      * @var array
  72.      * @access protected
  73.      */
  74.     var $lang;
  75.  
  76.     /**
  77.      * @var string
  78.      * @access protected
  79.      */
  80.     var $currentPageID;
  81.  
  82.     // }}}
  83.     // {{{ Constructor
  84.  
  85.     /**
  86.      * Constructor
  87.      * Constructs the Translation2_Decorator
  88.      *
  89.      * @param object &$translation2 Translation2 object to decorate
  90.      */
  91.     function Translation2_Decorator(& $translation2)
  92.     {
  93.         $this->translation2 = & $translation2;
  94.         //used for debug only
  95.         $this->storage       = & $translation2->storage;
  96.         $this->currentPageID = & $translation2->currentPageID;
  97.         $this->lang          = & $translation2->lang;
  98.     }
  99.  
  100.     // }}}
  101.     // {{{ setOptions()
  102.  
  103.     /**
  104.      * set Decorator options
  105.      *
  106.      * @param array $options decorator options
  107.      *
  108.      * @return void
  109.      */
  110.     function setOptions($options=array())
  111.     {
  112.         if (is_array($options)) {
  113.             foreach ($options as $option => $value) {
  114.                 $this->setOption($option, $value);
  115.             }
  116.         }
  117.     }
  118.  
  119.     // }}}
  120.     // {{{ setOption()
  121.  
  122.     /**
  123.      * set Decorator option
  124.      *
  125.      * @param string $option option name
  126.      * @param mixed  $value  option value
  127.      *
  128.      * @return void
  129.      */
  130.     function setOption($option, $value=null)
  131.     {
  132.         if (isset($this->$option)) {
  133.             $this->$option = $value;
  134.         } elseif (is_a($this->translation2, 'Translation2_Decorator')) {
  135.             $this->translation2->setOption($option, $value);
  136.         }
  137.     }
  138.  
  139.     // }}}
  140.     // {{{ setContainerOptions()
  141.  
  142.     /**
  143.      * Set some storage driver options
  144.      *
  145.      * @param array $options storage driver options
  146.      *
  147.      * @return void
  148.      * @access protected
  149.      */
  150.     function setContainerOptions($options)
  151.     {
  152.         $this->storage->_parseOptions($options);
  153.     }
  154.  
  155.     // }}}
  156.     // {{{ getDecorator()
  157.  
  158.     /**
  159.      * Return an instance of a decorator
  160.      *
  161.      * @param string $decorator Name of the decorator
  162.      * @param object $object    instance [optional]
  163.      *
  164.      * @return object Decorator object reference
  165.      * @access public
  166.      */
  167.     function & getDecorator($decorator)
  168.     {
  169.         if (func_num_args() > 1) {
  170.             $obj = func_get_arg(1);
  171.             $new_decorator =& $this->translation2->getDecorator($decorator, $obj);
  172.         } else {
  173.             $new_decorator =& $this->translation2->getDecorator($decorator, $this);
  174.         }
  175.         return $new_decorator;
  176.     }
  177.  
  178.     // }}}
  179.     // {{{ setCharset()
  180.  
  181.     /**
  182.      * Set charset used to read/store the translations
  183.      *
  184.      * @param string $charset character set (encoding)
  185.      *
  186.      * @return PEAR_Error on failure
  187.      */
  188.     function setCharset($charset)
  189.     {
  190.         return $this->translation2->setCharset($charset);
  191.     }
  192.  
  193.     // }}}
  194.     // {{{ setLang()
  195.  
  196.     /**
  197.      * Set default language
  198.      *
  199.      * @param string $langID language ID
  200.      *
  201.      * @return void
  202.      */
  203.     function setLang($langID)
  204.     {
  205.         $this->translation2->setLang($langID);
  206.     }
  207.  
  208.     // }}}
  209.     // {{{ setPageID($pageID)
  210.  
  211.     /**
  212.      * Set default page
  213.      *
  214.      * @param string $pageID page/group ID
  215.      *
  216.      * @return void
  217.      */
  218.     function setPageID($pageID = null)
  219.     {
  220.         $this->translation2->setPageID($pageID);
  221.     }
  222.  
  223.     // }}}
  224.     // {{{ getLang()
  225.  
  226.     /**
  227.      * Get language info
  228.      *
  229.      * @param string $langID language ID
  230.      * @param string $format ['name', 'meta', 'error_text', 'array']
  231.      *
  232.      * @return mixed [string | array], depending on $format
  233.      */
  234.     function getLang($langID=null, $format='name')
  235.     {
  236.         return $this->translation2->getLang($langID, $format);
  237.     }
  238.  
  239.     // }}}
  240.     // {{{ getLangs()
  241.  
  242.     /**
  243.      * Get languages
  244.      *
  245.      * @param string $format ['ids', 'names', 'array']
  246.      *
  247.      * @return array
  248.      */
  249.     function getLangs($format='name')
  250.     {
  251.         return $this->translation2->getLangs($format);
  252.     }
  253.  
  254.     // }}}
  255.     // {{{ setParams()
  256.  
  257.     /**
  258.      * Set parameters for next string
  259.      *
  260.      * @param array $params array of replacement parameters
  261.      *
  262.      * @return void
  263.      */
  264.     function setParams($params = null)
  265.     {
  266.         $this->translation2->setParams($params);
  267.     }
  268.  
  269.     // }}}
  270.     // {{{ getRaw()
  271.  
  272.     /**
  273.      * Get translated string
  274.      *
  275.      * No filter is applied.
  276.      *
  277.      * @param string $stringID    string ID
  278.      * @param string $pageID      page/group ID
  279.      * @param string $langID      language ID
  280.      * @param string $defaultText Text to display when the strings in both
  281.      *                            the default and the fallback lang are empty
  282.      *
  283.      * @return string
  284.      */
  285.     function getRaw($stringID, $pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null, $defaultText = '')
  286.     {
  287.         return $this->translation2->getRaw($stringID, $pageID, $langID, $defaultText);
  288.     }
  289.  
  290.     // }}}
  291.     // {{{ get()
  292.  
  293.     /**
  294.      * Get translated string
  295.      *
  296.      * All the filters are applied.
  297.      *
  298.      * @param string $stringID    string ID
  299.      * @param string $pageID      page/group ID
  300.      * @param string $langID      language ID
  301.      * @param string $defaultText Text to display when the string is empty
  302.      *               NB: This parameter is only used in the DefaultText decorator
  303.      *
  304.      * @return string
  305.      */
  306.     function get($stringID, $pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null, $defaultText = '')
  307.     {
  308.         return $this->translation2->get($stringID, $pageID, $langID, $defaultText);
  309.     }
  310.  
  311.     // }}}
  312.     // {{{ getRawPage()
  313.  
  314.     /**
  315.      * Get the array of strings in a page
  316.      *
  317.      * Fetch the strings from the container, without any replacing
  318.      *
  319.      * @param string $pageID page/group ID
  320.      * @param string $langID language ID
  321.      *
  322.      * @return array
  323.      */
  324.     function getRawPage($pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null)
  325.     {
  326.         return $this->translation2->getRawPage($pageID, $langID);
  327.     }
  328.  
  329.     // }}}
  330.     // {{{ getPage()
  331.  
  332.     /**
  333.      * Same as getRawPage, but resort to fallback language and
  334.      * replace parameters when needed
  335.      *
  336.      * @param string $pageID page/group ID
  337.      * @param string $langID language ID
  338.      *
  339.      * @return array
  340.      */
  341.     function getPage($pageID = TRANSLATION2_DEFAULT_PAGEID, $langID = null)
  342.     {
  343.         $this->translation2->getPage($pageID, $langID);
  344.     }
  345.  
  346.     // }}}
  347.     // {{{ _replaceParams()
  348.  
  349.     /**
  350.      * Replace parameters in strings
  351.      *
  352.      * @param mixed $strings strings where the replacements must occur
  353.      *
  354.      * @return mixed
  355.      * @access protected
  356.      */
  357.     function _replaceParams($strings)
  358.     {
  359.         return $this->translation2->_replaceParams($strings);
  360.     }
  361.  
  362.     // }}}
  363.     // {{{ replaceEmptyStringsWithKeys()
  364.  
  365.     /**
  366.      * Replace empty strings with their stringID
  367.      *
  368.      * @param array $strings array of strings to be replaced if empty
  369.      *
  370.      * @return array
  371.      * @access public
  372.      */
  373.     function replaceEmptyStringsWithKeys($strings)
  374.     {
  375.         return $this->translation2->replaceEmptyStringsWithKeys($strings);
  376.     }
  377.  
  378.     // }}}
  379.     // {{{ getStringID()
  380.  
  381.     /**
  382.      * Get the stringID for the given string. This method is the reverse of get().
  383.      *
  384.      * @param string $string This is NOT the stringID, this is a real string.
  385.      *               The method will search for its matching stringID, and then
  386.      *               it will return the associate string in the selected language.
  387.      * @param string $pageID page/group ID
  388.      *
  389.      * @return string
  390.      */
  391.     function getStringID($string, $pageID = TRANSLATION2_DEFAULT_PAGEID)
  392.     {
  393.         return $this->translation2->getStringID($string, $pageID);
  394.     }
  395.  
  396.     // }}}
  397.     // {{{ __clone()
  398.  
  399.     /**
  400.      * Clone internal object references
  401.      *
  402.      * This method is called automatically by PHP5
  403.      *
  404.      * @return void
  405.      * @access protected
  406.      */
  407.     function __clone()
  408.     {
  409.         $this->translation2 = clone($this->translation2);
  410.     }
  411.  
  412.     // }}}
  413. }
  414. ?>