Skip to content

Commit adc2652

Browse files
author
Kirill Nesmeyanov
committed
Add Mapper::getType() and Mapper::getTypeByValue() methods.
1 parent 807a525 commit adc2652

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

src/Mapper.php

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use JetBrains\PhpStorm\Language;
88
use TypeLang\Mapper\Exception\Definition\TypeNotFoundException;
9-
use TypeLang\Mapper\Exception\Mapping\RuntimeException;
109
use TypeLang\Mapper\Platform\PlatformInterface;
1110
use TypeLang\Mapper\Platform\StandardPlatform;
1211
use TypeLang\Mapper\Runtime\Configuration;
@@ -109,14 +108,11 @@ public function getParser(): TypeParserFacadeInterface
109108
return $this->parser;
110109
}
111110

112-
/**
113-
* @throws RuntimeException
114-
* @throws TypeNotFoundException
115-
* @throws \Throwable
116-
*/
117111
public function normalize(mixed $value, #[Language('PHP')] ?string $type = null): mixed
118112
{
119-
$instance = $this->getType($value, $type);
113+
$instance = $type === null
114+
? $this->types->getTypeByValue($value)
115+
: $this->types->getTypeByDefinition($type);
120116

121117
return $instance->cast($value, RootContext::forNormalization(
122118
value: $value,
@@ -126,13 +122,11 @@ public function normalize(mixed $value, #[Language('PHP')] ?string $type = null)
126122
));
127123
}
128124

129-
/**
130-
* @throws TypeNotFoundException
131-
* @throws \Throwable
132-
*/
133125
public function isNormalizable(mixed $value, #[Language('PHP')] ?string $type = null): bool
134126
{
135-
$instance = $this->getType($value, $type);
127+
$instance = $type === null
128+
? $this->types->getTypeByValue($value)
129+
: $this->types->getTypeByDefinition($type);
136130

137131
return $instance->match($value, RootContext::forNormalization(
138132
value: $value,
@@ -142,14 +136,9 @@ public function isNormalizable(mixed $value, #[Language('PHP')] ?string $type =
142136
));
143137
}
144138

145-
/**
146-
* @throws RuntimeException
147-
* @throws TypeNotFoundException
148-
* @throws \Throwable
149-
*/
150139
public function denormalize(mixed $value, #[Language('PHP')] string $type): mixed
151140
{
152-
$instance = $this->getType($value, $type);
141+
$instance = $this->types->getTypeByDefinition($type);
153142

154143
return $instance->cast($value, RootContext::forDenormalization(
155144
value: $value,
@@ -159,13 +148,9 @@ public function denormalize(mixed $value, #[Language('PHP')] string $type): mixe
159148
));
160149
}
161150

162-
/**
163-
* @throws TypeNotFoundException
164-
* @throws \Throwable
165-
*/
166151
public function isDenormalizable(mixed $value, #[Language('PHP')] string $type): bool
167152
{
168-
$instance = $this->getType($value, $type);
153+
$instance = $this->types->getTypeByDefinition($type);
169154

170155
return $instance->match($value, RootContext::forDenormalization(
171156
value: $value,
@@ -176,21 +161,38 @@ public function isDenormalizable(mixed $value, #[Language('PHP')] string $type):
176161
}
177162

178163
/**
179-
* @param non-empty-string|null $type
164+
* Returns type for mapping by signature.
180165
*
181-
* @throws TypeNotFoundException
182-
* @throws \Throwable
166+
* @api
167+
* @param non-empty-string $type
168+
* @throws TypeNotFoundException in case of type not found
169+
* @throws \Throwable in case of internal error occurs
183170
*/
184-
private function getType(mixed $value, ?string $type): TypeInterface
171+
public function getType(#[Language('PHP')] string $type): TypeInterface
185172
{
186-
if ($type === null) {
187-
return $this->types->getTypeByValue($value);
188-
}
189-
190173
return $this->types->getTypeByDefinition($type);
191174
}
192175

193176
/**
177+
* Returns type for mapping by value.
178+
*
179+
* @api
180+
* @throws TypeNotFoundException in case of type not found
181+
* @throws \Throwable in case of internal error occurs
182+
*/
183+
public function getTypeByValue(mixed $value): TypeInterface
184+
{
185+
return $this->types->getTypeByValue($value);
186+
}
187+
188+
/**
189+
* Warms up the cache for the selected class or object.
190+
*
191+
* Please note that the cache can only be warmed up if the
192+
* appropriate driver is used otherwise it doesn't give any effect.
193+
*
194+
* @api
195+
*
194196
* @param class-string|object $class
195197
*
196198
* @throws TypeNotFoundException

0 commit comments

Comments
 (0)