|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Swaggest\JsonSchema\Constraint; |
|
4
|
|
|
|
|
5
|
|
|
use Swaggest\JsonSchema\Context; |
|
6
|
|
|
use Swaggest\JsonSchema\Exception\ContentException; |
|
7
|
|
|
|
|
8
|
|
|
class Content |
|
9
|
|
|
{ |
|
10
|
|
|
const MEDIA_TYPE_APPLICATION_JSON = 'application/json'; |
|
11
|
|
|
const ENCODING_BASE64 = 'base64'; |
|
12
|
|
|
|
|
13
|
|
|
const BASE64_INVALID_REGEX = '_[^A-Za-z0-9+/=]+_'; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param Context $options |
|
18
|
|
|
* @param string|null $encoding |
|
19
|
|
|
* @param string|null $mediaType |
|
20
|
|
|
* @param string $data |
|
21
|
|
|
* @param bool $import |
|
22
|
|
|
* @return bool|mixed|string |
|
23
|
|
|
* @throws ContentException |
|
24
|
|
|
*/ |
|
25
|
20 |
|
public static function process(Context $options, $encoding, $mediaType, $data, $import = true) |
|
26
|
|
|
{ |
|
27
|
20 |
|
if ($import) { |
|
28
|
20 |
|
if ($encoding !== null) { |
|
29
|
|
|
switch ($encoding) { |
|
30
|
14 |
|
case self::ENCODING_BASE64: |
|
31
|
14 |
|
if ($options->strictBase64Validation && preg_match(self::BASE64_INVALID_REGEX, $data)) { |
|
32
|
2 |
|
throw new ContentException('Invalid base64 string'); |
|
33
|
|
|
} |
|
34
|
12 |
|
$data = base64_decode($data); |
|
35
|
12 |
|
if ($data === false && !$options->skipValidation) { // @phpstan-ignore-line |
|
36
|
|
|
throw new ContentException('Unable to decode base64'); |
|
37
|
|
|
} |
|
38
|
12 |
|
break; |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
18 |
|
if ($mediaType !== null && $data !== false) { |
|
43
|
|
|
switch ($mediaType) { |
|
44
|
13 |
|
case self::MEDIA_TYPE_APPLICATION_JSON: |
|
45
|
13 |
|
$data = json_decode($data); |
|
46
|
13 |
|
$lastErrorCode = json_last_error(); |
|
47
|
13 |
|
if (($lastErrorCode !== JSON_ERROR_NONE) && !$options->skipValidation) { |
|
48
|
|
|
// TODO add readable error message |
|
49
|
2 |
|
throw new ContentException('Unable to decode json, err code: ' . $lastErrorCode); |
|
50
|
|
|
} |
|
51
|
11 |
|
break; |
|
52
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
16 |
|
return $data; |
|
57
|
|
|
} else { |
|
58
|
|
|
// export |
|
59
|
|
|
|
|
60
|
6 |
|
if ($mediaType !== null) { |
|
61
|
|
|
switch ($mediaType) { |
|
62
|
4 |
|
case self::MEDIA_TYPE_APPLICATION_JSON: |
|
63
|
4 |
|
$data = json_encode($data); |
|
64
|
4 |
|
$lastErrorCode = json_last_error(); |
|
65
|
4 |
|
if (($lastErrorCode !== JSON_ERROR_NONE) && !$options->skipValidation) { |
|
66
|
|
|
// TODO add readable error message |
|
67
|
|
|
throw new ContentException('Unable to encode json, err code: ' . $lastErrorCode); |
|
68
|
|
|
} |
|
69
|
4 |
|
break; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
6 |
|
if ($encoding !== null && $data !== false) { |
|
74
|
|
|
switch ($encoding) { |
|
75
|
4 |
|
case self::ENCODING_BASE64: |
|
76
|
4 |
|
$data = base64_encode($data); |
|
77
|
4 |
|
break; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
6 |
|
return $data; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |