Exemplo n.º 1
0
 /**
  * Constructor of the string.
  *
  *
  * If no charset is provided the default charset is assumed. The default charset
  * can be set on the Customweb_Core_Charset. If the charset is set to 'fix' than
  * the string is tried to convert to UTF-8 by keeping UTF-8 chars and converting
  * ISO-8859-1 and WINOWS-1250 chars to UTF-8. This may be useful in cases, when
  * the charset is unclear. However it will be never perfect. See
  * Customweb_Core_Charset_UTF8::fixCharset for more information.
  *
  * If the flag keepCharset is set to true, then the charset is kept as provided.
  * Otherwise the string is converted to the default charset.
  *
  *
  * @param string $string
  * @param string | Customweb_Core_Charset $charset
  * @param boolean $keepCharset
  */
 public function __construct($string, $charset = null, $keepCharset = false)
 {
     $this->string = $string;
     // In case no charset is provided we assume it is the default charset.
     if ($charset === null) {
         $charset = Customweb_Core_Charset::getDefaultCharset();
     }
     if ($charset === 'fix') {
         $string = Customweb_Core_Charset_UTF8::fixCharset($string);
         $charset = 'UTF-8';
     }
     // Since we may serialize this object, we store the charset name and not the
     // whole charset.
     if (is_object($charset)) {
         if (!$charset instanceof Customweb_Core_Charset) {
             throw new Customweb_Core_Exception_CastException('Customweb_Core_Charset');
         }
         $this->charsetObject = $charset;
         $this->charset = $charset->getName();
     } else {
         $this->charsetObject = Customweb_Core_Charset::forName($charset);
         $this->charset = $this->charsetObject->getName();
     }
     // Convert to default charset, if not defined differently
     if ($keepCharset !== true && $this->charset !== Customweb_Core_Charset::getDefaultCharset()->getName()) {
         $this->string = Customweb_Core_Charset::convert($this->string, $this->charsetObject, Customweb_Core_Charset::getDefaultCharset());
         $this->charsetObject = Customweb_Core_Charset::getDefaultCharset();
         $this->charset = $this->charsetObject->getName();
     }
 }