-
-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathtype.texy
More file actions
199 lines (131 loc) · 4.45 KB
/
type.texy
File metadata and controls
199 lines (131 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
PHP Tip
*******
.[perex]
[api:Nette\Utils\Type] je razred za delo s podatkovnimi tipi PHP.
Namestitev:
```shell
composer require nette/utils
```
Vsi primeri predpostavljajo ustvarjen alias:
```php
use Nette\Utils\Type;
```
fromReflection($reflection): ?Type .[method]
--------------------------------------------
Statična metoda ustvari objekt Type na podlagi refleksije. Parameter je lahko objekt `ReflectionMethod` ali `ReflectionFunction` (vrne tip vrnjene vrednosti) ali `ReflectionParameter` ali `ReflectionProperty`. Prevede `self`, `static` in `parent` v dejansko ime razreda. Če subjekt nima nobenega tipa, vrne `null`.
```php
class DemoClass
{
public self $foo;
}
$prop = new ReflectionProperty(DemoClass::class, 'foo');
echo Type::fromReflection($prop); // 'DemoClass'
```
fromString(string $type): Type .[method]
----------------------------------------
Statična metoda ustvari objekt Type glede na besedilni zapis.
```php
$type = Type::fromString('Foo|Bar');
echo $type; // 'Foo|Bar'
```
getNames(): (string|array)[] .[method]
--------------------------------------
Vrne polje podtipov, iz katerih je sestavljen sestavljeni tip, kot nize.
```php
$type = Type::fromString('string|null'); // ali '?string'
$type->getNames(); // ['string', 'null']
$type = Type::fromString('(Foo&Bar)|string');
$type->getNames(); // [['Foo', 'Bar'], 'string']
```
getTypes(): Type[] .[method]
----------------------------
Vrne polje podtipov, iz katerih je sestavljen sestavljeni tip, kot objekte `ReflectionType`:
```php
$type = Type::fromString('string|null'); // ali '?string'
$type->getTypes(); // [Type::fromString('string'), Type::fromString('null')]
$type = Type::fromString('(Foo&Bar)|string');
$type->getTypes(); // [Type::fromString('Foo&Bar'), Type::fromString('string')]
$type = Type::fromString('Foo&Bar');
$type->getTypes(); // [Type::fromString('Foo'), Type::fromString('Bar')]
```
getSingleName(): ?string .[method]
----------------------------------
Pri preprostih tipih vrne ime tipa, sicer null.
```php
$type = Type::fromString('string|null');
echo $type; // '?string'
echo $type->getSingleName(); // 'string'
$type = Type::fromString('?Foo');
echo $type; // '?Foo'
echo $type->getSingleName(); // 'Foo'
$type = Type::fromString('Foo|Bar');
echo $type; // 'Foo|Bar'
echo $type->getSingleName(); // null
```
isSimple(): bool .[method]
--------------------------
Vrne, ali gre za preprost tip. Za preproste tipe se štejejo tudi preprosti nullable tipi:
```php
$type = Type::fromString('string');
$type->isSimple(); // true
$type->isUnion(); // false
$type = Type::fromString('?Foo'); // ali 'Foo|null'
$type->isSimple(); // true
$type->isUnion(); // true
```
isUnion(): bool .[method]
-------------------------
Vrne, ali gre za union tip.
```php
$type = Type::fromString('string|int');
$type->isUnion(); // true
```
isIntersection(): bool .[method]
--------------------------------
Vrne, ali gre za intersection tip.
```php
$type = Type::fromString('Foo&Bar');
$type->isIntersection(); // true
```
isBuiltin(): bool .[method]
---------------------------
Vrne, ali je tip preprost in hkrati vgrajen tip PHP.
```php
$type = Type::fromString('string');
$type->isBuiltin(); // true
$type = Type::fromString('string|int');
$type->isBuiltin(); // false
$type = Type::fromString('Foo');
$type->isBuiltin(); // false
```
isClass(): bool .[method]
-------------------------
Vrne, ali je tip preprost in hkrati ime razreda.
```php
$type = Type::fromString('string');
$type->isClass(); // false
$type = Type::fromString('Foo|null');
$type->isClass(); // true
$type = Type::fromString('Foo|Bar');
$type->isClass(); // false
```
isClassKeyword(): bool .[method]
--------------------------------
Vrne, ali je tip eden od notranjih tipov `self`, `parent`, `static`.
```php
$type = Type::fromString('self');
$type->isClassKeyword(); // true
$type = Type::fromString('Foo');
$type->isClassKeyword(); // false
```
allows(string $type): bool .[method]
------------------------------------
Metoda `allows()` preverja združljivost tipov. Na primer, omogoča ugotoviti, ali bi vrednost določenega tipa lahko bila posredovana kot parameter.
```php
$type = Type::fromString('string|null');
$type->allows('string'); // true
$type->allows('null'); // true
$type->allows('Foo'); // false
$type = Type::fromString('mixed');
$type->allows('null'); // true
```