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