PageRenderTime 19ms CodeModel.GetById 8ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://zoop.googlecode.com/
PHP | 189 lines | 80 code | 26 blank | 83 comment | 9 complexity | e9c134c323f1d71658faa76a8e7e5ec6 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: Dictionary.php 20096 2010-01-06 02:05:09Z bkarwin $
 20 */
 21
 22
 23/** Internally used classes */
 24require_once 'Zend/Pdf/Element/Name.php';
 25
 26
 27/** Zend_Pdf_Element */
 28require_once 'Zend/Pdf/Element.php';
 29
 30/**
 31 * PDF file 'dictionary' element implementation
 32 *
 33 * @category   Zend
 34 * @package    Zend_Pdf
 35 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 36 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 37 */
 38class Zend_Pdf_Element_Dictionary extends Zend_Pdf_Element
 39{
 40    /**
 41     * Dictionary elements
 42     * Array of Zend_Pdf_Element objects ('name' => Zend_Pdf_Element)
 43     *
 44     * @var array
 45     */
 46    private $_items = array();
 47
 48
 49    /**
 50     * Object constructor
 51     *
 52     * @param array $val   - array of Zend_Pdf_Element objects
 53     * @throws Zend_Pdf_Exception
 54     */
 55    public function __construct($val = null)
 56    {
 57        if ($val === null) {
 58            return;
 59        } else if (!is_array($val)) {
 60            require_once 'Zend/Pdf/Exception.php';
 61            throw new Zend_Pdf_Exception('Argument must be an array');
 62        }
 63
 64        foreach ($val as $name => $element) {
 65            if (!$element instanceof Zend_Pdf_Element) {
 66                require_once 'Zend/Pdf/Exception.php';
 67                throw new Zend_Pdf_Exception('Array elements must be Zend_Pdf_Element objects');
 68            }
 69            if (!is_string($name)) {
 70                require_once 'Zend/Pdf/Exception.php';
 71                throw new Zend_Pdf_Exception('Array keys must be strings');
 72            }
 73            $this->_items[$name] = $element;
 74        }
 75    }
 76
 77
 78    /**
 79     * Add element to an array
 80     *
 81     * @name Zend_Pdf_Element_Name $name
 82     * @param Zend_Pdf_Element $val   - Zend_Pdf_Element object
 83     * @throws Zend_Pdf_Exception
 84     */
 85    public function add(Zend_Pdf_Element_Name $name, Zend_Pdf_Element $val)
 86    {
 87        $this->_items[$name->value] = $val;
 88    }
 89
 90    /**
 91     * Return dictionary keys
 92     *
 93     * @return array
 94     */
 95    public function getKeys()
 96    {
 97        return array_keys($this->_items);
 98    }
 99
100
101    /**
102     * Get handler
103     *
104     * @param string $property
105     * @return Zend_Pdf_Element | null
106     */
107    public function __get($item)
108    {
109        $element = isset($this->_items[$item]) ? $this->_items[$item]
110                                               : null;
111
112        return $element;
113    }
114
115    /**
116     * Set handler
117     *
118     * @param string $property
119     * @param  mixed $value
120     */
121    public function __set($item, $value)
122    {
123        if ($value === null) {
124            unset($this->_items[$item]);
125        } else {
126            $this->_items[$item] = $value;
127        }
128    }
129
130    /**
131     * Return type of the element.
132     *
133     * @return integer
134     */
135    public function getType()
136    {
137        return Zend_Pdf_Element::TYPE_DICTIONARY;
138    }
139
140
141    /**
142     * Return object as string
143     *
144     * @param Zend_Pdf_Factory $factory
145     * @return string
146     */
147    public function toString($factory = null)
148    {
149        $outStr = '<<';
150        $lastNL = 0;
151
152        foreach ($this->_items as $name => $element) {
153            if (!is_object($element)) {
154                require_once 'Zend/Pdf/Exception.php';
155                throw new Zend_Pdf_Exception('Wrong data');
156            }
157
158            if (strlen($outStr) - $lastNL > 128)  {
159                $outStr .= "\n";
160                $lastNL = strlen($outStr);
161            }
162
163            $nameObj = new Zend_Pdf_Element_Name($name);
164            $outStr .= $nameObj->toString($factory) . ' ' . $element->toString($factory) . ' ';
165        }
166        $outStr .= '>>';
167
168        return $outStr;
169    }
170
171
172    /**
173     * Convert PDF element to PHP type.
174     *
175     * Dictionary is returned as an associative array
176     *
177     * @return mixed
178     */
179    public function toPhp()
180    {
181        $phpArray = array();
182
183        foreach ($this->_items as $itemName => $item) {
184            $phpArray[$itemName] = $item->toPhp();
185        }
186
187        return $phpArray;
188    }
189}