/library/Zend/Pdf/InternalType/AbstractTypeObject.php

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