PocketMine-MP 5.35.1 git-09f4626fa630fccbe1d56a65a90ff8f3566e4db8
Loading...
Searching...
No Matches
UnionType.php
1<?php
2
3namespace DaveRandom\CallbackValidator\Type;
4
5use function array_shift;
6use function assert;
7use function count;
8
9final class UnionType implements BaseType{
13 public function __construct(
14 public readonly array $types,
15 ){}
16
17 public function stringify(int $depth = 0) : string{
18 if(count($this->types) === 2){
19 //simplify nullable types with ?Type
20 foreach($this->types as $k => $type){
21 if($type instanceof NamedType && $type->type === BuiltInType::NULL){
22 $types = $this->types;
23 unset($types[$k]);
24 $remaining = array_shift($types);
25 assert($remaining !== null);
26 return '?' . $remaining->stringify($depth + 1);
27 }
28 }
29 }
30
31 $result = implode('|', array_map(fn($type) => $type->stringify($depth + 1), $this->types));
32 return $depth === 0 ? $result : "($result)";
33 }
34}
__construct(public readonly array $types,)
Definition UnionType.php:13