PageRenderTime 27ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/zend/Zend/Pdf/Element/Name.php

http://zoop.googlecode.com/
PHP | 161 lines | 67 code | 20 blank | 74 comment | 10 complexity | 6a9c63c3512fa647074290b07cd9fb69 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
 18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 19 * @version    $Id: Name.php 20096 2010-01-06 02:05:09Z bkarwin $
 20 */
 21
 22
 23/** Zend_Pdf_Element */
 24require_once 'Zend/Pdf/Element.php';
 25
 26
 27/**
 28 * PDF file 'name' element implementation
 29 *
 30 * @category   Zend
 31 * @package    Zend_Pdf
 32 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 33 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 34 */
 35class Zend_Pdf_Element_Name extends Zend_Pdf_Element
 36{
 37    /**
 38     * Object value
 39     *
 40     * @var string
 41     */
 42    public $value;
 43
 44
 45    /**
 46     * Object constructor
 47     *
 48     * @param string $val
 49     * @throws Zend_Pdf_Exception
 50     */
 51    public function __construct($val)
 52    {
 53        settype($val, 'string');
 54        if (strpos($val,"\x00") !== false) {
 55            require_once 'Zend/Pdf/Exception.php';
 56            throw new Zend_Pdf_Exception('Null character is not allowed in PDF Names');
 57        }
 58        $this->value   = (string)$val;
 59    }
 60
 61
 62    /**
 63     * Return type of the element.
 64     *
 65     * @return integer
 66     */
 67    public function getType()
 68    {
 69        return Zend_Pdf_Element::TYPE_NAME;
 70    }
 71
 72
 73    /**
 74     * Escape string according to the PDF rules
 75     *
 76     * @param string $inStr
 77     * @return string
 78     */
 79    public static function escape($inStr)
 80    {
 81        $outStr = '';
 82
 83        for ($count = 0; $count < strlen($inStr); $count++) {
 84            $nextCode = ord($inStr[$count]);
 85
 86            switch ($inStr[$count]) {
 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                // fall through to next case
109                case '#':
110                    $outStr .= sprintf('#%02X', $nextCode);
111                    break;
112
113                default:
114                    if ($nextCode >= 33 && $nextCode <= 126 ) {
115                        // Visible ASCII symbol
116                        $outStr .= $inStr[$count];
117                    } else {
118                        $outStr .= sprintf('#%02X', $nextCode);
119                    }
120            }
121
122        }
123
124        return $outStr;
125    }
126
127
128    /**
129     * Unescape string according to the PDF rules
130     *
131     * @param string $inStr
132     * @return string
133     */
134    public static function unescape($inStr)
135    {
136        $outStr = '';
137
138        for ($count = 0; $count < strlen($inStr); $count++) {
139            if ($inStr[$count] != '#' )  {
140                $outStr .= $inStr[$count];
141            } else {
142                // Escape sequence
143                $outStr .= chr(base_convert(substr($inStr, $count+1, 2), 16, 10 ));
144                $count +=2;
145            }
146        }
147        return $outStr;
148    }
149
150
151    /**
152     * Return object as string
153     *
154     * @param Zend_Pdf_Factory $factory
155     * @return string
156     */
157    public function toString($factory = null)
158    {
159        return '/' . self::escape((string)$this->value);
160    }
161}