Fqsen.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * phpDocumentor
  4. *
  5. * PHP Version 5.5
  6. *
  7. * @copyright 2010-2015 Mike van Riel / Naenius (http://www.naenius.com)
  8. * @license http://www.opensource.org/licenses/mit-license.php MIT
  9. * @link http://phpdoc.org
  10. */
  11. namespace phpDocumentor\Reflection;
  12. /**
  13. * Value Object for Fqsen.
  14. *
  15. * @link https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc-meta.md
  16. */
  17. final class Fqsen
  18. {
  19. /**
  20. * @var string full quallified class name
  21. */
  22. private $fqsen;
  23. /**
  24. * @var string name of the element without path.
  25. */
  26. private $name;
  27. /**
  28. * Initializes the object.
  29. *
  30. * @param string $fqsen
  31. *
  32. * @throws \InvalidArgumentException when $fqsen is not matching the format.
  33. */
  34. public function __construct($fqsen)
  35. {
  36. $matches = array();
  37. $result = preg_match(
  38. '/^\\\\([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff\\\\]*)?(?:[:]{2}\\$?([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*))?(?:\\(\\))?$/',
  39. $fqsen,
  40. $matches
  41. );
  42. if ($result === 0) {
  43. throw new \InvalidArgumentException(
  44. sprintf('"%s" is not a valid Fqsen.', $fqsen)
  45. );
  46. }
  47. $this->fqsen = $fqsen;
  48. if (isset($matches[2])) {
  49. $this->name = $matches[2];
  50. } else {
  51. $matches = explode('\\', $fqsen);
  52. $this->name = trim(end($matches), '()');
  53. }
  54. }
  55. /**
  56. * converts this class to string.
  57. *
  58. * @return string
  59. */
  60. public function __toString()
  61. {
  62. return $this->fqsen;
  63. }
  64. /**
  65. * Returns the name of the element without path.
  66. *
  67. * @return string
  68. */
  69. public function getName()
  70. {
  71. return $this->name;
  72. }
  73. }