PageRenderTime 24ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

/jay/net/xp_framework/tools/vm/util/NameMapping.class.php

https://github.com/thekid/xp-experiments
PHP | 145 lines | 74 code | 17 blank | 54 comment | 15 complexity | 8208840b993e620de802e97ecbc7bed8 MD5 | raw file
  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. /**
  7. * Maps names and types
  8. *
  9. * @purpose Utility
  10. */
  11. class NameMapping extends Object {
  12. public
  13. $mapping= array(
  14. 'xp' => 'xp',
  15. 'parent' => 'parent',
  16. 'self' => 'self',
  17. ),
  18. $current= NULL,
  19. $namespaceSeparator= '.';
  20. /**
  21. * Set namespace separator
  22. *
  23. * @param string s new value
  24. */
  25. public function setNamespaceSeparator($s) {
  26. $this->namespaceSeparator= $s;
  27. }
  28. /**
  29. * Add a mapping
  30. *
  31. * @param string key
  32. * @param string value
  33. */
  34. public function addMapping($key, $value) {
  35. $this->mapping[strtolower($key)]= $value;
  36. }
  37. /**
  38. * Retrieve a mapping
  39. *
  40. * @param string key
  41. * @return string value
  42. * @throws lang.IllegalArgumentException in case not mapping can be found
  43. */
  44. public function getMapping($key) {
  45. $lookup= strtolower($key);
  46. if (!isset($this->mapping[$lookup])) {
  47. throw new IllegalArgumentException('Mapping for "'.$key.'" not found');
  48. }
  49. return $this->mapping[$lookup];
  50. }
  51. /**
  52. * Set current class
  53. *
  54. * @param text.doclet.ClassDoc c
  55. */
  56. public function setCurrentClass($c) {
  57. $this->current= $c;
  58. }
  59. /**
  60. * Retrieves qualified name of a given short name
  61. *
  62. * @param string
  63. * @return string
  64. * @throws lang.IllegalArgumentException in case not mapping can be found
  65. */
  66. public function qualifiedNameOf($short) {
  67. if (strstr($short, '.')) {
  68. $q= $short;
  69. } else {
  70. $q= $this->getMapping($short);
  71. }
  72. if (!$this->current) return $q;
  73. $current= $this->current->qualifiedName();
  74. if ($current === $q) {
  75. // If this class is the same as the current, use "self" keyword
  76. return 'self';
  77. } else if (substr($current, 0, strrpos($current, '.')) == substr($q, 0, strrpos($q, '.'))) {
  78. // If this class is in the same package as the current omit package name
  79. return substr($q, strrpos($q, '.')+ 1);
  80. }
  81. return $q;
  82. }
  83. /**
  84. * Retrieves packaged name of a given qualified name
  85. *
  86. * @param string q qualified class name
  87. * @return string
  88. */
  89. public function packagedNameOf($q) {
  90. return strtr($q, '.', $this->namespaceSeparator);
  91. }
  92. /**
  93. * Retrieves type name
  94. *
  95. * @param string type
  96. * @param bool arg default FALSE
  97. * @return string
  98. */
  99. public function forType($type, $arg= FALSE) {
  100. static $map= array( // Migrate gettype() style names to var_dump() style names
  101. 'integer' => 'int',
  102. 'double' => 'float',
  103. 'boolean' => 'bool',
  104. );
  105. static $builtin= array(
  106. 'int' => TRUE,
  107. 'float' => TRUE,
  108. 'bool' => TRUE,
  109. 'string' => TRUE,
  110. 'array' => TRUE,
  111. 'mixed' => TRUE,
  112. 'resource' => TRUE
  113. );
  114. $va= ('*' == substr($type, -1) && $arg) ? '...' : '';
  115. $array= (!$va && '[]' == substr($type, -2)) ? '[]' : '';
  116. if (FALSE !== ($generic= strpos($type, '<'))) {
  117. $type= substr($type, 0, $generic);
  118. }
  119. $type= trim($type, '&[]*');
  120. $lookup= strtolower($type);
  121. if (isset($map[$lookup])) $type= $map[$lookup];
  122. if (!isset($builtin[$type])) { // User-defined
  123. $type= $this->packagedNameOf($this->qualifiedNameOf($type));
  124. }
  125. return $type.$va.$array;
  126. }
  127. }
  128. ?>