PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 37ms app.codeStats 0ms

/library/Zend/Pdf/Element/String/Binary.php

https://bitbucket.org/baruffaldi/website-2008-computer-shopping-3
PHP | 110 lines | 42 code | 16 blank | 52 comment | 7 complexity | 62dfaf03aea537f0d2ec3471d16c4f09 MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Pdf
 17 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 19 */
 20
 21
 22/** Zend_Pdf_Element_String */
 23require_once 'Zend/Pdf/Element/String.php';
 24
 25
 26/**
 27 * PDF file 'binary string' element implementation
 28 *
 29 * @category   Zend
 30 * @package    Zend_Pdf
 31 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 32 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 33 */
 34class Zend_Pdf_Element_String_Binary extends Zend_Pdf_Element_String
 35{
 36    /**
 37     * Object value
 38     *
 39     * @var string
 40     */
 41    public $value;
 42
 43
 44    /**
 45     * Escape string according to the PDF rules
 46     *
 47     * @param string $inStr
 48     * @return string
 49     */
 50    public static function escape($inStr)
 51    {
 52        $outStr = '';
 53
 54        for ($count = 0; $count < strlen($inStr); $count++) {
 55            $outStr .= sprintf('%02X', ord($inStr[$count]));
 56        }
 57        return $outStr;
 58    }
 59
 60
 61    /**
 62     * Unescape string according to the PDF rules
 63     *
 64     * @param string $inStr
 65     * @return string
 66     */
 67    public static function unescape($inStr)
 68    {
 69        $outStr = '';
 70        $nextHexCode = '';
 71
 72        for ($count = 0; $count < strlen($inStr); $count++) {
 73            $nextCharCode = ord($inStr[$count]);
 74
 75            if( ($nextCharCode >= 48  /*'0'*/ &&
 76                 $nextCharCode <= 57  /*'9'*/   ) ||
 77                ($nextCharCode >= 97  /*'a'*/ &&
 78                 $nextCharCode <= 102 /*'f'*/   ) ||
 79                ($nextCharCode >= 65  /*'A'*/ &&
 80                 $nextCharCode <= 70  /*'F'*/   ) ) {
 81                $nextHexCode .= $inStr[$count];
 82            }
 83
 84            if (strlen($nextHexCode) == 2) {
 85                $outStr .= chr(intval($nextHexCode, 16));
 86                $nextHexCode = '';
 87            }
 88        }
 89
 90        if ($nextHexCode != '') {
 91            // We have odd number of digits.
 92            // Final digit is assumed to be '0'
 93            $outStr .= chr(base_convert($nextHexCode . '0', 16, 10));
 94        }
 95
 96        return $outStr;
 97    }
 98
 99
100    /**
101     * Return object as string
102     *
103     * @param Zend_Pdf_Factory $factory
104     * @return string
105     */
106    public function toString($factory = null)
107    {
108        return '<' . self::escape((string)$this->value) . '>';
109    }
110}