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\Type;
16
17/**
18 * Represents an array type as described in the PSR-5, the PHPDoc Standard.
19 *
20 * An array can be represented in two forms:
21 *
22 * 1. Untyped (`array`), where the key and value type is unknown and hence classified as 'Mixed_'.
23 * 2. Types (`string[]`), where the value type is provided by preceding an opening and closing square bracket with a
24 *    type name.
25 */
26final class Array_ implements Type
27{
28    /** @var Type */
29    private $valueType;
30
31    /** @var Type */
32    private $keyType;
33
34    /**
35     * Initializes this representation of an array with the given Type or Fqsen.
36     *
37     * @param Type $valueType
38     * @param Type $keyType
39     */
40    public function __construct(Type $valueType = null, Type $keyType = null)
41    {
42        if ($keyType === null) {
43            $keyType = new Compound([ new String_(), new Integer() ]);
44        }
45        if ($valueType === null) {
46            $valueType = new Mixed_();
47        }
48
49        $this->valueType = $valueType;
50        $this->keyType = $keyType;
51    }
52
53    /**
54     * Returns the type for the keys of this array.
55     *
56     * @return Type
57     */
58    public function getKeyType()
59    {
60        return $this->keyType;
61    }
62
63    /**
64     * Returns the value for the keys of this array.
65     *
66     * @return Type
67     */
68    public function getValueType()
69    {
70        return $this->valueType;
71    }
72
73    /**
74     * Returns a rendered output of the Type as it would be used in a DocBlock.
75     *
76     * @return string
77     */
78    public function __toString()
79    {
80        if ($this->valueType instanceof Mixed_) {
81            return 'array';
82        }
83
84        return $this->valueType . '[]';
85    }
86}
87