PageRenderTime 143ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/Pdf/Element/Name.php

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