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
12namespace phpDocumentor\Reflection;
13
14/**
15 * Value Object for Fqsen.
16 *
17 * @link https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc-meta.md
18 */
19final class Fqsen
20{
21    /**
22     * @var string full quallified class name
23     */
24    private $fqsen;
25
26    /**
27     * @var string name of the element without path.
28     */
29    private $name;
30
31    /**
32     * Initializes the object.
33     *
34     * @param string $fqsen
35     *
36     * @throws \InvalidArgumentException when $fqsen is not matching the format.
37     */
38    public function __construct($fqsen)
39    {
40        $matches = array();
41        $result = preg_match(
42            '/^\\\\([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff\\\\]*)?(?:[:]{2}\\$?([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*))?(?:\\(\\))?$/',
43                $fqsen,
44                $matches
45        );
46
47        if ($result === 0) {
48            throw new \InvalidArgumentException(
49                sprintf('"%s" is not a valid Fqsen.', $fqsen)
50            );
51        }
52
53        $this->fqsen = $fqsen;
54
55        if (isset($matches[2])) {
56            $this->name = $matches[2];
57        } else {
58            $matches = explode('\\', $fqsen);
59            $this->name = trim(end($matches), '()');
60        }
61    }
62
63    /**
64     * converts this class to string.
65     *
66     * @return string
67     */
68    public function __toString()
69    {
70        return $this->fqsen;
71    }
72
73    /**
74     * Returns the name of the element without path.
75     *
76     * @return string
77     */
78    public function getName()
79    {
80        return $this->name;
81    }
82}
83