PageRenderTime 35ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/monica/monica/vendor/zendframework/zendframework/library/Zend/Code/NameInformation.php

https://bitbucket.org/alexandretaz/maniac_divers
PHP | 155 lines | 104 code | 14 blank | 37 comment | 10 complexity | c4e73ca7523192ac8d1d83997cac4d6f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Code;
  10. class NameInformation
  11. {
  12. /**
  13. * @var string
  14. */
  15. protected $namespace = null;
  16. /**
  17. * @var array
  18. */
  19. protected $uses = array();
  20. /**
  21. * @param string $namespace
  22. * @param array $uses
  23. */
  24. public function __construct($namespace = null, array $uses = array())
  25. {
  26. if ($namespace) {
  27. $this->setNamespace($namespace);
  28. }
  29. if ($uses) {
  30. $this->setUses($uses);
  31. }
  32. }
  33. /**
  34. * @param string $namespace
  35. * @return NameInformation
  36. */
  37. public function setNamespace($namespace)
  38. {
  39. $this->namespace = (string) $namespace;
  40. return $this;
  41. }
  42. /**
  43. * @return string
  44. */
  45. public function getNamespace()
  46. {
  47. return $this->namespace;
  48. }
  49. /**
  50. * @return boolean
  51. */
  52. public function hasNamespace()
  53. {
  54. return ($this->namespace != null);
  55. }
  56. /**
  57. * @param array $uses
  58. */
  59. public function setUses(array $uses)
  60. {
  61. $this->uses = array();
  62. $this->addUses($uses);
  63. return $this;
  64. }
  65. /**
  66. * @param array $uses
  67. */
  68. public function addUses(array $uses)
  69. {
  70. foreach ($uses as $use => $as) {
  71. if (is_int($use)) {
  72. $this->addUse($as);
  73. } elseif (is_string($use)) {
  74. $this->addUse($use, $as);
  75. }
  76. }
  77. return $this;
  78. }
  79. /**
  80. * @param array|string $use
  81. * @param string $as
  82. */
  83. public function addUse($use, $as = null)
  84. {
  85. if (is_array($use) && array_key_exists('use', $use) && array_key_exists('as', $use)) {
  86. $uses = $use;
  87. $use = $uses['use'];
  88. $as = $uses['as'];
  89. }
  90. $use = trim($use, '\\');
  91. if ($as === null) {
  92. $as = trim($use, '\\');
  93. $nsSeparatorPosition = strrpos($as, '\\');
  94. if ($nsSeparatorPosition !== false && $nsSeparatorPosition !== 0 && $nsSeparatorPosition != strlen($as)) {
  95. $as = substr($as, $nsSeparatorPosition + 1);
  96. }
  97. }
  98. $this->uses[$use] = $as;
  99. }
  100. /**
  101. * @return array
  102. */
  103. public function getUses()
  104. {
  105. return $this->uses;
  106. }
  107. /**
  108. * @param string $name
  109. * @return string
  110. */
  111. public function resolveName($name)
  112. {
  113. if ($this->namespace && !$this->uses && strlen($name) > 0 && $name{0} != '\\') {
  114. return $this->namespace . '\\' . $name;
  115. }
  116. if (!$this->uses || strlen($name) <= 0 || $name{0} == '\\') {
  117. return ltrim($name, '\\');
  118. }
  119. if ($this->namespace || $this->uses) {
  120. $firstPart = $name;
  121. if (($firstPartEnd = strpos($firstPart, '\\')) !== false) {
  122. $firstPart = substr($firstPart, 0, $firstPartEnd);
  123. } else {
  124. $firstPartEnd = strlen($firstPart);
  125. }
  126. if (($fqns = array_search($firstPart, $this->uses)) !== false) {
  127. return substr_replace($name, $fqns, 0, $firstPartEnd);
  128. }
  129. if ($this->namespace) {
  130. return $this->namespace . '\\' . $name;
  131. }
  132. }
  133. return $name;
  134. }
  135. }