/library/Zend/Pdf/Element.php

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