File indexing completed on 2025-01-19 05:21:21

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Pdf
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 
0023 /** Internally used classes */
0024 // require_once 'Zend/Pdf/Element/Name.php';
0025 
0026 
0027 /** Zend_Pdf_Element */
0028 // require_once 'Zend/Pdf/Element.php';
0029 
0030 /**
0031  * PDF file 'dictionary' element implementation
0032  *
0033  * @category   Zend
0034  * @package    Zend_Pdf
0035  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0036  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0037  */
0038 class Zend_Pdf_Element_Dictionary extends Zend_Pdf_Element
0039 {
0040     /**
0041      * Dictionary elements
0042      * Array of Zend_Pdf_Element objects ('name' => Zend_Pdf_Element)
0043      *
0044      * @var array
0045      */
0046     private $_items = array();
0047 
0048 
0049     /**
0050      * Object constructor
0051      *
0052      * @param array $val   - array of Zend_Pdf_Element objects
0053      * @throws Zend_Pdf_Exception
0054      */
0055     public function __construct($val = null)
0056     {
0057         if ($val === null) {
0058             return;
0059         } else if (!is_array($val)) {
0060             // require_once 'Zend/Pdf/Exception.php';
0061             throw new Zend_Pdf_Exception('Argument must be an array');
0062         }
0063 
0064         foreach ($val as $name => $element) {
0065             if (!$element instanceof Zend_Pdf_Element) {
0066                 // require_once 'Zend/Pdf/Exception.php';
0067                 throw new Zend_Pdf_Exception('Array elements must be Zend_Pdf_Element objects');
0068             }
0069             if (!is_string($name)) {
0070                 // require_once 'Zend/Pdf/Exception.php';
0071                 throw new Zend_Pdf_Exception('Array keys must be strings');
0072             }
0073             $this->_items[$name] = $element;
0074         }
0075     }
0076 
0077 
0078     /**
0079      * Add element to an array
0080      *
0081      * @name Zend_Pdf_Element_Name $name
0082      * @param Zend_Pdf_Element $val   - Zend_Pdf_Element object
0083      * @throws Zend_Pdf_Exception
0084      */
0085     public function add(Zend_Pdf_Element_Name $name, Zend_Pdf_Element $val)
0086     {
0087         $this->_items[$name->value] = $val;
0088     }
0089 
0090     /**
0091      * Return dictionary keys
0092      *
0093      * @return array
0094      */
0095     public function getKeys()
0096     {
0097         return array_keys($this->_items);
0098     }
0099 
0100 
0101     /**
0102      * Get handler
0103      *
0104      * @param string $property
0105      * @return Zend_Pdf_Element | null
0106      */
0107     public function __get($item)
0108     {
0109         $element = isset($this->_items[$item]) ? $this->_items[$item]
0110                                                : null;
0111 
0112         return $element;
0113     }
0114 
0115     /**
0116      * Set handler
0117      *
0118      * @param string $property
0119      * @param  mixed $value
0120      */
0121     public function __set($item, $value)
0122     {
0123         if ($value === null) {
0124             unset($this->_items[$item]);
0125         } else {
0126             $this->_items[$item] = $value;
0127         }
0128     }
0129 
0130     /**
0131      * Return type of the element.
0132      *
0133      * @return integer
0134      */
0135     public function getType()
0136     {
0137         return Zend_Pdf_Element::TYPE_DICTIONARY;
0138     }
0139 
0140 
0141     /**
0142      * Return object as string
0143      *
0144      * @param Zend_Pdf_Factory $factory
0145      * @return string
0146      */
0147     public function toString($factory = null)
0148     {
0149         $outStr = '<<';
0150         $lastNL = 0;
0151 
0152         foreach ($this->_items as $name => $element) {
0153             if (!is_object($element)) {
0154                 // require_once 'Zend/Pdf/Exception.php';
0155                 throw new Zend_Pdf_Exception('Wrong data');
0156             }
0157 
0158             if (strlen($outStr) - $lastNL > 128)  {
0159                 $outStr .= "\n";
0160                 $lastNL = strlen($outStr);
0161             }
0162 
0163             $nameObj = new Zend_Pdf_Element_Name($name);
0164             $outStr .= $nameObj->toString($factory) . ' ' . $element->toString($factory) . ' ';
0165         }
0166         $outStr .= '>>';
0167 
0168         return $outStr;
0169     }
0170 
0171     /**
0172      * Detach PDF object from the factory (if applicable), clone it and attach to new factory.
0173      *
0174      * @param Zend_Pdf_ElementFactory $factory  The factory to attach
0175      * @param array &$processed  List of already processed indirect objects, used to avoid objects duplication
0176      * @param integer $mode  Cloning mode (defines filter for objects cloning)
0177      * @returns Zend_Pdf_Element
0178      * @throws Zend_Pdf_Exception
0179      */
0180     public function makeClone(Zend_Pdf_ElementFactory $factory, array &$processed, $mode)
0181     {
0182         if (isset($this->_items['Type'])) {
0183             if ($this->_items['Type']->value == 'Pages') {
0184                 // It's a page tree node
0185                 // skip it and its children
0186                 return new Zend_Pdf_Element_Null();
0187             }
0188 
0189             if ($this->_items['Type']->value == 'Page'  &&
0190                 $mode == Zend_Pdf_Element::CLONE_MODE_SKIP_PAGES
0191             ) {
0192                 // It's a page node, skip it
0193                 return new Zend_Pdf_Element_Null();
0194             }
0195         }
0196 
0197         $newDictionary = new self();
0198         foreach ($this->_items as $key => $value) {
0199             $newDictionary->_items[$key] = $value->makeClone($factory, $processed, $mode);
0200         }
0201 
0202         return $newDictionary;
0203     }
0204 
0205     /**
0206      * Set top level parent indirect object.
0207      *
0208      * @param Zend_Pdf_Element_Object $parent
0209      */
0210     public function setParentObject(Zend_Pdf_Element_Object $parent)
0211     {
0212         parent::setParentObject($parent);
0213 
0214         foreach ($this->_items as $item) {
0215             $item->setParentObject($parent);
0216         }
0217     }
0218 
0219     /**
0220      * Convert PDF element to PHP type.
0221      *
0222      * Dictionary is returned as an associative array
0223      *
0224      * @return mixed
0225      */
0226     public function toPhp()
0227     {
0228         $phpArray = array();
0229 
0230         foreach ($this->_items as $itemName => $item) {
0231             $phpArray[$itemName] = $item->toPhp();
0232         }
0233 
0234         return $phpArray;
0235     }
0236 }