Horde_Pack::pack PHP Method

pack() public method

Pack a string.
public pack ( mixed $data, array $opts = [] ) : string
$data mixed The data to pack.
$opts array Additional options:
  - compress: (mixed) If false, don't use compression. If true, uses
              default compress length (DEFAULT). If 0, always compress.
              All other integer values: compress only if data is
              greater than this string length.
  - drivers: (array) Only use these drivers to pack. By default, driver
             to use is auto-determined.
  - phpob: (boolean) If true, the data contains PHP serializable
           objects (i.e. objects that have a PHP-specific serialized
           representation). If false, the data does not contain any of
           these objects. If not present, will auto-determine
           existence of these objects.
return string The packed string.
    public function pack($data, array $opts = array())
    {
        $opts = array_merge(array('compress' => true), $opts);
        if (!isset($opts['phpob'])) {
            $auto = new Horde_Pack_Autodetermine($data);
            $opts['phpob'] = $auto->phpob;
        }
        foreach (self::$_drivers as $key => $val) {
            if (!empty($opts['phpob']) && !$val->phpob) {
                continue;
            }
            if (isset($opts['drivers'])) {
                if (!in_array(get_class($val), $opts['drivers'])) {
                    continue;
                }
            }
            /* Format of data:
             * First-byte:
             *   1,2,4,8,16,32 - Reserved for pack format.
             *   64 - Packed data has been compressed.
             *   128 - RESERVED for future use (if set, indicates that initial
             *         byte will extend into next byte).
             * Packed (and compressed data) follows this byte. */
            try {
                $packed = $val->pack($data);
            } catch (Horde_Pack_Exception $e) {
                continue;
            }
            if ($opts['compress'] !== false) {
                if ($opts['compress'] === 0) {
                    $compress = true;
                } else {
                    if ($opts['compress'] === true) {
                        $opts['compress'] = self::DEFAULT_COMPRESS;
                    }
                    $compress = strlen($packed) > $opts['compress'];
                }
                if ($compress) {
                    $packed = self::$_compress->compress($packed);
                    $key |= self::COMPRESS_MASK;
                }
            }
            return pack('C', $key) . $packed;
        }
        throw new Horde_Pack_Exception('Could not pack data.');
    }

Usage Example

Example #1
0
 /**
  * Updates the cache.
  */
 public function save()
 {
     foreach ($this->_update as $mbox => $val) {
         try {
             if (!empty($val['u'])) {
                 $ptr =& $this->_data[$mbox];
                 foreach ($this->_getMsgCids($mbox, array_keys($val['u'])) as $k2 => $v2) {
                     try {
                         $this->_hash->set($v2, $this->_pack->pack($ptr[$k2]), array('expire' => $this->_params['lifetime']));
                     } catch (Horde_Pack_Exception $e) {
                         $this->deleteMsgs($mbox, array($v2));
                         $val['d'][] = $v2;
                     }
                 }
             }
             if (!empty($val['d'])) {
                 $this->_hash->delete(array_values($this->_getMsgCids($mbox, $val['d'])));
             }
             if (!empty($val['m'])) {
                 try {
                     $this->_hash->set($this->_getCid($mbox), $this->_pack->pack($this->_mbox[$mbox]), array('expire' => $this->_params['lifetime']));
                 } catch (Horde_Pack_Exception $e) {
                 }
             }
         } catch (Horde_Exception $e) {
         }
     }
     $this->_update = array();
 }
All Usage Examples Of Horde_Pack::pack