1<?php
2/**
3 * This file is part of phpDocumentor.
4 *
5 * For the full copyright and license information, please view the LICENSE
6 * file that was distributed with this source code.
7 *
8 * @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
9 * @license   http://www.opensource.org/licenses/mit-license.php MIT
10 * @link      http://phpdoc.org
11 */
12
13namespace phpDocumentor\Reflection\Types;
14
15use phpDocumentor\Reflection\Fqsen;
16use phpDocumentor\Reflection\Type;
17
18/**
19 * Value Object representing an object.
20 *
21 * An object can be either typed or untyped. When an object is typed it means that it has an identifier, the FQSEN,
22 * pointing to an element in PHP. Object types that are untyped do not refer to a specific class but represent objects
23 * in general.
24 */
25final class Object_ implements Type
26{
27    /** @var Fqsen|null */
28    private $fqsen;
29
30    /**
31     * Initializes this object with an optional FQSEN, if not provided this object is considered 'untyped'.
32     *
33     * @param Fqsen $fqsen
34     * @throws \InvalidArgumentException when provided $fqsen is not a valid type.
35     */
36    public function __construct(Fqsen $fqsen = null)
37    {
38        if (strpos((string)$fqsen, '::') !== false || strpos((string)$fqsen, '()') !== false) {
39            throw new \InvalidArgumentException(
40                'Object types can only refer to a class, interface or trait but a method, function, constant or '
41                . 'property was received: ' . (string)$fqsen
42            );
43        }
44
45        $this->fqsen = $fqsen;
46    }
47
48    /**
49     * Returns the FQSEN associated with this object.
50     *
51     * @return Fqsen|null
52     */
53    public function getFqsen()
54    {
55        return $this->fqsen;
56    }
57
58    /**
59     * Returns a rendered output of the Type as it would be used in a DocBlock.
60     *
61     * @return string
62     */
63    public function __toString()
64    {
65        if ($this->fqsen) {
66            return (string)$this->fqsen;
67        }
68
69        return 'object';
70    }
71}
72