Element.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /** Zend_Pdf_Element */
  22. require_once 'Zend/Pdf/Element.php';
  23. /** Zend_Pdf_Element_Object */
  24. require_once 'Zend/Pdf/Element/Object.php';
  25. /**
  26. * PDF file element implementation
  27. *
  28. * @package Zend_Pdf
  29. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. abstract class Zend_Pdf_Element
  33. {
  34. const TYPE_BOOL = 1;
  35. const TYPE_NUMERIC = 2;
  36. const TYPE_STRING = 3;
  37. const TYPE_NAME = 4;
  38. const TYPE_ARRAY = 5;
  39. const TYPE_DICTIONARY = 6;
  40. const TYPE_STREAM = 7;
  41. const TYPE_NULL = 11;
  42. /**
  43. * Reference to the top level indirect object, which contains this element.
  44. *
  45. * @var Zend_Pdf_Element_Object
  46. */
  47. private $_parentObject = null;
  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. * Convert element to a string, which can be directly
  57. * written to a PDF file.
  58. *
  59. * $factory parameter defines operation context.
  60. *
  61. * @param Zend_Pdf_Factory $factory
  62. * @return string
  63. */
  64. abstract public function toString($factory = null);
  65. /**
  66. * Set top level parent indirect object.
  67. *
  68. * @param Zend_Pdf_Element_Object $parent
  69. */
  70. public function setParentObject(Zend_Pdf_Element_Object &$parent)
  71. {
  72. $this->_parentObject = &$parent;
  73. }
  74. /**
  75. * Get top level parent indirect object.
  76. *
  77. * @return Zend_Pdf_Element_Object
  78. */
  79. public function getParentObject()
  80. {
  81. return $this->_parentObject;
  82. }
  83. /**
  84. * Mark object as modified, to include it into new PDF file segment.
  85. *
  86. * We don't automate this action to keep control on PDF update process.
  87. * All new objects are treated as "modified" automatically.
  88. */
  89. public function touch()
  90. {
  91. if ($this->_parentObject !== null) {
  92. $this->_parentObject->touch();
  93. }
  94. }
  95. /**
  96. * Clean up resources, used by object
  97. */
  98. public function cleanUp()
  99. {
  100. // Do nothing
  101. }
  102. /**
  103. * Convert PDF element to PHP type.
  104. *
  105. * @return mixed
  106. */
  107. public function toPhp()
  108. {
  109. return $this->value;
  110. }
  111. /**
  112. * Convert PHP value into PDF element.
  113. *
  114. * @param mixed $input
  115. * @return Zend_Pdf_Element
  116. */
  117. public static function phpToPdf($input)
  118. {
  119. if (is_numeric($input)) {
  120. return new Zend_Pdf_Element_Numeric($input);
  121. } else if (is_bool($input)) {
  122. return new Zend_Pdf_Element_Boolean($input);
  123. } else if (is_array($input)) {
  124. $pdfElementsArray = array();
  125. $isDictionary = false;
  126. foreach ($input as $key => $value) {
  127. if (is_string($key)) {
  128. $isDictionary = true;
  129. }
  130. $pdfElementsArray[$key] = Zend_Pdf_Element::phpToPdf($value);
  131. }
  132. if ($isDictionary) {
  133. return new Zend_Pdf_Element_Dictionary($pdfElementsArray);
  134. } else {
  135. return new Zend_Pdf_Element_Array($pdfElementsArray);
  136. }
  137. } else {
  138. return new Zend_Pdf_Element_String((string)$input);
  139. }
  140. }
  141. }