-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYamlTypeDecoder.php
More file actions
39 lines (31 loc) · 921 Bytes
/
YamlTypeDecoder.php
File metadata and controls
39 lines (31 loc) · 921 Bytes
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
<?php
declare(strict_types=1);
namespace Chubbyphp\DecodeEncode\Decoder;
use Chubbyphp\DecodeEncode\RuntimeException;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
final class YamlTypeDecoder implements TypeDecoderInterface
{
public function getContentType(): string
{
return 'application/x-yaml';
}
/**
* @return array<string, mixed>
*
* @throws RuntimeException
*/
public function decode(string $data): array
{
try {
$decoded = Yaml::parse($data);
} catch (ParseException) {
throw RuntimeException::createNotParsable($this->getContentType());
}
if (!\is_array($decoded)) {
throw RuntimeException::createNotParsable($this->getContentType(), 'Not an object');
}
/** @var array<string, mixed> $decoded */
return $decoded;
}
}