-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.php
63 lines (53 loc) · 1.59 KB
/
Utils.php
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
<?php
namespace Exbil\LexOffice;
use GuzzleHttp\Psr7\Stream;
class Utils
{
/**
* @param string $resource
* @param array $options
* @return Stream
*/
public static function streamFor($resource = '', array $options = []): Stream
{
if (is_scalar($resource)) {
$stream = fopen('php://temp', 'r+');
if ($resource !== '') {
fwrite($stream, $resource);
fseek($stream, 0);
}
return new Stream($stream, $options);
}
throw new InvalidArgumentException('Invalid resource type: ' . gettype($resource));
}
/**
* @param $value
* @param int $options
* @param int $depth
* @return string
*/
public static function jsonEncode($value, int $options = 0, int $depth = 512): string
{
$json = json_encode($value, $options, $depth);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgumentException('json_encode error: ' . json_last_error_msg());
}
/** @var string */
return $json;
}
/**
* @param string $json
* @param bool $assoc
* @param int $depth
* @param int $options
* @return mixed
*/
public static function jsonDecode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
{
$data = json_decode($json, $assoc, $depth, $options);
if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidArgumentException('json_decode error: ' . json_last_error_msg());
}
return $data;
}
}