Element.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_Object */
  22. require_once 'Zend/Pdf/Element/Object.php';
  23. /**
  24. * PDF file element implementation
  25. *
  26. * @package Zend_Pdf
  27. * @copyright Copyright (c) 2005-2009 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. * Reference to the top level indirect object, which contains this element.
  42. *
  43. * @var Zend_Pdf_Element_Object
  44. */
  45. private $_parentObject = null;
  46. /**
  47. * Return type of the element.
  48. * See ZPdfPDFConst for possible values
  49. *
  50. * @return integer
  51. */
  52. abstract public function getType();
  53. /**
  54. * Convert element to a string, which can be directly
  55. * written to a PDF file.
  56. *
  57. * $factory parameter defines operation context.
  58. *
  59. * @param Zend_Pdf_Factory $factory
  60. * @return string
  61. */
  62. abstract public function toString($factory = null);
  63. /**
  64. * Set top level parent indirect object.
  65. *
  66. * @param Zend_Pdf_Element_Object $parent
  67. */
  68. public function setParentObject(Zend_Pdf_Element_Object $parent)
  69. {
  70. $this->_parentObject = $parent;
  71. }
  72. /**
  73. * Get top level parent indirect object.
  74. *
  75. * @return Zend_Pdf_Element_Object
  76. */
  77. public function getParentObject()
  78. {
  79. return $this->_parentObject;
  80. }
  81. /**
  82. * Mark object as modified, to include it into new PDF file segment.
  83. *
  84. * We don't automate this action to keep control on PDF update process.
  85. * All new objects are treated as "modified" automatically.
  86. */
  87. public function touch()
  88. {
  89. if ($this->_parentObject !== null) {
  90. $this->_parentObject->touch();
  91. }
  92. }
  93. /**
  94. * Clean up resources, used by object
  95. */
  96. public function cleanUp()
  97. {
  98. // Do nothing
  99. }
  100. /**
  101. * Convert PDF element to PHP type.
  102. *
  103. * @return mixed
  104. */
  105. public function toPhp()
  106. {
  107. return $this->value;
  108. }
  109. /**
  110. * Convert PHP value into PDF element.
  111. *
  112. * @param mixed $input
  113. * @return Zend_Pdf_Element
  114. */
  115. public static function phpToPdf($input)
  116. {
  117. if (is_numeric($input)) {
  118. return new Zend_Pdf_Element_Numeric($input);
  119. } else if (is_bool($input)) {
  120. return new Zend_Pdf_Element_Boolean($input);
  121. } else if (is_array($input)) {
  122. $pdfElementsArray = array();
  123. $isDictionary = false;
  124. foreach ($input as $key => $value) {
  125. if (is_string($key)) {
  126. $isDictionary = true;
  127. }
  128. $pdfElementsArray[$key] = Zend_Pdf_Element::phpToPdf($value);
  129. }
  130. if ($isDictionary) {
  131. return new Zend_Pdf_Element_Dictionary($pdfElementsArray);
  132. } else {
  133. return new Zend_Pdf_Element_Array($pdfElementsArray);
  134. }
  135. } else {
  136. return new Zend_Pdf_Element_String((string)$input);
  137. }
  138. }
  139. }