Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
JsonSchemaIndex
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 3
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 indexSubtree
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
42
 newRef
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * JSON Schema Validation Library
4 *
5 * Copyright (c) 2005-2012, Rob Lanphier
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 *     * Redistributions of source code must retain the above copyright
13 *       notice, this list of conditions and the following disclaimer.
14 *
15 *     * Redistributions in binary form must reproduce the above
16 *       copyright notice, this list of conditions and the following
17 *       disclaimer in the documentation and/or other materials provided
18 *       with the distribution.
19 *
20 *     * Neither my name nor the names of my contributors may be used to
21 *       endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 * @author Rob Lanphier <robla@wikimedia.org>
37 * @copyright © 2011-2012 Rob Lanphier
38 * @license http://jsonwidget.org/LICENSE BSD-3-Clause
39 */
40
41namespace MediaWiki\Extension\EventLogging\Libs\JsonSchemaValidation;
42
43use Exception;
44
45/**
46 * The JsonSchemaIndex object holds all schema refs with an "id", and is used
47 * to resolve an idref to a schema ref.  This also holds the root of the schema
48 * tree.  This also serves as sort of a class factory for schema refs.
49 */
50class JsonSchemaIndex {
51    /** @var array|null */
52    public $root;
53    /** @var array */
54    public $idtable;
55
56    /**
57     * The whole tree is indexed on instantiation of this class.
58     * @param array|null $schema
59     */
60    public function __construct( $schema ) {
61        $this->root = $schema;
62        $this->idtable = [];
63
64        if ( $this->root === null ) {
65            return;
66        }
67
68        $this->indexSubtree( $this->root );
69    }
70
71    /**
72     * Recursively find all of the ids in this schema, and store them in the
73     * index.
74     * @param array $schemanode
75     */
76    public function indexSubtree( $schemanode ) {
77        if ( !array_key_exists( 'type', $schemanode ) ) {
78            $schemanode['type'] = 'any';
79        }
80        $nodetype = $schemanode['type'];
81        switch ( $nodetype ) {
82            case 'object':
83                foreach ( $schemanode['properties'] as $value ) {
84                    $this->indexSubtree( $value );
85                }
86
87                break;
88            case 'array':
89                $this->indexSubtree( $schemanode['items'] );
90                break;
91        }
92        if ( isset( $schemanode['id'] ) ) {
93            $this->idtable[$schemanode['id']] = $schemanode;
94        }
95    }
96
97    /**
98     * Generate a new schema ref, or return an existing one from the index if
99     * the node is an idref.
100     * @param array $node
101     * @param TreeRef|null $parent
102     * @param int|null $nodeindex
103     * @param string $nodename
104     * @return TreeRef
105     * @throws JsonSchemaException
106     */
107    public function newRef( $node, $parent, $nodeindex, $nodename ) {
108        if ( array_key_exists( '$ref', $node ) ) {
109            if ( strspn( $node['$ref'], '#' ) != 1 ) {
110                throw new JsonSchemaException( 'jsonschema-badidref', $node['$ref'] );
111            }
112            $idref = $node['$ref'];
113            try {
114                $node = $this->idtable[$idref];
115            } catch ( Exception $e ) {
116                throw new JsonSchemaException( 'jsonschema-badidref', $node['$ref'] );
117            }
118        }
119
120        return new TreeRef( $node, $parent, $nodeindex, $nodename );
121    }
122}