Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
70.83% |
17 / 24 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
SchemaFactory | |
70.83% |
17 / 24 |
|
66.67% |
4 / 6 |
14.00 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getNormalFormFactory | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getCanonicalFormFactory | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getStandAloneFactory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
parse | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
create | |
75.00% |
6 / 8 |
|
0.00% |
0 / 1 |
4.25 |
1 | <?php |
2 | /** |
3 | * WikiLambda SchemaFactory class for generating Open API validators |
4 | * |
5 | * @file |
6 | * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt |
7 | * @license MIT |
8 | */ |
9 | |
10 | namespace MediaWiki\Extension\WikiLambda\Validation; |
11 | |
12 | use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry; |
13 | use Opis\JsonSchema\ISchemaLoader; |
14 | use Opis\JsonSchema\Schema; |
15 | use Opis\JsonSchema\Validator; |
16 | |
17 | class SchemaFactory { |
18 | /** |
19 | * @var ISchemaLoader |
20 | */ |
21 | private $loader; |
22 | |
23 | /** |
24 | * @param ISchemaLoader|null $loader Which loader to use |
25 | */ |
26 | private function __construct( $loader = null ) { |
27 | $this->loader = $loader; |
28 | } |
29 | |
30 | /** |
31 | * Creates a SchemaFactory for normal-form ZObjects. |
32 | * |
33 | * @param ISchemaLoader|null $loader Which loader to use |
34 | * @return SchemaFactory |
35 | */ |
36 | public static function getNormalFormFactory( $loader = null ): SchemaFactory { |
37 | if ( $loader == null ) { |
38 | $loader = new YumYumYamlLoader(); |
39 | } |
40 | $normalDirectory = SchemataUtils::joinPath( SchemataUtils::dataDirectory(), 'NORMAL' ); |
41 | $loader->registerPath( $normalDirectory, 'NORMAL' ); |
42 | return new SchemaFactory( $loader ); |
43 | } |
44 | |
45 | /** |
46 | * Creates a SchemaFactory for canonical-form ZObjects. |
47 | * |
48 | * @param string|null $loader Which loader to use |
49 | * @return SchemaFactory |
50 | */ |
51 | public static function getCanonicalFormFactory( $loader = null ): SchemaFactory { |
52 | if ( $loader == null ) { |
53 | $loader = new YumYumYamlLoader(); |
54 | } |
55 | $canonicalDirectory = SchemataUtils::joinPath( SchemataUtils::dataDirectory(), 'CANONICAL' ); |
56 | $loader->registerPath( $canonicalDirectory, 'CANONICAL' ); |
57 | return new SchemaFactory( $loader ); |
58 | } |
59 | |
60 | /** |
61 | * Creates a SchemaFactory for parsing standalone schemata (no external refs). |
62 | * |
63 | * @return SchemaFactory |
64 | */ |
65 | public static function getStandAloneFactory(): SchemaFactory { |
66 | return new SchemaFactory(); |
67 | } |
68 | |
69 | /** |
70 | * @param mixed $schemaSpec |
71 | * @return SchemaWrapper |
72 | */ |
73 | public function parse( $schemaSpec ) { |
74 | $validator = new Validator(); |
75 | $jsonEncoded = json_encode( $schemaSpec ); |
76 | $schema = Schema::fromJsonString( $jsonEncoded ); |
77 | return new SchemaWrapper( $schema, $validator ); |
78 | } |
79 | |
80 | /** |
81 | * Creates a SchemaWrapper that validates the normalized form of the provided |
82 | * type. |
83 | * |
84 | * @param string $ZID |
85 | * @return SchemaWrapper|null |
86 | */ |
87 | public function create( $ZID ) { |
88 | if ( $ZID == ZTypeRegistry::Z_BOOLEAN_TRUE || $ZID == ZTypeRegistry::Z_BOOLEAN_FALSE ) { |
89 | $ZID = ZTypeRegistry::Z_BOOLEAN; |
90 | } |
91 | // TODO (T300514): Assert that this->loader is not null. |
92 | $schema = $this->loader->loadSchema( $ZID ); |
93 | if ( $schema == null ) { |
94 | return null; |
95 | } |
96 | $validator = new Validator(); |
97 | $validator->setLoader( $this->loader ); |
98 | return new SchemaWrapper( $schema, $validator ); |
99 | } |
100 | } |