1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tdn\PhpTypes\Type\Traits; |
4
|
|
|
|
5
|
|
|
use Tdn\PhpTypes\Exception\InvalidTypeCastException; |
6
|
|
|
use Tdn\PhpTypes\Type\Collection; |
7
|
|
|
use Tdn\PhpTypes\Type\StringType; |
8
|
|
|
use Tdn\PhpTypes\Type\BooleanType; |
9
|
|
|
use Tdn\PhpTypes\Type\IntType; |
10
|
|
|
use Tdn\PhpTypes\Type\FloatType; |
11
|
|
|
use Tdn\PhpTypes\Type\Type; |
12
|
|
|
use Tdn\PhpTypes\Type\TransmutableTypeInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait Transmutable. |
16
|
|
|
*/ |
17
|
|
|
trait Transmutable |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @return StringType |
21
|
|
|
*/ |
22
|
35 |
|
public function toStringType(): StringType |
23
|
|
|
{ |
24
|
35 |
|
return StringType::valueOf($this); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return BooleanType |
29
|
|
|
*/ |
30
|
3 |
|
public function toBoolType(): BooleanType |
31
|
|
|
{ |
32
|
3 |
|
return BooleanType::valueOf($this); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return IntType |
37
|
|
|
*/ |
38
|
4 |
|
public function toIntType(): IntType |
39
|
|
|
{ |
40
|
4 |
|
return IntType::valueOf($this); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return FloatType |
45
|
|
|
*/ |
46
|
5 |
|
public function toFloatType(): FloatType |
47
|
|
|
{ |
48
|
5 |
|
return FloatType::valueOf($this); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return Collection |
53
|
|
|
*/ |
54
|
2 |
|
public function toCollection(): Collection |
55
|
|
|
{ |
56
|
2 |
|
return Collection::valueOf($this); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
1 |
|
public function toBool(): bool |
63
|
|
|
{ |
64
|
1 |
|
if ($this instanceof TransmutableTypeInterface) { |
65
|
1 |
|
return $this(Type::BOOL); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
throw $this->createInvalidTransformationException('bool'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
1 |
|
public function toInt(): int |
75
|
|
|
{ |
76
|
1 |
|
if ($this instanceof TransmutableTypeInterface) { |
77
|
1 |
|
return $this(Type::INT); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
throw $this->createInvalidTransformationException('int'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return float |
85
|
|
|
*/ |
86
|
1 |
|
public function toFloat(): float |
87
|
|
|
{ |
88
|
1 |
|
if ($this instanceof TransmutableTypeInterface) { |
89
|
1 |
|
return $this(Type::FLOAT); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
throw $this->createInvalidTransformationException('float'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
7 |
|
public function toString(): string |
99
|
|
|
{ |
100
|
7 |
|
if ($this instanceof TransmutableTypeInterface) { |
101
|
7 |
|
return $this(Type::STRING); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
throw $this->createInvalidTransformationException('string'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
19 |
|
public function toArray(): array |
111
|
|
|
{ |
112
|
19 |
|
if ($this instanceof TransmutableTypeInterface) { |
113
|
19 |
|
return $this(Type::ARRAY); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
throw $this->createInvalidTransformationException('array'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function __toString(): string |
123
|
|
|
{ |
124
|
|
|
return $this->toString(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function createInvalidTransformationException($type) |
128
|
|
|
{ |
129
|
|
|
return new InvalidTypeCastException(static::class, $type); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|