PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Pdf/Element/Name.php

https://bitbucket.org/baruffaldi/website-2008-computer-shopping-3
PHP | 159 lines | 66 code | 20 blank | 73 comment | 10 complexity | ec9c81ef39ae8e452f078a35b133ca70 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Pdf_Element */
  21. require_once 'Zend/Pdf/Element.php';
  22. /**
  23. * PDF file 'name' element implementation
  24. *
  25. * @category Zend
  26. * @package Zend_Pdf
  27. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Pdf_Element_Name extends Zend_Pdf_Element
  31. {
  32. /**
  33. * Object value
  34. *
  35. * @var string
  36. */
  37. public $value;
  38. /**
  39. * Object constructor
  40. *
  41. * @param string $val
  42. * @throws Zend_Pdf_Exception
  43. */
  44. public function __construct($val)
  45. {
  46. settype($val, 'string');
  47. if (strpos($val,"\x00") !== false) {
  48. throw new Zend_Pdf_Exception('Null character is not allowed in PDF Names');
  49. }
  50. $this->value = (string)$val;
  51. }
  52. /**
  53. * Return type of the element.
  54. *
  55. * @return integer
  56. */
  57. public function getType()
  58. {
  59. return Zend_Pdf_Element::TYPE_NAME;
  60. }
  61. /**
  62. * Escape string according to the PDF rules
  63. *
  64. * @param string $inStr
  65. * @return string
  66. */
  67. public static function escape($inStr)
  68. {
  69. $outStr = '';
  70. for ($count = 0; $count < strlen($inStr); $count++) {
  71. $nextCode = ord($inStr[$count]);
  72. switch ($inStr[$count]) {
  73. case '(':
  74. // fall through to next case
  75. case ')':
  76. // fall through to next case
  77. case '<':
  78. // fall through to next case
  79. case '>':
  80. // fall through to next case
  81. case '[':
  82. // fall through to next case
  83. case ']':
  84. // fall through to next case
  85. case '{':
  86. // fall through to next case
  87. case '}':
  88. // fall through to next case
  89. case '/':
  90. // fall through to next case
  91. case '%':
  92. // fall through to next case
  93. case '\\':
  94. // fall through to next case
  95. case '#':
  96. $outStr .= sprintf('#%02X', $nextCode);
  97. break;
  98. default:
  99. if ($nextCode >= 33 && $nextCode <= 126 ) {
  100. // Visible ASCII symbol
  101. $outStr .= $inStr[$count];
  102. } else {
  103. $outStr .= sprintf('#%02X', $nextCode);
  104. }
  105. }
  106. }
  107. return $outStr;
  108. }
  109. /**
  110. * Unescape string according to the PDF rules
  111. *
  112. * @param string $inStr
  113. * @return string
  114. */
  115. public static function unescape($inStr)
  116. {
  117. $outStr = '';
  118. for ($count = 0; $count < strlen($inStr); $count++) {
  119. if ($inStr[$count] != '#' ) {
  120. $outStr .= $inStr[$count];
  121. } else {
  122. // Escape sequence
  123. $outStr .= chr(base_convert(substr($inStr, $count+1, 2), 16, 10 ));
  124. $count +=2;
  125. }
  126. }
  127. return $outStr;
  128. }
  129. /**
  130. * Return object as string
  131. *
  132. * @param Zend_Pdf_Factory $factory
  133. * @return string
  134. */
  135. public function toString($factory = null)
  136. {
  137. return '/' . self::escape((string)$this->value);
  138. }
  139. }