<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */


class SerializeOperations
{
  
  public static function serialize4Settings($pObj)
  {
    return 'BASE64UTF8#' . base64_encode(serialize($pObj));
  }

  
  public static function unserialize($pStr)
  {
    $prefixPos = StringOperations::strpos($pStr, '#');
    if ($prefixPos !== false) {
      
      $prefix = StringOperations::substr($pStr, 0, $prefixPos + 1);

      switch ($prefix) {
        case 'BASE64#':
          $encoding = new \EncodeCharsetFromTo(CP1252, UTF_8);
          return $encoding->encodeRecursive(unserialize(base64_decode(StringOperations::substr($pStr, $prefixPos))));
        case 'BASE64UTF8#':
          return unserialize(base64_decode(StringOperations::substr($pStr, $prefixPos)));
      }
    }

    return unserialize($pStr);
  }
}