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<[email protected]> |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
10
|
|
|
* @link http://phpdoc.org |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace phpDocumentor\Reflection\Types; |
14
|
|
|
|
15
|
|
|
use phpDocumentor\Reflection\Type; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Represents a list of values. This is an abstract class for Array_ and Collection. |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractList implements Type |
22
|
|
|
{ |
23
|
|
|
/** @var Type */ |
24
|
|
|
protected $valueType; |
25
|
|
|
|
26
|
|
|
/** @var Type|null */ |
27
|
|
|
protected $keyType; |
28
|
|
|
|
29
|
|
|
/** @var Type */ |
30
|
|
|
protected $defaultKeyType; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initializes this representation of an array with the given Type. |
34
|
|
|
* |
35
|
|
|
* @param Type $valueType |
36
|
|
|
* @param Type $keyType |
37
|
|
|
*/ |
38
|
|
|
public function __construct(Type $valueType = null, Type $keyType = null) |
39
|
|
|
{ |
40
|
|
|
if ($valueType === null) { |
41
|
|
|
$valueType = new Mixed_(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->valueType = $valueType; |
45
|
|
|
$this->defaultKeyType = new Compound([ new String_(), new Integer() ]); |
46
|
|
|
$this->keyType = $keyType; |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Returns the type for the keys of this array. |
52
|
|
|
* |
53
|
|
|
* @return Type |
54
|
|
|
*/ |
55
|
|
|
public function getKeyType() |
56
|
|
|
{ |
57
|
|
|
if ($this->keyType === null) { |
58
|
|
|
return $this->defaultKeyType; |
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->keyType) { |
81
|
|
|
return 'array<'.$this->keyType.','.$this->valueType.'>'; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($this->valueType instanceof Mixed_) { |
85
|
|
|
return 'array'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($this->valueType instanceof Compound) { |
89
|
|
|
return '(' . $this->valueType . ')[]'; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->valueType . '[]'; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|