web/lib/Zend/Pdf/Element.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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: Element.php 22797 2010-08-06 15:02:12Z alexander $
       
    20  */
       
    21 
       
    22 
       
    23 /**
       
    24  * PDF file element implementation
       
    25  *
       
    26  * @package    Zend_Pdf
       
    27  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    28  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    29  */
       
    30 abstract class Zend_Pdf_Element
       
    31 {
       
    32     const TYPE_BOOL        = 1;
       
    33     const TYPE_NUMERIC     = 2;
       
    34     const TYPE_STRING      = 3;
       
    35     const TYPE_NAME        = 4;
       
    36     const TYPE_ARRAY       = 5;
       
    37     const TYPE_DICTIONARY  = 6;
       
    38     const TYPE_STREAM      = 7;
       
    39     const TYPE_NULL        = 11;
       
    40 
       
    41     /**
       
    42      * Reference to the top level indirect object, which contains this element.
       
    43      *
       
    44      * @var Zend_Pdf_Element_Object
       
    45      */
       
    46     private $_parentObject = null;
       
    47 
       
    48     /**
       
    49      * Return type of the element.
       
    50      * See ZPdfPDFConst for possible values
       
    51      *
       
    52      * @return integer
       
    53      */
       
    54     abstract public function getType();
       
    55 
       
    56     /**
       
    57      * Convert element to a string, which can be directly
       
    58      * written to a PDF file.
       
    59      *
       
    60      * $factory parameter defines operation context.
       
    61      *
       
    62      * @param Zend_Pdf_Factory $factory
       
    63      * @return string
       
    64      */
       
    65     abstract public function toString($factory = null);
       
    66 
       
    67     const CLONE_MODE_SKIP_PAGES    = 1; // Do not follow pages during deep copy process
       
    68     const CLONE_MODE_FORCE_CLONING = 2; // Force top level object cloning even it's already processed
       
    69 
       
    70     /**
       
    71      * Detach PDF object from the factory (if applicable), clone it and attach to new factory.
       
    72      *
       
    73      * @todo It's nevessry to check if SplObjectStorage class works faster
       
    74      * (Needs PHP 5.3.x to attach object _with_ additional data to storage)
       
    75      *
       
    76      * @param Zend_Pdf_ElementFactory $factory  The factory to attach
       
    77      * @param array &$processed List of already processed indirect objects, used to avoid objects duplication
       
    78      * @param integer $mode  Cloning mode (defines filter for objects cloning)
       
    79      * @returns Zend_Pdf_Element
       
    80      */
       
    81     public function makeClone(Zend_Pdf_ElementFactory $factory, array &$processed, $mode)
       
    82     {
       
    83         return clone $this;
       
    84     }
       
    85 
       
    86     /**
       
    87      * Set top level parent indirect object.
       
    88      *
       
    89      * @param Zend_Pdf_Element_Object $parent
       
    90      */
       
    91     public function setParentObject(Zend_Pdf_Element_Object $parent)
       
    92     {
       
    93         $this->_parentObject = $parent;
       
    94     }
       
    95 
       
    96 
       
    97     /**
       
    98      * Get top level parent indirect object.
       
    99      *
       
   100      * @return Zend_Pdf_Element_Object
       
   101      */
       
   102     public function getParentObject()
       
   103     {
       
   104         return $this->_parentObject;
       
   105     }
       
   106 
       
   107 
       
   108     /**
       
   109      * Mark object as modified, to include it into new PDF file segment.
       
   110      *
       
   111      * We don't automate this action to keep control on PDF update process.
       
   112      * All new objects are treated as "modified" automatically.
       
   113      */
       
   114     public function touch()
       
   115     {
       
   116         if ($this->_parentObject !== null) {
       
   117             $this->_parentObject->touch();
       
   118         }
       
   119     }
       
   120 
       
   121     /**
       
   122      * Clean up resources, used by object
       
   123      */
       
   124     public function cleanUp()
       
   125     {
       
   126         // Do nothing
       
   127     }
       
   128 
       
   129     /**
       
   130      * Convert PDF element to PHP type.
       
   131      *
       
   132      * @return mixed
       
   133      */
       
   134     public function toPhp()
       
   135     {
       
   136         return $this->value;
       
   137     }
       
   138 
       
   139     /**
       
   140      * Convert PHP value into PDF element.
       
   141      *
       
   142      * @param mixed $input
       
   143      * @return Zend_Pdf_Element
       
   144      */
       
   145     public static function phpToPdf($input)
       
   146     {
       
   147         if (is_numeric($input)) {
       
   148             require_once 'Zend/Pdf/Element/Numeric.php';
       
   149             return new Zend_Pdf_Element_Numeric($input);
       
   150         } else if (is_bool($input)) {
       
   151             require_once 'Zend/Pdf/Element/Boolean.php';
       
   152             return new Zend_Pdf_Element_Boolean($input);
       
   153         } else if (is_array($input)) {
       
   154             $pdfElementsArray = array();
       
   155             $isDictionary = false;
       
   156 
       
   157             foreach ($input as $key => $value) {
       
   158                 if (is_string($key)) {
       
   159                     $isDictionary = true;
       
   160                 }
       
   161                 $pdfElementsArray[$key] = Zend_Pdf_Element::phpToPdf($value);
       
   162             }
       
   163 
       
   164             if ($isDictionary) {
       
   165                 require_once 'Zend/Pdf/Element/Dictionary.php';
       
   166                 return new Zend_Pdf_Element_Dictionary($pdfElementsArray);
       
   167             } else {
       
   168                 require_once 'Zend/Pdf/Element/Array.php';
       
   169                 return new Zend_Pdf_Element_Array($pdfElementsArray);
       
   170             }
       
   171         } else {
       
   172             require_once 'Zend/Pdf/Element/String.php';
       
   173             return new Zend_Pdf_Element_String((string)$input);
       
   174         }
       
   175     }
       
   176 }