Fqsen.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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('/^\\\\([\\w_\\\\]*)(?:[:]{2}\\$?([\\w_]+))?(?:\\(\\))?$/', $fqsen, $matches);
  38. if ($result === 0) {
  39. throw new \InvalidArgumentException(
  40. sprintf('"%s" is not a valid Fqsen.', $fqsen)
  41. );
  42. }
  43. $this->fqsen = $fqsen;
  44. if (isset($matches[2])) {
  45. $this->name = $matches[2];
  46. } else {
  47. $matches = explode('\\', $fqsen);
  48. $this->name = trim(end($matches), '()');
  49. }
  50. }
  51. /**
  52. * converts this class to string.
  53. *
  54. * @return string
  55. */
  56. public function __toString()
  57. {
  58. return $this->fqsen;
  59. }
  60. /**
  61. * Returns the name of the element without path.
  62. *
  63. * @return string
  64. */
  65. public function getName()
  66. {
  67. return $this->name;
  68. }
  69. }