/framework/vendor/zend/Zend/Pdf/Element.php

http://zoop.googlecode.com/ · PHP · 158 lines · 65 code · 16 blank · 77 comment · 10 complexity · 49902dce92a798871f2946ef3d17c2a0 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-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 20096 2010-01-06 02:05:09Z bkarwin $
  20. */
  21. /**
  22. * PDF file element implementation
  23. *
  24. * @package Zend_Pdf
  25. * @copyright Copyright (c) 2005-2010 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. * @return string
  59. */
  60. abstract public function toString($factory = null);
  61. /**
  62. * Set top level parent indirect object.
  63. *
  64. * @param Zend_Pdf_Element_Object $parent
  65. */
  66. public function setParentObject(Zend_Pdf_Element_Object $parent)
  67. {
  68. $this->_parentObject = $parent;
  69. }
  70. /**
  71. * Get top level parent indirect object.
  72. *
  73. * @return Zend_Pdf_Element_Object
  74. */
  75. public function getParentObject()
  76. {
  77. return $this->_parentObject;
  78. }
  79. /**
  80. * Mark object as modified, to include it into new PDF file segment.
  81. *
  82. * We don't automate this action to keep control on PDF update process.
  83. * All new objects are treated as "modified" automatically.
  84. */
  85. public function touch()
  86. {
  87. if ($this->_parentObject !== null) {
  88. $this->_parentObject->touch();
  89. }
  90. }
  91. /**
  92. * Clean up resources, used by object
  93. */
  94. public function cleanUp()
  95. {
  96. // Do nothing
  97. }
  98. /**
  99. * Convert PDF element to PHP type.
  100. *
  101. * @return mixed
  102. */
  103. public function toPhp()
  104. {
  105. return $this->value;
  106. }
  107. /**
  108. * Convert PHP value into PDF element.
  109. *
  110. * @param mixed $input
  111. * @return Zend_Pdf_Element
  112. */
  113. public static function phpToPdf($input)
  114. {
  115. if (is_numeric($input)) {
  116. require_once 'Zend/Pdf/Element/Numeric.php';
  117. return new Zend_Pdf_Element_Numeric($input);
  118. } else if (is_bool($input)) {
  119. require_once 'Zend/Pdf/Element/Boolean.php';
  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. require_once 'Zend/Pdf/Element/Dictionary.php';
  132. return new Zend_Pdf_Element_Dictionary($pdfElementsArray);
  133. } else {
  134. require_once 'Zend/Pdf/Element/Array.php';
  135. return new Zend_Pdf_Element_Array($pdfElementsArray);
  136. }
  137. } else {
  138. require_once 'Zend/Pdf/Element/String.php';
  139. return new Zend_Pdf_Element_String((string)$input);
  140. }
  141. }
  142. }