Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
32 / 32 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
Loader | |
100.00% |
32 / 32 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
1 / 1 |
asArray | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
3 | |||
asJson | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getSchemaPath | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace BO\Zmsentities\Schema; |
4 | |
5 | use Exception; |
6 | |
7 | class Loader |
8 | { |
9 | public static function asArray($schemaFilename) |
10 | { |
11 | $jsonString = self::asJson($schemaFilename); |
12 | $array = json_decode($jsonString, true); |
13 | $object = json_decode($jsonString); |
14 | if (null === $array && $jsonString) { |
15 | $json_error = json_last_error(); |
16 | $json_error_list = array( |
17 | JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', |
18 | JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
19 | JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', |
20 | JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', |
21 | JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
22 | JSON_ERROR_NONE => 'No errors', |
23 | ); |
24 | throw new \BO\Zmsentities\Exception\SchemaFailedParseJsonFile( |
25 | "Could not parse JSON File $schemaFilename: " . $json_error_list[$json_error] |
26 | ); |
27 | } |
28 | $schema = new Schema($array); |
29 | $schema->setJsonObject($object); |
30 | return $schema; |
31 | } |
32 | |
33 | public static function asJson($schemaFilename) |
34 | { |
35 | if (!$schemaFilename) { |
36 | throw new \BO\Zmsentities\Exception\SchemaMissingJsonFile("Missing JSON-Schema file"); |
37 | } |
38 | $directory = preg_match('#^/#', $schemaFilename) ? '' : self::getSchemaPath(); |
39 | $filename = $directory . DIRECTORY_SEPARATOR . $schemaFilename; |
40 | return file_get_contents($filename); |
41 | } |
42 | |
43 | public static function getSchemaPath() |
44 | { |
45 | $pathTrace = [ |
46 | __DIR__, |
47 | '..', |
48 | '..', |
49 | '..', |
50 | 'schema' |
51 | ]; |
52 | return realpath(implode(DIRECTORY_SEPARATOR, $pathTrace)); |
53 | } |
54 | } |