Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Loader
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 asArray
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
3
 asJson
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 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        $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    /**
34     * @return false|string
35     */
36    public static function asJson($schemaFilename): string|false
37    {
38        if (!$schemaFilename) {
39            throw new \BO\Zmsentities\Exception\SchemaMissingJsonFile("Missing JSON-Schema file");
40        }
41        $directory = preg_match('#^/#', $schemaFilename) ? '' : self::getSchemaPath();
42        $filename = $directory . DIRECTORY_SEPARATOR . $schemaFilename;
43        return file_get_contents($filename);
44    }
45
46    /**
47     * @return false|string
48     */
49    public static function getSchemaPath(): string|false
50    {
51        $pathTrace = [
52            __DIR__,
53            '..',
54            '..',
55            '..',
56            'schema'
57        ];
58        return realpath(implode(DIRECTORY_SEPARATOR, $pathTrace));
59    }
60}