-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonEncoder.php
More file actions
102 lines (90 loc) · 2.98 KB
/
JsonEncoder.php
File metadata and controls
102 lines (90 loc) · 2.98 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
<?php
/*
* This file is part of the Artemeon Core - Web Application Framework.
*
* (c) Artemeon <www.artemeon.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Artemeon\HttpClient\Http\Body\Encoder;
use Artemeon\HttpClient\Exception\RuntimeException;
use Artemeon\HttpClient\Http\MediaType;
use Artemeon\HttpClient\Stream\Stream;
use Override;
use Psr\Http\Message\StreamInterface;
/**
* Encoder for "application/json" encoded body content.
*/
class JsonEncoder implements Encoder
{
/**
* @param array<array-key, mixed>|object $value
* @param int $options Optional json encode options: @see https://www.php.net/manual/de/function.json-encode.php
* @param string $mimeType Optional custom mime type
*/
private function __construct(
private readonly array | object $value,
private readonly int $options = 0,
private readonly string $mimeType = MediaType::JSON,
) {
}
/**
* Named constructor to create an instance based on the given array.
*
* ```php
* # Associative arrays are always encoded as json object:
* $encoder = JsonEncoder::fromArray(['username' = 'John.Doe'])
*
* # Use JSON options: @see https://www.php.net/manual/en/function.json-encode.php
* $encoder = JsonEncoder::fromArray(
* ['value1', 'value2','value3'],
* JSON_OBJECT_AS_ARRAY | JSON_UNESCAPED_UNICODE
* )
*
* $encoder->encode();
* ```
*
* @param array<array-key, mixed> $value Array to encode, associative array always encoded as json object
* @param int $options Optional Bitmask of json constants: @see https://www.php.net/manual/en/function.json-encode.php
* @param string $mimeType Optional custom mime type
*/
public static function fromArray(array $value, int $options = 0, string $mimeType = MediaType::JSON): self
{
return new self($value, $options, $mimeType);
}
/**
* Named constructor to create an instance based on the given object.
*
* @param object $value Object to encode
* @param int $options Bitmask of json constants:
* @see https://www.php.net/manual/en/function.json-encode.php
*/
public static function fromObject(object $value, int $options = 0, string $mimeType = MediaType::JSON): self
{
return new self($value, $options, $mimeType);
}
/**
* @inheritDoc
* @throws RuntimeException
*/
#[Override]
public function encode(): StreamInterface
{
$json = json_encode($this->value, $this->options);
if ($json === false) {
$error = json_last_error_msg();
throw new RuntimeException("Can't encode to json: $error");
}
return Stream::fromString($json);
}
/**
* @inheritDoc
*/
#[Override]
public function getMimeType(): string
{
return $this->mimeType;
}
}