|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Minotaur |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
|
8
|
|
|
* the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
15
|
|
|
* License for the specific language governing permissions and limitations under |
|
16
|
|
|
* the License. |
|
17
|
|
|
* |
|
18
|
|
|
* @copyright 2015-2017 Appertly |
|
19
|
|
|
* @license Apache-2.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace Minotaur\Net; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* A class that converts JSON data to equivalent Hack collections. |
|
25
|
|
|
*/ |
|
26
|
|
|
class JsonMapper |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Converts a JSON string to a Vector. |
|
30
|
|
|
* |
|
31
|
|
|
* @param $json - The JSON to convert |
|
32
|
|
|
* @return \ConstVector The `Vector` version |
|
33
|
|
|
* @throws \Minotaur\Net\Exception\Illegible if the JSON is invalid |
|
34
|
|
|
*/ |
|
35
|
|
|
public function toVector(?string $json): \ConstVector |
|
36
|
|
|
{ |
|
37
|
|
|
$a = $json === null ? null : json_decode($json, true); |
|
38
|
|
|
if (!is_array($a)) { |
|
39
|
|
|
throw new Exception\Illegible($json, "Invalid JSON array"); |
|
40
|
|
|
} |
|
41
|
|
|
return new \HH\Vector($a); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Converts a JSON string to a Map. |
|
46
|
|
|
* |
|
47
|
|
|
* @param $json - The JSON to convert |
|
48
|
|
|
* @return \ConstMap The `Map` version |
|
49
|
|
|
* @throws \Minotaur\Net\Exception\Illegible if the JSON is invalid |
|
50
|
|
|
*/ |
|
51
|
|
|
public function toMap(?string $json): \ConstMap |
|
52
|
|
|
{ |
|
53
|
|
|
$a = $json === null ? null : json_decode($json, true); |
|
54
|
|
|
if (!is_array($a)) { |
|
55
|
|
|
throw new Exception\Illegible($json, "Invalid JSON map"); |
|
56
|
|
|
} |
|
57
|
|
|
return new \HH\Map($a); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|