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

/library/Zend/Pdf/Element/Name.php

https://bitbucket.org/baruffaldi/website-2008-computer-shopping-3
PHP | 159 lines | 66 code | 20 blank | 73 comment | 10 complexity | ec9c81ef39ae8e452f078a35b133ca70 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 */
 23require_once 'Zend/Pdf/Element.php';
 24
 25
 26/**
 27 * PDF file 'name' 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_Name extends Zend_Pdf_Element
 35{
 36    /**
 37     * Object value
 38     *
 39     * @var string
 40     */
 41    public $value;
 42
 43
 44    /**
 45     * Object constructor
 46     *
 47     * @param string $val
 48     * @throws Zend_Pdf_Exception
 49     */
 50    public function __construct($val)
 51    {
 52        settype($val, 'string');
 53        if (strpos($val,"\x00") !== false) {
 54            throw new Zend_Pdf_Exception('Null character is not allowed in PDF Names');
 55        }
 56        $this->value   = (string)$val;
 57    }
 58
 59
 60    /**
 61     * Return type of the element.
 62     *
 63     * @return integer
 64     */
 65    public function getType()
 66    {
 67        return Zend_Pdf_Element::TYPE_NAME;
 68    }
 69
 70
 71    /**
 72     * Escape string according to the PDF rules
 73     *
 74     * @param string $inStr
 75     * @return string
 76     */
 77    public static function escape($inStr)
 78    {
 79        $outStr = '';
 80
 81        for ($count = 0; $count < strlen($inStr); $count++) {
 82            $nextCode = ord($inStr[$count]);
 83
 84            switch ($inStr[$count]) {
 85                case '(':
 86                // fall through to next case
 87                case ')':
 88                // fall through to next case
 89                case '<':
 90                // fall through to next case
 91                case '>':
 92                // fall through to next case
 93                case '[':
 94                // fall through to next case
 95                case ']':
 96                // fall through to next case
 97                case '{':
 98                // fall through to next case
 99                case '}':
100                // fall through to next case
101                case '/':
102                // fall through to next case
103                case '%':
104                // fall through to next case
105                case '\\':
106                // fall through to next case
107                case '#':
108                    $outStr .= sprintf('#%02X', $nextCode);
109                    break;
110
111                default:
112                    if ($nextCode >= 33 && $nextCode <= 126 ) {
113                        // Visible ASCII symbol
114                        $outStr .= $inStr[$count];
115                    } else {
116                        $outStr .= sprintf('#%02X', $nextCode);
117                    }
118            }
119
120        }
121
122        return $outStr;
123    }
124
125
126    /**
127     * Unescape string according to the PDF rules
128     *
129     * @param string $inStr
130     * @return string
131     */
132    public static function unescape($inStr)
133    {
134        $outStr = '';
135
136        for ($count = 0; $count < strlen($inStr); $count++) {
137            if ($inStr[$count] != '#' )  {
138                $outStr .= $inStr[$count];
139            } else {
140                // Escape sequence
141                $outStr .= chr(base_convert(substr($inStr, $count+1, 2), 16, 10 ));
142                $count +=2;
143            }
144        }
145        return $outStr;
146    }
147
148
149    /**
150     * Return object as string
151     *
152     * @param Zend_Pdf_Factory $factory
153     * @return string
154     */
155    public function toString($factory = null)
156    {
157        return '/' . self::escape((string)$this->value);
158    }
159}