PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/DocBlox/Reflection/Constant.php

https://github.com/androa/Docblox
PHP | 147 lines | 76 code | 16 blank | 55 comment | 11 complexity | 9ce52079ce7c8a771b2de9de0f12cdb5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * DocBlox
  4. *
  5. * PHP Version 5
  6. *
  7. * @category DocBlox
  8. * @package Reflection
  9. * @author Mike van Riel <mike.vanriel@naenius.com>
  10. * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
  11. * @license http://www.opensource.org/licenses/mit-license.php MIT
  12. * @link http://docblox-project.org
  13. */
  14. /**
  15. * Parses a constant definition.
  16. *
  17. * @category DocBlox
  18. * @package Reflection
  19. * @author Mike van Riel <mike.vanriel@naenius.com>
  20. * @license http://www.opensource.org/licenses/mit-license.php MIT
  21. * @link http://docblox-project.org
  22. */
  23. class DocBlox_Reflection_Constant extends DocBlox_Reflection_DocBlockedAbstract
  24. {
  25. /** @var string Contains the value contained in the constant */
  26. protected $value = '';
  27. /**
  28. * Retrieves the generic information.
  29. *
  30. * Finds out what the name and value is of this constant on top of the information found using the
  31. * DocBlox_Reflection_DocBlockedAbstract parent method.
  32. *
  33. * @param DocBlox_Token_Iterator $tokens
  34. *
  35. * @see DocBlox_Reflection_DocBlockedAbstract::processGenericInformation
  36. *
  37. * @return void
  38. */
  39. protected function processGenericInformation(DocBlox_Token_Iterator $tokens)
  40. {
  41. if ($tokens->current()->content == 'define')
  42. {
  43. // find the first encapsed string and strip the opening and closing
  44. // apostrophe
  45. $name_token = $tokens->gotoNextByType(
  46. T_CONSTANT_ENCAPSED_STRING, 5, array(',')
  47. );
  48. if (!$name_token)
  49. {
  50. $this->log(
  51. 'Unable to process constant in file ' . $tokens->getFilename()
  52. . ' at line ' . $tokens->current()->getLineNumber(),
  53. DocBlox_Core_Log::CRIT
  54. );
  55. return;
  56. }
  57. $this->setName(substr($name_token->content, 1, -1));
  58. // skip to after the comma
  59. while($tokens->current()->content != ',')
  60. {
  61. if ($tokens->next() === false)
  62. {
  63. break;
  64. }
  65. }
  66. // get everything until the closing brace and use that for value, take child parenthesis under consideration
  67. $value = '';
  68. $level = 0;
  69. while (!(($tokens->current()->content == ')') && ($level == -1)))
  70. {
  71. if ($tokens->next() === false)
  72. {
  73. break;
  74. }
  75. switch($tokens->current()->content)
  76. {
  77. case '(': $level++; break;
  78. case ')': $level--; break;
  79. }
  80. $value .= $tokens->current()->content;
  81. }
  82. $this->setValue(trim(substr($value, 0, -1)));
  83. }
  84. else
  85. {
  86. $this->setName($tokens->gotoNextByType(T_STRING, 5, array('='))->content);
  87. $this->setValue($this->findDefault($tokens));
  88. }
  89. parent::processGenericInformation($tokens);
  90. }
  91. /**
  92. * Stores the value contained in this constant.
  93. *
  94. * @param string $value
  95. *
  96. * @return void
  97. */
  98. public function setValue($value)
  99. {
  100. $this->value = $value;
  101. }
  102. /**
  103. * Returns the value contained in this Constant.
  104. *
  105. * @return string
  106. */
  107. public function getValue()
  108. {
  109. return $this->value;
  110. }
  111. /**
  112. * Returns the XML representation of this object or false if an error occurred.
  113. *
  114. * @return string|boolean
  115. */
  116. public function __toXml()
  117. {
  118. if (!$this->getName())
  119. {
  120. $xml = new SimpleXMLElement('');
  121. return $xml->asXML();
  122. }
  123. $xml = new SimpleXMLElement('<constant></constant>');
  124. $xml->name = $this->getName();
  125. $xml->value = $this->getValue();
  126. $xml['namespace'] = $this->getNamespace();
  127. $xml['line'] = $this->getLineNumber();
  128. $this->addDocblockToSimpleXmlElement($xml);
  129. return $xml->asXML();
  130. }
  131. }