Fqsen.php 1.8 KB

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